Skip to content

Commit ed16034

Browse files
committed
Do not create a PR if one exists
Signed-off-by: Pablo Galindo <[email protected]>
1 parent a6e2f0c commit ed16034

File tree

2 files changed

+20
-3
lines changed

2 files changed

+20
-3
lines changed

buildbothammer/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,4 +60,4 @@ The script will:
6060
- `REPO_OWNER`: The owner of the main repository (default: "python")
6161
- `REPO_NAME`: The name of the repository (default: "cpython")
6262
- `FORK_OWNER`: Your GitHub username (default: "$REPO_OWNER")
63-
- `REPO_CLONE_DIR`: The directory for the local clone of the repository
63+
- `REPO_CLONE_PATH`: The directory for the local clone of the repository

buildbothammer/src/buildbothammer/__init__.py

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,31 @@ def ensure_repo_clone():
197197
run_command(["git", "reset", "--hard", "origin/main"], cwd=str(REPO_CLONE_DIR))
198198

199199

200+
def check_existing_pr(repo, branch_name):
201+
logger.info(f"Checking for existing PR for branch: {branch_name}")
202+
existing_prs = repo.get_pulls(state='open', head=f"{FORK_OWNER}:{branch_name}")
203+
return next(existing_prs, None)
204+
205+
200206
def create_revert_pr(commit_sha, builder, failing_build):
201207
logger.info(f"Creating revert PR for commit: {commit_sha}")
202208
g = Github(GITHUB_TOKEN)
203209

204210
try:
205211
main_repo = g.get_repo(f"{REPO_OWNER}/{REPO_NAME}")
206212

213+
branch_name = f"revert-{commit_sha[:7]}"
214+
215+
# Check for existing PR
216+
existing_pr = check_existing_pr(main_repo, branch_name)
217+
if existing_pr:
218+
logger.info(f"Existing PR found: {existing_pr.html_url}")
219+
return None, None
220+
221+
with FileLock(LOCK_FILE):
222+
ensure_repo_clone()
223+
224+
207225
with FileLock(LOCK_FILE):
208226
ensure_repo_clone()
209227

@@ -215,7 +233,6 @@ def create_revert_pr(commit_sha, builder, failing_build):
215233
cwd=str(REPO_CLONE_DIR),
216234
)
217235

218-
branch_name = f"revert-{commit_sha[:7]}"
219236
run_command(["git", "checkout", "-b", branch_name], cwd=str(REPO_CLONE_DIR))
220237
logger.info(f"Created and checked out new branch: {branch_name}")
221238

@@ -322,7 +339,7 @@ async def process_builder(session, builder, first_failing_build):
322339
pr_url, discord_message = create_revert_pr(
323340
commit_sha, builder, first_failing_build
324341
)
325-
if pr_url:
342+
if pr_url and discord_message:
326343
logger.info(f"Created revert PR for commit {commit_sha}: {pr_url}")
327344
await send_discord_notification(session, discord_message)
328345
else:

0 commit comments

Comments
 (0)