Skip to content

Commit 7555b96

Browse files
committed
accept PR number and more
1 parent 85aa9c3 commit 7555b96

1 file changed

Lines changed: 52 additions & 5 deletions

File tree

bot.py

Lines changed: 52 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -414,18 +414,65 @@ def main():
414414

415415
elif event_name == "workflow_call":
416416
# Called from orchestrator - update PR summary
417-
# Try to get PR number from context
417+
# Try to get PR number from multiple sources
418418
pr_number = None
419-
if "pull_request" in event:
420-
pr_number = event["pull_request"]["number"]
421-
elif "issue" in event and "pull_request" in event["issue"]:
422-
pr_number = event["issue"]["number"]
419+
420+
# First try from environment variable (set by GitHub Actions workflow)
421+
pr_number_str = os.getenv("GITHUB_PR_NUMBER") or os.getenv("PR_NUMBER")
422+
if pr_number_str:
423+
try:
424+
pr_number = int(pr_number_str)
425+
print(f"ℹ️ Got PR number from environment variable: {pr_number}")
426+
except ValueError:
427+
pass
428+
429+
# Try from event
430+
if not pr_number:
431+
if "pull_request" in event:
432+
pr_number = event["pull_request"]["number"]
433+
elif "issue" in event and "pull_request" in event["issue"]:
434+
pr_number = event["issue"]["number"]
435+
436+
# Try to get PR number from the ref (if it's a PR branch)
437+
if not pr_number:
438+
ref = os.getenv("GITHUB_HEAD_REF") or os.getenv("GITHUB_REF", "")
439+
# If we have a PR context, try to find the PR
440+
if ref and "/" in ref:
441+
try:
442+
# Try to get PRs for this ref
443+
pulls = bot.repo.get_pulls(head=f"{bot.repo.owner.login}:{ref}", state="open")
444+
for pr in pulls:
445+
pr_number = pr.number
446+
break
447+
except Exception as e:
448+
print(f"⚠️ Could not find PR by ref: {e}")
449+
450+
# Last resort: try to get from GitHub context if available
451+
if not pr_number and "GITHUB_EVENT_NAME" in os.environ:
452+
# For pull_request events, the number should be in the context
453+
# But for workflow_call, we need to infer it
454+
# Check if we're in a PR context by looking at the ref
455+
ref = os.getenv("GITHUB_HEAD_REF", "")
456+
base_ref = os.getenv("GITHUB_BASE_REF", "")
457+
if ref and base_ref:
458+
# We're in a PR context, try to find the PR
459+
try:
460+
pulls = list(bot.repo.get_pulls(head=f"{bot.repo.owner.login}:{ref}", base=base_ref, state="open", sort="updated", direction="desc"))
461+
if pulls:
462+
pr_number = pulls[0].number
463+
print(f"ℹ️ Found PR #{pr_number} by ref {ref} -> {base_ref}")
464+
except Exception as e:
465+
print(f"⚠️ Could not find PR by refs: {e}")
423466

424467
if pr_number:
425468
print(f"📊 Updating PR summary for PR #{pr_number} (workflow_call)")
426469
bot.post_pr_summary(pr_number, force_update=True)
427470
else:
428471
print("⚠️ Could not determine PR number from workflow_call event")
472+
print(f" Event keys: {list(event.keys()) if event else 'No event'}")
473+
print(f" GITHUB_HEAD_REF: {os.getenv('GITHUB_HEAD_REF', 'not set')}")
474+
print(f" GITHUB_BASE_REF: {os.getenv('GITHUB_BASE_REF', 'not set')}")
475+
print(f" GITHUB_REF: {os.getenv('GITHUB_REF', 'not set')}")
429476

430477

431478
if __name__ == "__main__":

0 commit comments

Comments
 (0)