-
-
Notifications
You must be signed in to change notification settings - Fork 782
Add pants-plugins/pack_metadata to inform pants about our packs
#5868
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+372
−2
Merged
Changes from 22 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
e7aab82
pants: new pack_metadata plugin
cognifloyd 77af55d
pants: register tailor rules in pack_metadata plugin
cognifloyd 41a00b8
pants: do not include requirements.txt in metadata target
cognifloyd e70f926
use pack_metadata for st2tests.fixtures.packs.test_content_version_fi…
cognifloyd 13e8378
Try to print instructions if git submodule not initiated
cognifloyd 561079c
Try to use Sources.validate_resolved_files instead
cognifloyd c6ef08c
Try to use Sources.validate_resolved_files again
cognifloyd 421231a
Simplify pack_metadata_in_git_submodule
cognifloyd 94fc704
move pack_metadata_in_git_submodule to pack_metadata plugin
cognifloyd 8716187
Finish git submodule instructions
cognifloyd 835002d
add license header
cognifloyd 892b001
update to pants 2.8 (plugin apis, BUILD updates)
cognifloyd 84073a8
make api_spec and pack_metadata generate Resource targets
cognifloyd 4a1b241
Disable pack_metadata plugin until next PR
cognifloyd 16213a1
drop old comment
cognifloyd b6074c4
add tests for pants-plugins/pack_metadata
cognifloyd 185e871
add documentation for pants-plugins/pack_metadata
cognifloyd 8e78e31
update changelog entry
cognifloyd 4d0bd8f
fix pants-plugins/pack_metadata test
cognifloyd ac1c097
simplify and fix pants-plugins/pack_metadata test
cognifloyd 6e4afe0
fix exception check in pants-plugins/pack_metadata test
cognifloyd 6fbb1df
flake8 fixes
cognifloyd a73225c
Fix typo
cognifloyd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| python_sources() | ||
|
|
||
| python_tests( | ||
| name="tests", | ||
| ) |
Empty file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| # Copyright 2023 The StackStorm Authors. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| from pack_metadata import tailor | ||
| from pack_metadata.target_types import PackMetadata, PackMetadataInGitSubmodule | ||
|
|
||
|
|
||
| def rules(): | ||
| return tailor.rules() | ||
|
|
||
|
|
||
| def target_types(): | ||
| return [PackMetadata, PackMetadataInGitSubmodule] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,62 @@ | ||
| # Copyright 2023 The StackStorm Authors. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| import os | ||
| from dataclasses import dataclass | ||
|
|
||
| from pants.core.goals.tailor import ( | ||
| AllOwnedSources, | ||
| PutativeTarget, | ||
| PutativeTargets, | ||
| PutativeTargetsRequest, | ||
| ) | ||
| from pants.engine.fs import PathGlobs, Paths | ||
| from pants.engine.rules import collect_rules, Get, rule, UnionRule | ||
| from pants.util.logging import LogLevel | ||
|
|
||
| from pack_metadata.target_types import PackMetadata | ||
|
|
||
|
|
||
| @dataclass(frozen=True) | ||
| class PutativePackMetadataTargetsRequest(PutativeTargetsRequest): | ||
| pass | ||
|
|
||
|
|
||
| @rule( | ||
| desc="Find pack (config, action, alias, sensor, icon, etc) metadata files.", | ||
| level=LogLevel.DEBUG, | ||
| ) | ||
| async def find_putative_targets( | ||
| _: PutativePackMetadataTargetsRequest, all_owned_sources: AllOwnedSources | ||
| ) -> PutativeTargets: | ||
| all_pack_yaml_files = await Get(Paths, PathGlobs(["**/pack.yaml"])) | ||
|
|
||
| unowned_pack_yaml_files = set(all_pack_yaml_files.files) - set(all_owned_sources) | ||
| unowned_pack_dirs = [os.path.dirname(p) for p in unowned_pack_yaml_files] | ||
|
|
||
| name = "metadata" | ||
| return PutativeTargets( | ||
| [ | ||
| PutativeTarget.for_target_type( | ||
| PackMetadata, dirname, name, ("pack.yaml",), kwargs={"name": name} | ||
| ) | ||
| for dirname in unowned_pack_dirs | ||
| ] | ||
| ) | ||
|
|
||
|
|
||
| def rules(): | ||
| return [ | ||
| *collect_rules(), | ||
| UnionRule(PutativeTargetsRequest, PutativePackMetadataTargetsRequest), | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,107 @@ | ||
| # Copyright 2023 The StackStorm Authors. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
|
|
||
| from pants.core.goals.tailor import ( | ||
| AllOwnedSources, | ||
| PutativeTarget, | ||
| PutativeTargets, | ||
| ) | ||
| from pants.testutil.rule_runner import QueryRule, RuleRunner | ||
|
|
||
| from .tailor import ( | ||
| PutativePackMetadataTargetsRequest, | ||
| rules as pack_metadata_rules, | ||
| ) | ||
| from .target_types import PackMetadata, PackMetadataInGitSubmodule | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def rule_runner() -> RuleRunner: | ||
| return RuleRunner( | ||
| rules=[ | ||
| *pack_metadata_rules(), | ||
| QueryRule( | ||
| PutativeTargets, (PutativePackMetadataTargetsRequest, AllOwnedSources) | ||
| ), | ||
| ], | ||
| target_types=[PackMetadata, PackMetadataInGitSubmodule], | ||
| ) | ||
|
|
||
|
|
||
| def test_find_putative_targets(rule_runner: RuleRunner) -> None: | ||
| rule_runner.write_files( | ||
| { | ||
| "packs/already_owned/pack.yaml": "---\nname: already_owned\n", | ||
| "packs/already_owned/actions/action.yaml": "---\nname: action\n", | ||
| "packs/foo/pack.yaml": "---\nname: foo\n", | ||
| "packs/foo/actions/action.yaml": "---\nname: action\n", | ||
| "packs/bar/pack.yaml": "---\nname: bar\n", | ||
| "packs/bar/sensors/sensor.yaml": "---\nname: sensor\n", | ||
| "other/deep/baz/pack.yaml": "---\nname: baz\n", | ||
| } | ||
| ) | ||
| pts = rule_runner.request( | ||
| PutativeTargets, | ||
| [ | ||
| PutativePackMetadataTargetsRequest( | ||
| ( | ||
| "packs", | ||
| "packs/already_owned", | ||
| "packs/already_owned/actions", | ||
| "packs/foo", | ||
| "packs/foo/actions", | ||
| "packs/bar", | ||
| "packs/bar/sensors", | ||
| "other/deep/baz", | ||
| ) | ||
| ), | ||
| AllOwnedSources( | ||
| [ | ||
| "packs/already_owned/pack.yaml", | ||
| "packs/already_owned/actions/action.yaml", | ||
| ] | ||
| ), | ||
| ], | ||
| ) | ||
| assert ( | ||
| PutativeTargets( | ||
| [ | ||
| PutativeTarget.for_target_type( | ||
| PackMetadata, | ||
| path="packs/foo", | ||
| name="metadata", | ||
| triggering_sources=["pack.yaml"], | ||
| kwargs={"name": "metadata"}, | ||
| ), | ||
| PutativeTarget.for_target_type( | ||
| PackMetadata, | ||
| path="packs/bar", | ||
| name="metadata", | ||
| triggering_sources=["pack.yaml"], | ||
| kwargs={"name": "metadata"}, | ||
| ), | ||
| PutativeTarget.for_target_type( | ||
| PackMetadata, | ||
| path="other/deep/baz", | ||
| name="metadata", | ||
| triggering_sources=["pack.yaml"], | ||
| kwargs={"name": "metadata"}, | ||
| ), | ||
| ] | ||
| ) | ||
| == pts | ||
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| # Copyright 2023 The StackStorm Authors. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| from typing import Sequence | ||
|
|
||
| from pants.engine.target import COMMON_TARGET_FIELDS, Dependencies | ||
| from pants.core.target_types import ( | ||
| ResourcesGeneratingSourcesField, | ||
| ResourcesGeneratorTarget, | ||
| ) | ||
|
|
||
|
|
||
| class UnmatchedGlobsError(Exception): | ||
| """Error thrown when a required set of globs didn't match.""" | ||
|
|
||
|
|
||
| class PackMetadataSourcesField(ResourcesGeneratingSourcesField): | ||
| required = False | ||
| default = ( | ||
| # metadata does not include any python, shell, or other sources. | ||
| "pack.yaml", | ||
| "config.schema.yaml", | ||
| "*.yaml.example", | ||
| "**/*.yaml", | ||
| "**/*.yml", | ||
| "icon.png", # used in st2web ui | ||
| # "requirements*.txt", # including this causes target conflicts | ||
| # "README.md", | ||
| # "HISTORY.md", | ||
| ) | ||
|
|
||
|
|
||
| class PackMetadataInGitSubmoduleSources(PackMetadataSourcesField): | ||
| required = True | ||
|
|
||
| def validate_resolved_files(self, files: Sequence[str]) -> None: | ||
| if not files: | ||
| raise UnmatchedGlobsError( | ||
| # see: st2tests.fixturesloader.GIT_SUBMODULES_NOT_CHECKED_OUT_ERROR | ||
| "One or more git submodules is not checked out. Make sure to run " | ||
| '"git submodule update --init --recursive"' | ||
| "in the repository root directory to check out all the submodules." | ||
| ) | ||
| super().validate_resolved_files(files) | ||
|
|
||
|
|
||
| class PackMetadata(ResourcesGeneratorTarget): | ||
| alias = "pack_metadata" | ||
| core_fields = (*COMMON_TARGET_FIELDS, Dependencies, PackMetadataSourcesField) | ||
| help = ( | ||
| "Loose pack metadata files.\n\n" | ||
| "Pack metadata includes top-level files (pack.yaml, <pack>.yaml.examle, " | ||
|
cognifloyd marked this conversation as resolved.
Outdated
|
||
| "config.schema.yaml, and icon.png) and metadata for actions, " | ||
| "action-aliases, policies, rules, and sensors." | ||
| ) | ||
|
|
||
|
|
||
| class PackMetadataInGitSubmodule(PackMetadata): | ||
| alias = "pack_metadata_in_git_submodule" | ||
| core_fields = ( | ||
| *COMMON_TARGET_FIELDS, | ||
| Dependencies, | ||
| PackMetadataInGitSubmoduleSources, | ||
| ) | ||
| help = PackMetadata.help + ( | ||
| "\npack_metadata_in_git_submodule variant errors if the sources field " | ||
| "has unmatched globs. It prints instructions on how to checkout git " | ||
| "submodules." | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| # Copyright 2023 The StackStorm Authors. | ||
| # | ||
| # Licensed under the Apache License, Version 2.0 (the "License"); | ||
| # you may not use this file except in compliance with the License. | ||
| # You may obtain a copy of the License at | ||
| # | ||
| # http://www.apache.org/licenses/LICENSE-2.0 | ||
| # | ||
| # Unless required by applicable law or agreed to in writing, software | ||
| # distributed under the License is distributed on an "AS IS" BASIS, | ||
| # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| # See the License for the specific language governing permissions and | ||
| # limitations under the License. | ||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
|
|
||
| from pants.engine.addresses import Address | ||
| from pants.engine.internals.scheduler import ExecutionError | ||
| from pants.testutil.rule_runner import RuleRunner | ||
|
|
||
| from .target_types import ( | ||
| PackMetadata, | ||
| # PackMetadataSourcesField, | ||
| PackMetadataInGitSubmodule, | ||
| # PackMetadataInGitSubmoduleSources, | ||
| UnmatchedGlobsError, | ||
| ) | ||
|
|
||
|
|
||
| @pytest.fixture | ||
| def rule_runner() -> RuleRunner: | ||
| return RuleRunner( | ||
| rules=[], | ||
| target_types=[PackMetadata, PackMetadataInGitSubmodule], | ||
| ) | ||
|
|
||
|
|
||
| GIT_SUBMODULE_BUILD_FILE = """ | ||
| pack_metadata_in_git_submodule( | ||
| name="metadata", | ||
| sources=["./submodule_dir/pack.yaml"], | ||
| ) | ||
| """ | ||
|
|
||
|
|
||
| def test_git_submodule_sources_missing(rule_runner: RuleRunner) -> None: | ||
| rule_runner.write_files( | ||
| { | ||
| "packs/BUILD": GIT_SUBMODULE_BUILD_FILE, | ||
| } | ||
| ) | ||
| with pytest.raises(ExecutionError) as e: | ||
| _ = rule_runner.get_target(Address("packs", target_name="metadata")) | ||
| exc = e.value.wrapped_exceptions[0] | ||
| assert isinstance(exc, UnmatchedGlobsError) | ||
| assert "One or more git submodules is not checked out" in str(exc) | ||
|
|
||
|
|
||
| def test_git_submodule_sources_present(rule_runner: RuleRunner) -> None: | ||
| rule_runner.write_files( | ||
| { | ||
| "packs/BUILD": GIT_SUBMODULE_BUILD_FILE, | ||
| "packs/submodule_dir/pack.yaml": "---\nname: foobar\n", | ||
| } | ||
| ) | ||
| # basically: this asserts that it does not raise UnmatchedGlobsError | ||
| _ = rule_runner.get_target(Address("packs", target_name="metadata")) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.