From 26fb84357d741f6f9c5681613f9a6532d49959b3 Mon Sep 17 00:00:00 2001 From: Jennifer Power Date: Tue, 30 Apr 2024 18:20:55 -0400 Subject: [PATCH] refactor: organizes arguments by groups As the arguments list grows, grouping will make understanding how to use and when to set much clearer Signed-off-by: Jennifer Power --- trestlebot/entrypoints/entrypoint_base.py | 118 ++++++++++++---------- 1 file changed, 63 insertions(+), 55 deletions(-) diff --git a/trestlebot/entrypoints/entrypoint_base.py b/trestlebot/entrypoints/entrypoint_base.py index c9b3cce4..360190e0 100644 --- a/trestlebot/entrypoints/entrypoint_base.py +++ b/trestlebot/entrypoints/entrypoint_base.py @@ -19,14 +19,10 @@ from trestlebot import const from trestlebot.bot import TrestleBot -from trestlebot.github import GitHub, GitHubActionsResultsReporter, is_github_actions -from trestlebot.gitlab import ( - GitLab, - GitLabCIResultsReporter, - get_gitlab_root_url, - is_gitlab_ci, -) +from trestlebot.github import GitHubActionsResultsReporter, is_github_actions +from trestlebot.gitlab import GitLabCIResultsReporter, is_gitlab_ci from trestlebot.provider import GitProvider +from trestlebot.provider_factory import GitProviderFactory from trestlebot.reporter import BotResults, ResultsReporter from trestlebot.tasks.base_task import TaskBase @@ -50,12 +46,6 @@ def setup_common_arguments(self) -> None: action="count", default=0, ) - self.parser.add_argument( - "--branch", - type=str, - required=True, - help="Branch name to push changes to", - ) self.parser.add_argument( "--working-dir", type=str, @@ -64,64 +54,84 @@ def setup_common_arguments(self) -> None: help="Working directory wit git repository", ) self.parser.add_argument( - "--file-patterns", + "--dry-run", required=False, - type=str, - default=".", - help="Comma-separated list of file patterns to be used with `git add` in repository updates", + action="store_true", + help="Run tasks, but do not push to the repository", ) - self.parser.add_argument( - "--commit-message", - type=str, - required=False, - default="chore: automatic updates", - help="Commit message for automated updates", + self._set_required_git_args() + self._set_optional_git_args() + self._set_git_provider_args() + + def _set_required_git_args(self) -> None: + """Create an argument group for required git-related configuration.""" + required_git_arg_group = self.parser.add_argument_group( + "required git configuration" ) - self.parser.add_argument( - "--pull-request-title", + required_git_arg_group.add_argument( + "--branch", type=str, - required=False, - default="Automatic updates from trestlebot", - help="Customized title for submitted pull requests", + required=True, + help="Branch name to push changes to", ) - self.parser.add_argument( + required_git_arg_group.add_argument( "--committer-name", type=str, required=True, help="Name of committer", ) - self.parser.add_argument( + required_git_arg_group.add_argument( "--committer-email", type=str, required=True, help="Email for committer", ) - self.parser.add_argument( + + def _set_optional_git_args(self) -> None: + """Create an argument group for optional git-related configuration.""" + optional_git_arg_group = self.parser.add_argument_group( + "optional git configuration" + ) + optional_git_arg_group.add_argument( + "--file-patterns", + required=False, + type=str, + default=".", + help="Comma-separated list of file patterns to be used with `git add` in repository updates", + ) + optional_git_arg_group.add_argument( + "--commit-message", + type=str, + required=False, + default="chore: automatic updates", + help="Commit message for automated updates", + ) + optional_git_arg_group.add_argument( "--author-name", required=False, type=str, help="Name for commit author if differs from committer", ) - self.parser.add_argument( + optional_git_arg_group.add_argument( "--author-email", required=False, type=str, help="Email for commit author if differs from committer", ) - self.parser.add_argument( - "--dry-run", - required=False, - action="store_true", - help="Run tasks, but do not push to the repository", + + def _set_git_provider_args(self) -> None: + """Create an argument group for optional git-provider configuration.""" + git_provider_arg_group = self.parser.add_argument_group( + "git provider configuration" ) - self.parser.add_argument( + git_provider_arg_group.add_argument( "--target-branch", type=str, required=False, help="Target branch or base branch to create a pull request against. \ No pull request is created if unset", ) - self.parser.add_argument( + git_provider_arg_group.add_argument( "--with-token", nargs="?", type=argparse.FileType("r"), @@ -130,6 +140,13 @@ def setup_common_arguments(self) -> None: help="Read token from standard input for authenticated requests with \ Git provider (e.g. create pull requests)", ) + git_provider_arg_group.add_argument( + "--pull-request-title", + type=str, + required=False, + default="Automatic updates from trestlebot", + help="Customized title for submitted pull requests", + ) @staticmethod def set_git_provider(args: argparse.Namespace) -> Optional[GitProvider]: @@ -141,22 +158,13 @@ def set_git_provider(args: argparse.Namespace) -> Optional[GitProvider]: "--with-token", "with-token flag must be set when using target-branch", ) - - if is_github_actions(): - git_provider = GitHub(access_token=args.with_token.read().strip()) - elif is_gitlab_ci(): - server_api_url = get_gitlab_root_url() - git_provider = GitLab( - api_token=args.with_token.read().strip(), server_url=server_api_url - ) - else: - raise EntrypointInvalidArgException( - "--target-branch", - ( - "target-branch flag is set with an unset git provider. " - "To test locally, set the GITHUB_ACTIONS or GITLAB_CI environment variable." - ), - ) + access_token = args.with_token.read().strip() + try: + git_provider = GitProviderFactory.provider_factory(access_token) + except ValueError as e: + raise EntrypointInvalidArgException("--server-url", str(e)) + except RuntimeError as e: + raise EntrypointInvalidArgException("--target-branch", str(e)) from e return git_provider @staticmethod