From ec29a855cd9a6c2430b242919bdafeb533e62102 Mon Sep 17 00:00:00 2001 From: Mehdi ABAAKOUK Date: Fri, 1 Nov 2024 11:13:38 +0100 Subject: [PATCH] chore: remove argparse usage from stack module Change-Id: Ic4dee15f244a08330482808942e0e0d9a9901dc6 --- mergify_cli/cli.py | 15 +++++++++++---- mergify_cli/stack/edit.py | 3 +-- .../stack/github_action_auto_rebase.py | 19 +++++++------------ mergify_cli/stack/setup.py | 7 +------ 4 files changed, 20 insertions(+), 24 deletions(-) diff --git a/mergify_cli/cli.py b/mergify_cli/cli.py index 4fc4207..b63484b 100644 --- a/mergify_cli/cli.py +++ b/mergify_cli/cli.py @@ -93,7 +93,7 @@ async def get_default_token() -> str: async def _stack_push(args: argparse.Namespace) -> None: if args.setup: # backward compat - await setup.stack_setup(args) + await setup.stack_setup() return await push.stack_push( @@ -135,7 +135,7 @@ def register_stack_setup_parser( description="Configure the git hooks", help="Initial installation of the required git commit-msg hook", ) - parser.set_defaults(func=setup.stack_setup) + parser.set_defaults(func=lambda _: setup.stack_setup) def register_stack_edit_parser( @@ -146,7 +146,14 @@ def register_stack_edit_parser( description="Edit the stack history", help="Edit the stack history", ) - parser.set_defaults(func=edit.stack_edit) + parser.set_defaults(func=lambda _: edit.stack_edit) + + +async def _stack_github_action_auto_rebase(args: argparse.Namespace) -> None: + await github_action_auto_rebase.stack_github_action_auto_rebase( + args.github_server, + args.token, + ) def register_stack_github_action_autorebase( @@ -157,7 +164,7 @@ def register_stack_github_action_autorebase( description="Autorebase a pull requests stack", help="Checkout a pull requests stack", ) - parser.set_defaults(func=github_action_auto_rebase.stack_github_action_auto_rebase) + parser.set_defaults(func=_stack_github_action_auto_rebase) async def register_stack_checkout_parser( diff --git a/mergify_cli/stack/edit.py b/mergify_cli/stack/edit.py index 8f929ce..d1e0b00 100644 --- a/mergify_cli/stack/edit.py +++ b/mergify_cli/stack/edit.py @@ -1,10 +1,9 @@ -import argparse import os from mergify_cli import utils -async def stack_edit(_: argparse.Namespace) -> None: +async def stack_edit() -> None: os.chdir(await utils.git("rev-parse", "--show-toplevel")) trunk = await utils.get_trunk() base = await utils.git("merge-base", trunk, "HEAD") diff --git a/mergify_cli/stack/github_action_auto_rebase.py b/mergify_cli/stack/github_action_auto_rebase.py index e4e2d9d..3de33e0 100644 --- a/mergify_cli/stack/github_action_auto_rebase.py +++ b/mergify_cli/stack/github_action_auto_rebase.py @@ -3,7 +3,6 @@ import json import os import sys -import typing import aiofiles @@ -13,11 +12,7 @@ from mergify_cli.stack import push -if typing.TYPE_CHECKING: - import argparse - - -async def stack_github_action_auto_rebase(args: argparse.Namespace) -> None: +async def stack_github_action_auto_rebase(github_server: str, token: str) -> None: for env in ("GITHUB_EVENT_NAME", "GITHUB_EVENT_PATH", "GITHUB_REPOSITORY"): if env not in os.environ: console.log("This action only works in a GitHub Action", style="red") @@ -37,7 +32,7 @@ async def stack_github_action_auto_rebase(args: argparse.Namespace) -> None: ) sys.exit(1) - async with utils.get_github_http_client(args.github_server, args.token) as client: + async with utils.get_github_http_client(github_server, token) as client: await client.post( f"/repos/{user}/{repo}/issues/comments/{event['comment']['id']}/reactions", json={"content": "+1"}, @@ -71,8 +66,8 @@ async def stack_github_action_auto_rebase(args: argparse.Namespace) -> None: await utils.git("branch", "--set-upstream-to", f"origin/{base}") await checkout.stack_checkout( - args.github_server, - args.token, + github_server, + token, user=user, repo=repo, branch_prefix="", @@ -82,8 +77,8 @@ async def stack_github_action_auto_rebase(args: argparse.Namespace) -> None: dry_run=False, ) await push.stack_push( - args.github_server, - args.token, + github_server, + token, skip_rebase=False, next_only=False, branch_prefix="", @@ -95,7 +90,7 @@ async def stack_github_action_auto_rebase(args: argparse.Namespace) -> None: author=author, ) - async with utils.get_github_http_client(args.github_server, args.token) as client: + async with utils.get_github_http_client(github_server, token) as client: body_quote = "> " + "\n> ".join(event["comment"]["body"].split("\n")) await client.post( f"/repos/{user}/{repo}/issues/{pull['number']}/comments", diff --git a/mergify_cli/stack/setup.py b/mergify_cli/stack/setup.py index 0b3f3aa..1ee3fed 100644 --- a/mergify_cli/stack/setup.py +++ b/mergify_cli/stack/setup.py @@ -19,7 +19,6 @@ import pathlib import shutil import sys -import typing import aiofiles @@ -27,11 +26,7 @@ from mergify_cli import utils -if typing.TYPE_CHECKING: - import argparse - - -async def stack_setup(_: argparse.Namespace) -> None: +async def stack_setup() -> None: hooks_dir = pathlib.Path(await utils.git("rev-parse", "--git-path", "hooks")) installed_hook_file = hooks_dir / "commit-msg"