From 003f324d6913812155da987036f559de3e5d96a4 Mon Sep 17 00:00:00 2001 From: Pablo Galindo Date: Thu, 10 Oct 2024 23:31:51 +0100 Subject: [PATCH] Do not create a PR if one exists --- buildbothammer/src/buildbothammer/__init__.py | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/buildbothammer/src/buildbothammer/__init__.py b/buildbothammer/src/buildbothammer/__init__.py index 508d9f6a..2ef65277 100644 --- a/buildbothammer/src/buildbothammer/__init__.py +++ b/buildbothammer/src/buildbothammer/__init__.py @@ -197,6 +197,12 @@ def ensure_repo_clone(): run_command(["git", "reset", "--hard", "origin/main"], cwd=str(REPO_CLONE_DIR)) +def check_existing_pr(repo, branch_name): + logger.info(f"Checking for existing PR for branch: {branch_name}") + existing_prs = repo.get_pulls(state='open', head=f"{FORK_OWNER}:{branch_name}") + return next(existing_prs, None) + + def create_revert_pr(commit_sha, builder, failing_build): logger.info(f"Creating revert PR for commit: {commit_sha}") g = Github(GITHUB_TOKEN) @@ -204,6 +210,18 @@ def create_revert_pr(commit_sha, builder, failing_build): try: main_repo = g.get_repo(f"{REPO_OWNER}/{REPO_NAME}") + branch_name = f"revert-{commit_sha[:7]}" + + # Check for existing PR + existing_pr = check_existing_pr(main_repo, branch_name) + if existing_pr: + logger.info(f"Existing PR found: {existing_pr.html_url}") + return None, None + + with FileLock(LOCK_FILE): + ensure_repo_clone() + + with FileLock(LOCK_FILE): ensure_repo_clone() @@ -215,7 +233,6 @@ def create_revert_pr(commit_sha, builder, failing_build): cwd=str(REPO_CLONE_DIR), ) - branch_name = f"revert-{commit_sha[:7]}" run_command(["git", "checkout", "-b", branch_name], cwd=str(REPO_CLONE_DIR)) logger.info(f"Created and checked out new branch: {branch_name}") @@ -322,7 +339,7 @@ async def process_builder(session, builder, first_failing_build): pr_url, discord_message = create_revert_pr( commit_sha, builder, first_failing_build ) - if pr_url: + if pr_url and discord_message: logger.info(f"Created revert PR for commit {commit_sha}: {pr_url}") await send_discord_notification(session, discord_message) else: