From ed222b0d6b9119880088858adbd2abb0f55b7ff8 Mon Sep 17 00:00:00 2001 From: Mehdi ABAAKOUK Date: Thu, 24 Oct 2024 11:04:25 +0200 Subject: [PATCH 1/2] feat: allow to pass author This is required to find the stacked pull request if the stack is not owned by the owner of the token. This will be leverage to run mergify-cli inside an GitHub action. Change-Id: I1c850e473aa086afe1f30a194e0da99d29c6bfa9 --- mergify_cli/__init__.py | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/mergify_cli/__init__.py b/mergify_cli/__init__.py index 7a87e97..8915c0c 100755 --- a/mergify_cli/__init__.py +++ b/mergify_cli/__init__.py @@ -599,12 +599,9 @@ async def get_remote_changes( user: str, repo: str, stack_prefix: str, + author: str, ) -> RemoteChanges: - r_author, r_repo = await asyncio.gather( - client.get("/user"), - client.get(f"/repos/{user}/{repo}"), - ) - author = r_author.json()["login"] + r_repo = await client.get(f"/repos/{user}/{repo}") repository = r_repo.json() r = await client.get( @@ -649,7 +646,7 @@ async def get_remote_changes( # TODO(charly): fix code to conform to linter (number of arguments, local # variables, statements, positional arguments, branches) -async def stack_push( # noqa: PLR0913, PLR0915, PLR0917 +async def stack_push( # noqa: PLR0912, PLR0913, PLR0915, PLR0917 github_server: str, token: str, skip_rebase: bool, @@ -660,6 +657,7 @@ async def stack_push( # noqa: PLR0913, PLR0915, PLR0917 create_as_draft: bool = False, keep_pull_request_title_and_body: bool = False, only_update_existing_pulls: bool = False, + author: str | None = None, ) -> None: os.chdir(await git("rev-parse", "--show-toplevel")) dest_branch = await git_get_branch_name() @@ -720,8 +718,18 @@ async def stack_push( # noqa: PLR0913, PLR0915, PLR0917 follow_redirects=True, timeout=5.0, ) as client: + if author is None: + r_author = await client.get("/user") + author = r_author.json()["login"] + with console.status("Retrieving latest pushed stacks"): - remote_changes = await get_remote_changes(client, user, repo, stack_prefix) + remote_changes = await get_remote_changes( + client, + user, + repo, + stack_prefix, + author, + ) with console.status("Preparing stacked branches..."): console.log("Stacked pull request plan:", style="green") @@ -877,6 +885,7 @@ async def _stack_push(args: argparse.Namespace) -> None: args.draft, args.keep_pull_request_title_and_body, args.only_update_existing_pulls, + args.author, ) @@ -951,6 +960,11 @@ async def register_stack_push_parser( help="Don't update the title and body of already opened pull requests. " "Default fetched from git config if added with `git config --add mergify-cli.stack-keep-pr-title-body true`", ) + parser.add_argument( + "--author", + help="Set the author of the stack (default: the author of the token)", + ) + parser.add_argument( "--trunk", "-t", From 5fbc7216f3f1fa9d4c8a9498369e28dc9a5ba4e2 Mon Sep 17 00:00:00 2001 From: Mehdi ABAAKOUK Date: Fri, 25 Oct 2024 16:57:07 +0200 Subject: [PATCH 2/2] fix: handle correctly when body is empty GitHub randomly returns None or "" Change-Id: I72dd27ae39fc410fba636296be2117565d1d8d40 --- mergify_cli/__init__.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/mergify_cli/__init__.py b/mergify_cli/__init__.py index 8915c0c..1dbd1c2 100755 --- a/mergify_cli/__init__.py +++ b/mergify_cli/__init__.py @@ -453,11 +453,13 @@ async def create_or_update_stack( # noqa: PLR0913,PLR0917 "base": change.base_branch, } if keep_pull_request_title_and_body: - if change.pull["body"] is None: - msg = "GitHub returned a pull request without body set" - raise RuntimeError(msg) pull_changes.update( - {"body": format_pull_description(change.pull["body"], depends_on)}, + { + "body": format_pull_description( + change.pull["body"] or "", + depends_on, + ), + }, ) else: pull_changes.update(