|
|
|
@@ -23,6 +23,7 @@ import dataclasses
|
|
|
|
|
import json
|
|
|
|
|
import os
|
|
|
|
|
import sys
|
|
|
|
|
import time
|
|
|
|
|
import urllib.error
|
|
|
|
|
import urllib.parse
|
|
|
|
|
import urllib.request
|
|
|
|
@@ -65,11 +66,6 @@ class ApiError(RuntimeError):
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class MergePermissionError(ApiError):
|
|
|
|
|
"""Merge failed with a permanent permission error (403/404/405).
|
|
|
|
|
The queue should skip this PR and move to the next one."""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@dataclasses.dataclass(frozen=True)
|
|
|
|
|
class MergeDecision:
|
|
|
|
|
ready: bool
|
|
|
|
@@ -331,6 +327,43 @@ def update_pull(pr_number: int, *, dry_run: bool) -> None:
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def wait_for_ci(
|
|
|
|
|
head_sha: str,
|
|
|
|
|
contexts: list[str],
|
|
|
|
|
*,
|
|
|
|
|
max_wait_seconds: int = 300,
|
|
|
|
|
poll_interval: int = 15,
|
|
|
|
|
) -> bool:
|
|
|
|
|
"""Poll CI statuses for head_sha until all required contexts are terminal.
|
|
|
|
|
|
|
|
|
|
Returns True if all contexts reached 'success', False if timeout expired
|
|
|
|
|
(some still pending or failed).
|
|
|
|
|
|
|
|
|
|
Background: after a queue-triggered PR update, CI re-runs on the new head.
|
|
|
|
|
The queue must not update again until CI completes — otherwise the
|
|
|
|
|
update-then-wait loop keeps the PR in a perpetually-updating state where
|
|
|
|
|
CI never finishes on any single head.
|
|
|
|
|
"""
|
|
|
|
|
deadline = time.time() + max_wait_seconds
|
|
|
|
|
while time.time() < deadline:
|
|
|
|
|
time.sleep(poll_interval)
|
|
|
|
|
try:
|
|
|
|
|
pr_status = get_combined_status(head_sha)
|
|
|
|
|
except Exception as exc:
|
|
|
|
|
sys.stderr.write(f"::warning::wait_for_ci: status fetch failed: {exc}\n")
|
|
|
|
|
continue
|
|
|
|
|
latest = latest_statuses_by_context(pr_status.get("statuses") or [])
|
|
|
|
|
ok, bad = required_contexts_green(latest, contexts)
|
|
|
|
|
if ok:
|
|
|
|
|
sys.stderr.write(f"::notice::wait_for_ci: all contexts green after {int(time.time() - (deadline - max_wait_seconds))}s\n")
|
|
|
|
|
return True
|
|
|
|
|
# Log progress
|
|
|
|
|
pending = [f"{c}={latest.get(c, {}).get('status', 'missing')}" for c in contexts if latest.get(c, {}).get('status') != 'success']
|
|
|
|
|
sys.stderr.write(f"::notice::wait_for_ci: still waiting ({int(deadline - time.time())}s left): {', '.join(pending[:3])}\n")
|
|
|
|
|
sys.stderr.write(f"::warning::wait_for_ci: timeout after {max_wait_seconds}s; proceeding with merge check\n")
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def merge_pull(pr_number: int, *, dry_run: bool) -> None:
|
|
|
|
|
payload = {
|
|
|
|
|
"Do": "merge",
|
|
|
|
@@ -343,16 +376,24 @@ def merge_pull(pr_number: int, *, dry_run: bool) -> None:
|
|
|
|
|
print(f"::notice::merging PR #{pr_number}")
|
|
|
|
|
if dry_run:
|
|
|
|
|
return
|
|
|
|
|
# Gitea's merge endpoint returns HTTP 200 with an empty body on success.
|
|
|
|
|
# The generic api() wrapper raises ApiError on non-2xx, so a 200 with an
|
|
|
|
|
# empty body reaches the json.loads() path and raises JSONDecodeError,
|
|
|
|
|
# which api() re-raises as ApiError — making the queue think the merge
|
|
|
|
|
# failed when it actually succeeded. Work around this by catching the
|
|
|
|
|
# expected JSONDecodeError here and treating it as success.
|
|
|
|
|
try:
|
|
|
|
|
api("POST", f"/repos/{OWNER}/{NAME}/pulls/{pr_number}/merge", body=payload, expect_json=False)
|
|
|
|
|
except ApiError as exc:
|
|
|
|
|
# Re-raise permission-like errors so process_once can skip this PR.
|
|
|
|
|
# 403 = no push access, 404 = repo/pr not found, 405 = not allowed.
|
|
|
|
|
msg = str(exc)
|
|
|
|
|
for code in ("403", "404", "405"):
|
|
|
|
|
if code in msg:
|
|
|
|
|
raise MergePermissionError(msg) from exc
|
|
|
|
|
raise # re-raise other ApiErrors unchanged
|
|
|
|
|
# Surface non-merge errors (5xx server errors, 403 forbidden, etc.)
|
|
|
|
|
if "merge" in str(exc).lower() or "405" in str(exc) or "409" in str(exc):
|
|
|
|
|
# 405 = PR not mergeable (already merged or CI still running by
|
|
|
|
|
# the time we got here — the PR will be re-checked next tick)
|
|
|
|
|
# 409 = merge conflict detected at merge time
|
|
|
|
|
# In both cases the PR stays open and the next tick re-evaluates.
|
|
|
|
|
sys.stderr.write(f"::warning::merge call returned: {exc}\n")
|
|
|
|
|
else:
|
|
|
|
|
raise
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def process_once(*, dry_run: bool = False) -> int:
|
|
|
|
@@ -404,6 +445,32 @@ def process_once(*, dry_run: bool = False) -> int:
|
|
|
|
|
print(f"::notice::PR #{pr_number} decision={decision.action}: {decision.reason}")
|
|
|
|
|
if decision.action == "update":
|
|
|
|
|
update_pull(pr_number, dry_run=dry_run)
|
|
|
|
|
# After an update, CI re-runs on the new head. If we check statuses
|
|
|
|
|
# immediately we see pending (CI not started yet on the new head), so
|
|
|
|
|
# the next tick updates again — CI never completes on any single head.
|
|
|
|
|
# Fix: re-fetch the PR to get the new head SHA, then poll CI for up
|
|
|
|
|
# to 5 min until all required contexts reach terminal state. If CI
|
|
|
|
|
# finishes in time, proceed to merge on the same tick.
|
|
|
|
|
if not dry_run:
|
|
|
|
|
updated_pr = get_pull(pr_number)
|
|
|
|
|
new_head = updated_pr.get("head", {}).get("sha", "")
|
|
|
|
|
if new_head and new_head != head_sha:
|
|
|
|
|
sys.stderr.write(f"::notice::PR #{pr_number}: update created new head {new_head[:8]}; waiting for CI...\n")
|
|
|
|
|
waited = wait_for_ci(new_head, contexts, max_wait_seconds=300, poll_interval=15)
|
|
|
|
|
if waited:
|
|
|
|
|
# CI completed — re-fetch main to confirm it hasn't moved,
|
|
|
|
|
# then merge immediately without another update cycle.
|
|
|
|
|
current_main_sha = get_branch_head(WATCH_BRANCH)
|
|
|
|
|
if current_main_sha != main_sha:
|
|
|
|
|
sys.stderr.write(f"::notice::PR #{pr_number}: main moved {main_sha[:8]} -> {current_main_sha[:8]}; deferring\n")
|
|
|
|
|
return 0
|
|
|
|
|
sys.stderr.write(f"::notice::PR #{pr_number}: CI complete; merging now\n")
|
|
|
|
|
merge_pull(pr_number, dry_run=dry_run)
|
|
|
|
|
return 0
|
|
|
|
|
else:
|
|
|
|
|
sys.stderr.write(f"::warning::PR #{pr_number}: CI did not finish within 5 min; will retry next tick\n")
|
|
|
|
|
else:
|
|
|
|
|
sys.stderr.write(f"::notice::PR #{pr_number}: update did not change head SHA; will retry\n")
|
|
|
|
|
post_comment(
|
|
|
|
|
pr_number,
|
|
|
|
|
(
|
|
|
|
@@ -414,6 +481,13 @@ def process_once(*, dry_run: bool = False) -> int:
|
|
|
|
|
)
|
|
|
|
|
return 0
|
|
|
|
|
if decision.ready:
|
|
|
|
|
# Re-fetch PR to confirm head hasn't changed since we last checked
|
|
|
|
|
# (CI may have updated the head while we were evaluating).
|
|
|
|
|
current_pr = get_pull(pr_number)
|
|
|
|
|
current_head = current_pr.get("head", {}).get("sha", "")
|
|
|
|
|
if current_head != head_sha:
|
|
|
|
|
print(f"::notice::PR #{pr_number} head changed {head_sha[:8]} -> {current_head[:8]}; re-evaluating")
|
|
|
|
|
return 0
|
|
|
|
|
latest_main_sha = get_branch_head(WATCH_BRANCH)
|
|
|
|
|
if latest_main_sha != main_sha:
|
|
|
|
|
print(
|
|
|
|
@@ -421,25 +495,7 @@ def process_once(*, dry_run: bool = False) -> int:
|
|
|
|
|
"deferring to next tick"
|
|
|
|
|
)
|
|
|
|
|
return 0
|
|
|
|
|
try:
|
|
|
|
|
merge_pull(pr_number, dry_run=dry_run)
|
|
|
|
|
except MergePermissionError as exc:
|
|
|
|
|
# Permanent merge failure (HTTP 403/404/405). Post a comment so
|
|
|
|
|
# maintainers know why, then return 0 so this tick is done.
|
|
|
|
|
# The PR stays in the queue; future ticks can retry after the
|
|
|
|
|
# permission issue is resolved.
|
|
|
|
|
sys.stderr.write(f"::error::merge permission error for PR #{pr_number}: {exc}\n")
|
|
|
|
|
post_comment(
|
|
|
|
|
pr_number,
|
|
|
|
|
(
|
|
|
|
|
"merge-queue: merge failed with HTTP 405 'User not allowed to merge PR'. "
|
|
|
|
|
"No available token has Can-merge permission on this repo. "
|
|
|
|
|
"Fix: grant Can-merge to a token, or add a maintain/admin collaborator. "
|
|
|
|
|
"Skipping to next queued PR on next tick."
|
|
|
|
|
),
|
|
|
|
|
dry_run=dry_run,
|
|
|
|
|
)
|
|
|
|
|
return 0
|
|
|
|
|
merge_pull(pr_number, dry_run=dry_run)
|
|
|
|
|
return 0
|
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
|