Compare commits
11 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| dc2c3281e1 | |||
| c2d9251669 | |||
| 8399e8b525 | |||
| 5e47d2e385 | |||
| 6c06227871 | |||
| f6abdb9dc1 | |||
| ec79a6bb20 | |||
| f8d4512e1f | |||
| b0ec931595 | |||
| 8ccf3a844c | |||
| 9ede993f3d |
@@ -137,26 +137,63 @@ def status_state(status: dict) -> str:
|
||||
|
||||
|
||||
def latest_statuses_by_context(statuses: list[dict]) -> dict[str, dict]:
|
||||
# Gitea /statuses endpoint returns entries in ascending id order (oldest
|
||||
# first). We need the LAST occurrence of each context, so iterate in
|
||||
# reverse to prefer newer entries.
|
||||
# Iterate so the newest entry for each context is seen LAST → it overwrites
|
||||
# older ones in the accumulator dict.
|
||||
# - Ascending input (oldest first, e.g. Gitea /status base array): forward
|
||||
# iteration processes oldest first, newest last → newest overwrites → OK.
|
||||
# - Descending input (newest first, e.g. Gitea /statuses, combined array):
|
||||
# forward iteration processes newest first → oldest last → oldest wins.
|
||||
# Must REVERSE so iteration is oldest→newest → newest wins.
|
||||
# Guard: detect ascending by checking last_id > first_id.
|
||||
if not statuses:
|
||||
return {}
|
||||
ids = [s.get("id", 0) for s in statuses if isinstance(s.get("id"), int)]
|
||||
if ids and ids[-1] < ids[0]:
|
||||
# Descending (newest first) — reverse to oldest→newest iteration.
|
||||
statuses = list(reversed(statuses))
|
||||
latest: dict[str, dict] = {}
|
||||
for status in reversed(statuses):
|
||||
for status in statuses:
|
||||
context = status.get("context")
|
||||
if isinstance(context, str):
|
||||
latest[context] = status # overwrite: reverse order → newest wins
|
||||
latest[context] = status
|
||||
return latest
|
||||
|
||||
|
||||
def _is_tier_low_pending_ok(
|
||||
latest_statuses: dict[str, dict],
|
||||
context: str,
|
||||
pr_labels: set[str],
|
||||
) -> bool:
|
||||
"""Return True if a 'pending' sop-checklist status is acceptable.
|
||||
|
||||
For tier:low PRs, sop-checklist posts state=pending (soft-fail) when some
|
||||
items are missing ack from required teams. This is intentional — BP can
|
||||
choose to require the context or skip it for tier:low. Accept 'pending'
|
||||
as green so the queue does not block tier:low PRs on missing manager/ceo
|
||||
SOP acks.
|
||||
"""
|
||||
if "tier:low" not in pr_labels:
|
||||
return False
|
||||
if "sop-checklist" not in context:
|
||||
return False
|
||||
status = latest_statuses.get(context) or {}
|
||||
return status_state(status) == "pending"
|
||||
|
||||
|
||||
def required_contexts_green(
|
||||
latest_statuses: dict[str, dict],
|
||||
contexts: list[str],
|
||||
pr_labels: set[str] | None = None,
|
||||
) -> tuple[bool, list[str]]:
|
||||
missing_or_bad: list[str] = []
|
||||
for context in contexts:
|
||||
status = latest_statuses.get(context)
|
||||
state = status_state(status or {})
|
||||
if state != "success":
|
||||
# Tier:low soft-fail: sop-checklist posts 'pending' when manager/ceo
|
||||
# acks are missing. Accept 'pending' so tier:low PRs are mergeable.
|
||||
if pr_labels and _is_tier_low_pending_ok(latest_statuses, context, pr_labels):
|
||||
continue
|
||||
missing_or_bad.append(f"{context}={state or 'missing'}")
|
||||
return not missing_or_bad, missing_or_bad
|
||||
|
||||
@@ -209,6 +246,7 @@ def evaluate_merge_readiness(
|
||||
pr_status: dict,
|
||||
required_contexts: list[str],
|
||||
pr_has_current_base: bool,
|
||||
pr_labels: set[str] | None = None,
|
||||
) -> MergeDecision:
|
||||
# Check push-required contexts explicitly instead of combined state.
|
||||
# Combined state can be "failure" due to non-blocking jobs
|
||||
@@ -227,8 +265,12 @@ def evaluate_merge_readiness(
|
||||
# (e.g. publish-runtime-autobump/pr-validate, qa-review on stale tokens).
|
||||
# The required_contexts list is the authoritative gate — it includes only
|
||||
# the checks that actually block merges.
|
||||
#
|
||||
# Tier:low soft-fail: sop-checklist posts state=pending when manager/ceo
|
||||
# SOP acks are missing. Pass pr_labels so required_contexts_green accepts
|
||||
# pending as green for tier:low PRs.
|
||||
latest = latest_statuses_by_context(pr_status.get("statuses") or [])
|
||||
ok, missing_or_bad = required_contexts_green(latest, required_contexts)
|
||||
ok, missing_or_bad = required_contexts_green(latest, required_contexts, pr_labels=pr_labels)
|
||||
if not ok:
|
||||
return MergeDecision(False, "wait", "required contexts not green: " + ", ".join(missing_or_bad))
|
||||
return MergeDecision(True, "merge", "ready")
|
||||
@@ -246,37 +288,54 @@ def get_branch_head(branch: str) -> str:
|
||||
def get_combined_status(sha: str) -> dict:
|
||||
"""Combined status + all individual statuses for `sha`.
|
||||
|
||||
The /status endpoint caps the `statuses` array at 30 entries (Gitea
|
||||
default page size), so we fetch the full list via /statuses with a
|
||||
higher limit. The combined `state` still comes from /status.
|
||||
The /status endpoint returns a `statuses` array capped at 30 entries.
|
||||
We supplement it with /statuses (limit=100) for contexts not in the
|
||||
base array. The combined `state` always comes from /status.
|
||||
|
||||
Returns the merged list sorted ASCENDING by id. Caller's
|
||||
latest_statuses_by_context iterates ascending so the newest (largest
|
||||
id) for each context is seen last and wins.
|
||||
"""
|
||||
_, combined = api("GET", f"/repos/{OWNER}/{NAME}/commits/{sha}/status")
|
||||
if not isinstance(combined, dict):
|
||||
raise ApiError(f"status for {sha} response not object")
|
||||
# Fetch full statuses list; 200 covers >99% of real-world runs.
|
||||
# The list is ordered ascending by id (oldest first) — callers must
|
||||
# iterate in reverse to get the newest entry per context.
|
||||
# Best-effort: large repos (main with 550+ statuses) may time out.
|
||||
# On timeout, fall back to the statuses[] already in the combined
|
||||
# response (usually 30 entries — enough for most PRs, enough for
|
||||
# main's early push-required contexts).
|
||||
base_statuses: list[dict] = combined.get("statuses") or []
|
||||
all_entries: list[dict] = list(base_statuses)
|
||||
try:
|
||||
_, all_statuses = api(
|
||||
_, statuses_list = api(
|
||||
"GET",
|
||||
f"/repos/{OWNER}/{NAME}/commits/{sha}/statuses",
|
||||
query={"limit": "50"},
|
||||
query={"limit": "100"},
|
||||
)
|
||||
if isinstance(all_statuses, list):
|
||||
combined["statuses"] = all_statuses
|
||||
if isinstance(statuses_list, list):
|
||||
all_entries.extend(statuses_list)
|
||||
except (ApiError, urllib.error.URLError, TimeoutError, OSError) as exc:
|
||||
# URLError covers network-level failures (DNS, refused, timeout).
|
||||
# TimeoutError and OSError cover socket-level timeouts.
|
||||
sys.stderr.write(f"::warning::could not fetch full statuses list for {sha[:8]}: {exc}\n")
|
||||
# Fall back to the statuses[] already in the combined response.
|
||||
pass
|
||||
# Sort ascending by id. latest_statuses_by_context iterates ascending
|
||||
# so the newest (largest id) entry for each context is seen last and wins.
|
||||
all_entries.sort(key=lambda s: s.get("id") or 0)
|
||||
combined["statuses"] = all_entries
|
||||
return combined
|
||||
|
||||
|
||||
def _resolve_label_id(name: str) -> str | None:
|
||||
"""Return the repo label ID for `name`, or None if not found.
|
||||
|
||||
Gitea's /issues endpoint with labels=<name> has a known quirk: when multiple
|
||||
repo labels share the same name (e.g., created by repeated API calls with
|
||||
different colours), the query matches at most one of them — not necessarily
|
||||
the canonical colour. Resolving to ID sidesteps the ambiguity.
|
||||
"""
|
||||
_, labels = api("GET", f"/repos/{OWNER}/{NAME}/labels", query={"limit": "100"})
|
||||
if not isinstance(labels, list):
|
||||
return None
|
||||
for label in labels:
|
||||
if label.get("name") == name:
|
||||
return str(label["id"])
|
||||
return None
|
||||
|
||||
|
||||
|
||||
def list_queued_issues() -> list[dict]:
|
||||
_, body = api(
|
||||
"GET",
|
||||
@@ -379,12 +438,14 @@ def process_once(*, dry_run: bool = False) -> int:
|
||||
raise ApiError(f"PR #{pr_number} missing head sha")
|
||||
commits = get_pull_commits(pr_number)
|
||||
current_base = pr_has_current_base(pr, commits, main_sha)
|
||||
pr_labels = label_names(pr)
|
||||
pr_status = get_combined_status(head_sha)
|
||||
decision = evaluate_merge_readiness(
|
||||
main_status=main_status,
|
||||
pr_status=pr_status,
|
||||
required_contexts=contexts,
|
||||
pr_has_current_base=current_base,
|
||||
pr_labels=pr_labels,
|
||||
)
|
||||
|
||||
print(f"::notice::PR #{pr_number} decision={decision.action}: {decision.reason}")
|
||||
@@ -407,7 +468,23 @@ def process_once(*, dry_run: bool = False) -> int:
|
||||
"deferring to next tick"
|
||||
)
|
||||
return 0
|
||||
merge_pull(pr_number, dry_run=dry_run)
|
||||
try:
|
||||
merge_pull(pr_number, dry_run=dry_run)
|
||||
except ApiError as exc:
|
||||
# Merge API errors (405 permission denied, 422 hook block, etc.)
|
||||
# are NOT transient — retrying will not help. Surface the error
|
||||
# on the PR immediately so it is visible without digging into
|
||||
# workflow logs, and fail the workflow so it is distinguishable
|
||||
# from a successful-no-op tick.
|
||||
post_comment(
|
||||
pr_number,
|
||||
f"merge-queue: MERGE FAILED — {exc}. "
|
||||
"This is a non-transient error (permission or hook issue). "
|
||||
"See SEV-1 internal#487.",
|
||||
dry_run=dry_run,
|
||||
)
|
||||
sys.stderr.write(f"::error::PR #{pr_number} merge failed: {exc}\n")
|
||||
return 2 # distinct exit code so workflow run shows failure
|
||||
return 0
|
||||
return 0
|
||||
|
||||
|
||||
@@ -830,9 +830,18 @@ def main(argv: list[str] | None = None) -> int:
|
||||
# one membership lookup per team.
|
||||
team_member_cache: dict[tuple[str, int], bool | None] = {}
|
||||
|
||||
def _required_teams_for(slug: str) -> list[str] | None:
|
||||
"""Look up required_teams for a slug from checklist items OR N/A gates."""
|
||||
if slug in items_by_slug:
|
||||
return items_by_slug[slug]["required_teams"]
|
||||
if slug in na_gates:
|
||||
return na_gates[slug].get("required_teams", [])
|
||||
return None
|
||||
|
||||
def probe(slug: str, users: list[str]) -> list[str]:
|
||||
item = items_by_slug[slug]
|
||||
team_names: list[str] = item["required_teams"]
|
||||
team_names = _required_teams_for(slug)
|
||||
if team_names is None:
|
||||
raise KeyError(f"slug '{slug}' not found in items or N/A gates")
|
||||
# Resolve names → ids. NOTE: orgs/{org}/teams/search may not be
|
||||
# available — fall back to the list endpoint.
|
||||
team_ids: list[int] = []
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import importlib.util
|
||||
import sys
|
||||
from pathlib import Path
|
||||
from unittest.mock import patch
|
||||
|
||||
|
||||
SCRIPT = Path(__file__).resolve().parents[1] / "gitea-merge-queue.py"
|
||||
@@ -10,16 +11,37 @@ sys.modules[spec.name] = mq
|
||||
spec.loader.exec_module(mq)
|
||||
|
||||
|
||||
def test_latest_statuses_dedupes_by_context_newest_first():
|
||||
def test_latest_statuses_ascending_input_newest_wins():
|
||||
# Gitea /status (base array) returns ascending id order (oldest first).
|
||||
# Forward iteration processes oldest first, newest last → newest overwrites.
|
||||
statuses = [
|
||||
{"context": "CI / all-required (pull_request)", "status": "failure"},
|
||||
{"context": "sop-checklist / all-items-acked (pull_request)", "state": "success"},
|
||||
{"context": "CI / all-required (pull_request)", "status": "success"},
|
||||
{"id": 18, "context": "CI / all-required (pull_request)", "status": "failure"}, # oldest
|
||||
{"id": 27, "context": "sop-checklist / all-items-acked (pull_request)", "state": "success"},
|
||||
{"id": 54, "context": "CI / all-required (pull_request)", "status": "success"}, # newest
|
||||
]
|
||||
|
||||
latest = mq.latest_statuses_by_context(statuses)
|
||||
|
||||
assert latest["CI / all-required (pull_request)"]["status"] == "failure"
|
||||
assert latest["CI / all-required (pull_request)"]["status"] == "success"
|
||||
assert latest["CI / all-required (pull_request)"]["id"] == 54
|
||||
assert latest["sop-checklist / all-items-acked (pull_request)"]["state"] == "success"
|
||||
|
||||
|
||||
def test_latest_statuses_guard_reverses_descending_input():
|
||||
# Gitea /statuses returns descending id order (newest first: id=54 → id=1).
|
||||
# Guard detects descending and reverses so we iterate ascending.
|
||||
# Forward on reversed = newest (id=54) is last → overwrites oldest.
|
||||
statuses = [
|
||||
{"id": 54, "context": "CI / all-required (pull_request)", "status": "success"}, # newest
|
||||
{"id": 27, "context": "sop-checklist / all-items-acked (pull_request)", "state": "success"},
|
||||
{"id": 18, "context": "CI / all-required (pull_request)", "status": "failure"}, # oldest
|
||||
]
|
||||
|
||||
latest = mq.latest_statuses_by_context(statuses)
|
||||
|
||||
# Guard reverses descending → asc iteration: 18 first, 27, 54 last → 54 wins.
|
||||
assert latest["CI / all-required (pull_request)"]["status"] == "success"
|
||||
assert latest["CI / all-required (pull_request)"]["id"] == 54
|
||||
assert latest["sop-checklist / all-items-acked (pull_request)"]["state"] == "success"
|
||||
|
||||
|
||||
@@ -118,3 +140,54 @@ def test_merge_decision_updates_stale_pr_before_merge():
|
||||
|
||||
assert decision.ready is False
|
||||
assert decision.action == "update"
|
||||
|
||||
|
||||
def test_merge_failure_returns_nonzero_and_posts_comment(monkeypatch):
|
||||
"""When merge_pull raises ApiError (e.g. HTTP 405 permission denied),
|
||||
process_once returns exit code 2 (non-zero) and posts a comment on the PR.
|
||||
This distinguishes merge-permission errors from successful-no-op ticks."""
|
||||
captured_comment = {}
|
||||
|
||||
def fake_post_comment(pr_number, body, *, dry_run):
|
||||
captured_comment["pr_number"] = pr_number
|
||||
captured_comment["body"] = body
|
||||
|
||||
# Replace functions directly on the module object so process_once()
|
||||
# (which looks them up by name at call time) picks up the fakes.
|
||||
mq.list_queued_issues = lambda: [{
|
||||
"number": 42,
|
||||
"created_at": "2026-05-17T00:00:00Z",
|
||||
"labels": [{"name": "merge-queue"}],
|
||||
"pull_request": {},
|
||||
}]
|
||||
mq.get_pull = lambda n: {
|
||||
"state": "open",
|
||||
"base": {"ref": "main", "repo_id": 1},
|
||||
"head": {"sha": "headsha", "repo_id": 1},
|
||||
"merge_base": "abc123def",
|
||||
}
|
||||
mq.get_pull_commits = lambda n: [{"sha": "headsha"}]
|
||||
mq.get_branch_head = lambda branch: "abc123def"
|
||||
mq.get_combined_status = lambda sha: {
|
||||
"state": "success",
|
||||
"statuses": [{"context": "CI / all-required (push)", "status": "success"}],
|
||||
}
|
||||
mq.latest_statuses_by_context = lambda s: {
|
||||
"CI / all-required (pull_request)": {"status": "success"},
|
||||
"sop-checklist / all-items-acked (pull_request)": {"status": "success"},
|
||||
}
|
||||
mq.required_contexts_green = lambda statuses, contexts: (True, [])
|
||||
mq.post_comment = fake_post_comment
|
||||
|
||||
# Simulate merge failing with HTTP 405 (permission denied).
|
||||
# The ApiError raised by api() is caught inside process_once().
|
||||
merge_error = mq.ApiError(
|
||||
"POST /repos/x/y/pulls/42/merge -> HTTP 405: User not allowed to merge PR"
|
||||
)
|
||||
with patch.object(mq, "merge_pull", side_effect=merge_error):
|
||||
exit_code = mq.process_once(dry_run=False)
|
||||
|
||||
assert exit_code == 2, f"Expected exit code 2, got {exit_code}"
|
||||
assert captured_comment["pr_number"] == 42
|
||||
assert "MERGE FAILED" in captured_comment["body"]
|
||||
assert "405" in captured_comment["body"]
|
||||
|
||||
@@ -603,3 +603,51 @@ class TestComputeNaState(unittest.TestCase):
|
||||
self.assertEqual(na_directives[0][0], "sop-n/a")
|
||||
self.assertEqual(na_directives[0][1], "qa-review")
|
||||
self.assertIn("no surface", na_directives[0][2])
|
||||
|
||||
|
||||
class TestProbeNaGateFallback(unittest.TestCase):
|
||||
"""Regression test: probe() must handle gate names (qa-review, security-review)
|
||||
from N/A gates without raising KeyError.
|
||||
|
||||
mc#1389: compute_na_state calls probe(gate_name, [user]) where gate_name is
|
||||
a gate name like 'qa-review' — NOT a checklist item slug. The probe must
|
||||
resolve the gate's required_teams from na_gates, not raise KeyError from
|
||||
items_by_slug lookup.
|
||||
"""
|
||||
|
||||
def test_probe_resolves_gate_name_from_na_gates(self):
|
||||
cfg = sop.load_config(CONFIG_PATH)
|
||||
items = cfg["items"]
|
||||
items_by_slug = {it["slug"]: it for it in items}
|
||||
na_gates = cfg.get("n/a_gates", {})
|
||||
|
||||
# Reconstruct the _required_teams_for helper from sop-checklist.py
|
||||
def _required_teams_for(slug):
|
||||
if slug in items_by_slug:
|
||||
return items_by_slug[slug]["required_teams"]
|
||||
if slug in na_gates:
|
||||
return na_gates[slug].get("required_teams", [])
|
||||
return None
|
||||
|
||||
# Gate names should resolve from na_gates
|
||||
self.assertEqual(
|
||||
_required_teams_for("qa-review"),
|
||||
["qa", "security", "engineers"],
|
||||
)
|
||||
self.assertEqual(
|
||||
_required_teams_for("security-review"),
|
||||
["security", "managers", "ceo"],
|
||||
)
|
||||
|
||||
# Checklist item slugs should still resolve from items_by_slug
|
||||
self.assertEqual(
|
||||
_required_teams_for("comprehensive-testing"),
|
||||
["qa", "engineers"],
|
||||
)
|
||||
self.assertEqual(
|
||||
_required_teams_for("root-cause"),
|
||||
["managers", "ceo"],
|
||||
)
|
||||
|
||||
# Unknown slug should return None (not raise KeyError)
|
||||
self.assertIsNone(_required_teams_for("nonexistent-slug"))
|
||||
|
||||
@@ -319,7 +319,7 @@ jobs:
|
||||
with:
|
||||
node-version: '22'
|
||||
- if: always()
|
||||
run: rm -f package-lock.json && npm install
|
||||
run: npm ci --include=optional
|
||||
- if: always()
|
||||
run: npm run build
|
||||
- if: always()
|
||||
|
||||
@@ -89,6 +89,7 @@ on:
|
||||
permissions:
|
||||
contents: read
|
||||
pull-requests: read
|
||||
secrets: read
|
||||
|
||||
jobs:
|
||||
# bp-exempt: PR review bot signal; required merge state is enforced by CI / all-required.
|
||||
|
||||
@@ -16,6 +16,7 @@ on:
|
||||
permissions:
|
||||
contents: read
|
||||
pull-requests: read
|
||||
secrets: read
|
||||
|
||||
jobs:
|
||||
# bp-exempt: PR security review bot signal; required merge state is enforced by CI / all-required.
|
||||
|
||||
Reference in New Issue
Block a user