Skip to content

Commit

Permalink
feat(cli): adds rules tranformation entrypoint to infra module
Browse files Browse the repository at this point in the history
Moves cli.py and cli_base.py to entrypoint.py and entrypoint_base.py
Adds rules_transform.py to `infra/entrypoints`
Update CONTRIBUTING.md, pyproject.toml, and __main__.py with new name

Signed-off-by: Jennifer Power <[email protected]>
  • Loading branch information
jpower432 committed Oct 9, 2023
1 parent 8c8a8c6 commit 9bbefbf
Show file tree
Hide file tree
Showing 9 changed files with 119 additions and 39 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

import pytest

from trestlebot.cli import main as cli_main
from trestlebot.entrypoint import main as cli_main


@pytest.fixture
Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions trestlebot/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
9 changes: 7 additions & 2 deletions trestlebot/cli.py → trestlebot/entrypoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
File renamed without changes.
18 changes: 18 additions & 0 deletions trestlebot/infra/entrypoints/__init__.py
Original file line number Diff line number Diff line change
@@ -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.
"""
31 changes: 0 additions & 31 deletions trestlebot/infra/entrypoints/create.py

This file was deleted.

88 changes: 88 additions & 0 deletions trestlebot/infra/entrypoints/rule_transform.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 9bbefbf

Please sign in to comment.