diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index c33c0939..13818460 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -47,7 +47,7 @@ For workflow diagrams, see the [diagrams](./docs/diagrams/) under the `docs` fol #### Code structure -- `cli.py and cli_base.py` - Provides top level logic for entrypoints +- `entrypoint.py and entrypoint_base.py` - Provides top level logic for entrypoints - `entrypoint.sh` - Bash entrypoint script that is used exclusively with the GitHub Action - `provider.py, github.py, and gitlab.py` - Git provider abstract class and concrete implementations - `tasks` - Pre-tasks can be configured before the main git logic is run. Any task that does workspace management should go here. diff --git a/pyproject.toml b/pyproject.toml index 34e8727e..21558769 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -16,7 +16,7 @@ readme = 'README.md' repository = 'https://github.com/RedHatProductSecurity/trestle-bot' [tool.poetry.scripts] -trestle-bot = "trestlebot.cli:main" +trestle-bot = "trestlebot.entrypoint:main" [tool.poetry.dependencies] python = '^3.8.1' diff --git a/tests/trestlebot/test_cli.py b/tests/trestlebot/test_entrypoint.py similarity index 96% rename from tests/trestlebot/test_cli.py rename to tests/trestlebot/test_entrypoint.py index f883891b..1e82fc1c 100644 --- a/tests/trestlebot/test_cli.py +++ b/tests/trestlebot/test_entrypoint.py @@ -23,7 +23,7 @@ import pytest -from trestlebot.cli import main as cli_main +from trestlebot.entrypoint import main as cli_main @pytest.fixture @@ -114,7 +114,7 @@ def test_with_target_branch( # Patch is_github_actions since these tests will be running in # GitHub Actions - with patch("trestlebot.cli_base.is_github_actions") as mock_check: + with patch("trestlebot.entrypoint_base.is_github_actions") as mock_check: mock_check.return_value = False with pytest.raises(SystemExit): diff --git a/trestlebot/__main__.py b/trestlebot/__main__.py index 9c84b787..d41cab89 100644 --- a/trestlebot/__main__.py +++ b/trestlebot/__main__.py @@ -14,13 +14,13 @@ # License for the specific language governing permissions and limitations # under the License. -import trestlebot.cli +import trestlebot.entrypoint def init() -> None: """Initialize trestlebot""" if __name__ == "__main__": - trestlebot.cli.main() + trestlebot.entrypoint.main() init() diff --git a/trestlebot/cli.py b/trestlebot/entrypoint.py similarity index 95% rename from trestlebot/cli.py rename to trestlebot/entrypoint.py index e5d8b194..43534afa 100644 --- a/trestlebot/cli.py +++ b/trestlebot/entrypoint.py @@ -15,7 +15,12 @@ # under the License. -"""This module parses CLI arguments for the Trestle Bot.""" +""" +This module parses the default entrypoint for the Trestle Bot. + +This is the default entrypoint for the Trestle Bot which performs +autosync operations using compliance-trestle. +""" import argparse import logging @@ -25,7 +30,7 @@ import trestle.common.log as log from trestlebot import const -from trestlebot.cli_base import EntrypointBase, comma_sep_to_list +from trestlebot.entrypoint_base import EntrypointBase, comma_sep_to_list from trestlebot.tasks.assemble_task import AssembleTask from trestlebot.tasks.authored import types from trestlebot.tasks.base_task import TaskBase diff --git a/trestlebot/cli_base.py b/trestlebot/entrypoint_base.py similarity index 100% rename from trestlebot/cli_base.py rename to trestlebot/entrypoint_base.py diff --git a/trestlebot/infra/entrypoints/__init__.py b/trestlebot/infra/entrypoints/__init__.py new file mode 100644 index 00000000..c3788c1c --- /dev/null +++ b/trestlebot/infra/entrypoints/__init__.py @@ -0,0 +1,18 @@ +#!/usr/bin/python + +# Copyright 2023 Red Hat, Inc. +# +# 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. +""" +A collection of entrypoints for Trestlebot. +""" diff --git a/trestlebot/infra/entrypoints/create.py b/trestlebot/infra/entrypoints/create.py deleted file mode 100644 index fa2c70f3..00000000 --- a/trestlebot/infra/entrypoints/create.py +++ /dev/null @@ -1,31 +0,0 @@ -# Copyright 2023 Red Hat, Inc. -# -# 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. - -"""Create OSCAL content.""" - -import sys - - -def create_entrypoint(): - """Creates specified OSCAL formatted content.""" - pass - - -def main(): - - return create_entrypoint() - - -if __name__ == '__main__': - sys.exit(main()) \ No newline at end of file diff --git a/trestlebot/infra/entrypoints/rule_transform.py b/trestlebot/infra/entrypoints/rule_transform.py new file mode 100644 index 00000000..4f41e004 --- /dev/null +++ b/trestlebot/infra/entrypoints/rule_transform.py @@ -0,0 +1,88 @@ +# Copyright 2023 Red Hat, Inc. +# +# 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. + +"""Entrypoint for component definition rules transformation.""" + +import argparse +import logging +from typing import List + +import trestle.common.log as log + +from trestlebot.entrypoint_base import EntrypointBase +from trestlebot.tasks.base_task import TaskBase +from trestlebot.tasks.rule_transform_task import RuleTransformTask +from trestlebot.transformers.yaml_transformer import ToRulesYAMLTransformer + + +logger = logging.getLogger(__name__) + + +class RulesTransformEntrypoint(EntrypointBase): + """Entrypoint for the rules transformation operation.""" + + def __init__(self, parser: argparse.ArgumentParser) -> None: + """Initialize.""" + # Setup base arguments + super().__init__(parser) + self.setup_rules_transformation_arguments() + + def setup_rules_transformation_arguments(self) -> None: + """Setup arguments for the rule transformer entrypoint.""" + self.parser.add_argument( + "--rules-view-path", + required=True, + type=str, + help="Path to Trestle markdown files", + ) + self.parser.add_argument( + "--skip-items", + type=str, + required=False, + help="Comma-separated list of glob patterns of the chosen model type to skip when running \ + tasks", + ) + + def run(self, args: argparse.Namespace) -> None: + """Run the rule transform entrypoint.""" + + log.set_log_level_from_args(args=args) + + transformer: ToRulesYAMLTransformer = ToRulesYAMLTransformer() + + rule_transform_task: RuleTransformTask = RuleTransformTask( + args.working_dir, + args.rules_view_path, + transformer, + args.skip_items, + ) + pre_tasks: List[TaskBase] = [rule_transform_task] + + super().run_base(args, pre_tasks) + + +def main() -> None: + """Run the CLI.""" + parser = argparse.ArgumentParser( + description="Rules transformation entrypoint for trestle." + ) + rules_transform = RulesTransformEntrypoint(parser=parser) + + args = parser.parse_args() + + rules_transform.run(args) + + +if __name__ == "__main__": + main()