From 2453cb48b7994b4bf7a3cb0f92ac4e1384bbb8c8 Mon Sep 17 00:00:00 2001 From: bbhtt Date: Sun, 8 Jun 2025 07:15:36 +0530 Subject: [PATCH] merge: Use regex to extract everything --- .github/actions/merge/entrypoint.py | 45 +++++++++++++++++++---------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/.github/actions/merge/entrypoint.py b/.github/actions/merge/entrypoint.py index 0568ba1..d8abe2d 100755 --- a/.github/actions/merge/entrypoint.py +++ b/.github/actions/merge/entrypoint.py @@ -143,12 +143,33 @@ def main(): print("The comment does not start with '/merge'") sys.exit(0) - command_re = re.search("^/merge.*", github_comment, re.M) - if not command_re: - print("The comment doesn't contain '/merge' command") - sys.exit(0) + command_pattern = re.compile(r"^/merge(?::([\w.-]+))?(.*)$") + matched = command_pattern.search(github_comment) + if not matched: + print( + "The comment is not a valid '/merge' command.\n" + "Format: '/merge: " + "'" + ) + sys.exit(1) + + branch_match = matched.group(1) or "master" + if branch_match in ("master", "beta"): + target_repo_default_branch = branch_match else: - command = command_re.group() + target_repo_default_branch = f"branch/{branch_match}" + + print(f"Got target branch {target_repo_default_branch} from comment") + + rest_comment = matched.group(2) + + # https://docs.github.com/en/enterprise-cloud@latest/admin/managing-iam/iam-configuration-reference/username-considerations-for-external-authentication#about-username-normalization + # > Usernames for user accounts on GitHub can only contain alphanumeric characters and dashes + # > If the username is longer than 39 characters (including underscore and short code), + # > the provisioning attempt will fail with a 400 error. + additional_colbs = [m[1:] for m in re.findall(r"@[a-zA-Z0-9-]{1,39}", rest_comment)] + + print(f"Got additional collaborators {additional_colbs} from comment") gh = github.Github(github_token) org = gh.get_organization("flathub") @@ -212,15 +233,10 @@ def main(): "flathub", f"https://x-access-token:{github_token}@github.com/flathub/{appid}" ) - try: - remote_branch = command.split()[0].split(":")[1] - if remote_branch != "beta": - remote_branch = f"branch/{remote_branch}" - except IndexError: - remote_branch = "master" - print("Pushing changes to the new Flathub repo") - git_push = f"cd {tmpdir.name} && git push flathub {pr_branch}:{remote_branch}" + git_push = ( + f"cd {tmpdir.name} && git push flathub {pr_branch}:{target_repo_default_branch}" + ) ret = subprocess.run( git_push, shell=True, @@ -248,8 +264,7 @@ def main(): kde_maintainers = org.get_team_by_slug("KDE") kde_maintainers.update_team_repository(repo, "push") - collaborators = {user.replace("@", "") for user in command.split()[1:]} - for user in collaborators: + for user in additional_colbs: try: print(f"adding {user} to collaborators") repo.add_to_collaborators(user, permission="push")