Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .github/cursor-approve/card.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
OUTCOME_NOT_APPROVED = "not_approved"
OUTCOME_SUPERSEDED = "superseded"
OUTCOME_HUMAN = "needs_human"
OUTCOME_VETOED = "vetoed"
OUTCOME_OWN_PR = "own_pr"
OUTCOME_ERROR = "error"

Expand Down Expand Up @@ -159,6 +160,7 @@ def render_decide(round_no, max_rounds, sha: str, axes: list, decision, outcome:
else:
why = {
OUTCOME_HUMAN: ["the PR is labelled `needs-human-review`"],
OUTCOME_VETOED: ["vetoed: the PR is labelled `skip-cursor-review`"],
OUTCOME_OWN_PR: ["the approver authored this PR"],
OUTCOME_ERROR: ["the approval could not be posted (see the workflow run)"],
}.get(outcome)
Expand Down
6 changes: 6 additions & 0 deletions .github/cursor-approve/tests/test_card.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,12 @@ def test_decide_not_approved_lists_reasons_and_missing_axis(self):
self.assertIn("red on correctness", body)
self.assertIn("no red, at most 0 yellow", body)

def test_decide_vetoed_names_the_skip_label(self):
body = card.render_decide("1", "5", SHA, ["correctness"], decision(correctness="green"), "vetoed", "0", "")
self.assertIn("**Result: ❌ Not approved.**", body)
self.assertIn("- vetoed: the PR is labelled `skip-cursor-review`", body)
self.assertNotIn("no decision was reached", body)

def test_malformed_decision_shows_no_result(self):
body = card.render_decide("1", "5", SHA, ["correctness"], {"axes": "junk"}, "not_approved", "0", "")
self.assertIn("| correctness | ⚠️ no result |", body)
Expand Down
75 changes: 66 additions & 9 deletions .github/cursor-review/auto-approve.py
Original file line number Diff line number Diff line change
Expand Up @@ -866,6 +866,17 @@ def pr_head_base(pr: dict) -> tuple:
return _str_field(pr, "head", "sha"), _str_field(pr, "base", "ref")


def live_labels_readable(pr: dict) -> bool:
"""Whether the PR payload carries a label list `has_label` can fully read.
`has_label` reads a missing or shapeless `labels` — or skips an entry that is
not a dict with a string `name` — as "no such label", which fails OPEN for a
veto label; a caller that must fail closed checks this first."""
labels = pr.get("labels")
return isinstance(labels, list) and all(
isinstance(label, dict) and isinstance(label.get("name"), str) for label in labels
)


def has_label(pr: dict, name: str) -> bool:
# A truthy non-list `labels` (a number, `true`) is not iterable, and the
# TypeError would escape every caller's `except (RuntimeError, ValueError)`.
Expand Down Expand Up @@ -1466,9 +1477,18 @@ def cmd_round_cap(args) -> int:
# base ride in the body's markers (so `dismiss-stale` withdraws it on a push or
# retarget like any other auto-approval), this identity's own earlier approvals
# are withdrawn on every non-approval, and a `needs-human-review` PR is never
# approved. The outcome is written as the step output `outcome` for the card.

EXTERNAL_OUTCOMES = ("approved", "not_approved", "superseded", "needs_human", "own_pr", "error")
# approved. Nor is a PR labelled `skip-cursor-review`: a caller's label-keyed
# concurrency group cannot cancel an in-flight decide when that veto lands
Comment thread
mattmillerai marked this conversation as resolved.
Resolved
# mid-axes, so decide re-reads it live. The outcome is written as the step
# output `outcome` for the card.

EXTERNAL_OUTCOMES = ("approved", "not_approved", "superseded", "needs_human", "vetoed", "own_pr", "error")
# cursor-review.yml's veto label (its gate and concurrency slot hardcode it too).
SKIP_REVIEW_LABEL = "skip-cursor-review"
SKIP_REVIEW_MESSAGE = f"The PR was labelled `{SKIP_REVIEW_LABEL}` — cursor-approve approval withdrawn."
LABELS_UNREADABLE_MESSAGE = (
f"The PR's labels could not be read to rule out `{SKIP_REVIEW_LABEL}` — cursor-approve approval withdrawn."
)
EXTERNAL_AXES = ("business", "design", "correctness", "completeness", "conformance")


Expand Down Expand Up @@ -1519,12 +1539,21 @@ def cmd_approve_external(args) -> int:
try:
pr = read_pr(args.repo, args.pr_number)
live_head, live_base = pr_head_base(pr)
if not live_labels_readable(pr):
# The veto below must be read, not assumed absent.
raise ValueError("the PR payload carries no label list")
human_review = has_label(pr, HUMAN_REVIEW_LABEL)
vetoed = has_label(pr, SKIP_REVIEW_LABEL)
except (RuntimeError, ValueError) as e:
print(f"::error::Could not read the PR state: {e}")
withdraw_own_approvals(args)
return 1
if live_head != args.commit_sha:
# The veto is PR-wide, so it outranks `superseded`: that path withdraws only
# what is stale against the live head, and would leave an approval already
# standing on the live head in place on a vetoed PR.
if vetoed:
outcome, why = "vetoed", f"the PR is labelled `{SKIP_REVIEW_LABEL}`"
elif live_head != args.commit_sha:
outcome, why = "superseded", f"the PR head moved from {args.commit_sha[:7]} to {live_head[:7] or '?'}"
elif live_base != args.base_ref:
# Same head, different base: the axes diffed against a base the PR no
Expand All @@ -1544,6 +1573,8 @@ def cmd_approve_external(args) -> int:
# is stale against the LIVE head and base, so a decide that finishes
# late cannot dismiss an approval that run already posted.
return withdraw_own_approvals(args, STALE_MESSAGE, live_head, live_base)
if outcome == "vetoed":
return withdraw_own_approvals(args, SKIP_REVIEW_MESSAGE)
Comment thread
mattmillerai marked this conversation as resolved.
Resolved
return withdraw_own_approvals(args)

body = render_external_body(args.card_url, args.commit_sha, args.base_ref)
Expand All @@ -1554,7 +1585,11 @@ def cmd_approve_external(args) -> int:
{"commit_id": args.commit_sha, "event": APPROVE, "body": body},
)
)
except RuntimeError as e:
# As cmd_decide: the id is what a late veto or move dismisses by, so a
# `gh` that exits 0 without one is an ambiguous POST, not a success.
if not isinstance(posted, dict) or posted.get("id") is None:
raise ValueError(f"the review POST returned no review id ({type(posted).__name__})")
except (RuntimeError, ValueError) as e:
if "own pull request" in str(e).lower():
set_output("outcome", "own_pr")
emit(f"ℹ️ **cursor-approve: skipped** — the approver authored this PR ({e}).")
Expand All @@ -1568,17 +1603,39 @@ def cmd_approve_external(args) -> int:
pr_now = read_pr(args.repo, args.pr_number)
head_now, base_now = pr_head_base(pr_now)
labelled_now = has_label(pr_now, HUMAN_REVIEW_LABEL)
# An unreadable label list cannot clear the veto: withdraw, but under
# its own cause rather than claiming a label nobody applied.
labels_unreadable = not live_labels_readable(pr_now)
vetoed_now = has_label(pr_now, SKIP_REVIEW_LABEL)
except (RuntimeError, ValueError):
head_now = base_now = None
labelled_now = False
labelled_now = vetoed_now = labels_unreadable = False
moved = head_now != args.commit_sha or base_now != args.base_ref
if moved or labelled_now:
set_output("outcome", "superseded" if moved else "needs_human")
if moved or labelled_now or vetoed_now or labels_unreadable:
# Veto first, as before the POST: it is PR-wide.
if vetoed_now:
outcome, message = "vetoed", SKIP_REVIEW_MESSAGE
elif moved:
outcome, message = "superseded", STALE_MESSAGE
elif labelled_now:
outcome, message = "needs_human", HUMAN_REVIEW_MESSAGE
else:
outcome, message = "error", LABELS_UNREADABLE_MESSAGE
set_output("outcome", outcome)
try:
dismiss(args.repo, args.pr_number, posted["id"], STALE_MESSAGE if moved else HUMAN_REVIEW_MESSAGE)
dismiss(args.repo, args.pr_number, posted["id"], message)
Comment thread
mattmillerai marked this conversation as resolved.
Resolved
Comment thread
mattmillerai marked this conversation as resolved.
Resolved
except RuntimeError as e:
print(f"::error::The PR changed while the approval was posted, and withdrawing it failed: {e}. {DISMISS_PERMISSION_HINT}")
return 1
if outcome in ("vetoed", "error"):
# Not only the review just posted: any other standing approval by
# this identity (an earlier run's, or one posted concurrently) would
# still count toward branch protection on a PR that cannot be cleared.
if withdraw_own_approvals(args, message):
return 1
if outcome == "error":
print(f"::error::{LABELS_UNREADABLE_MESSAGE}")
return 1
emit("ℹ️ **cursor-approve: withdrawn** — the PR changed while the approval was being posted.")
return 0
set_output("outcome", "approved")
Expand Down
133 changes: 128 additions & 5 deletions .github/cursor-review/tests/test_approve_external.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,21 @@


class FakeGitHub:
def __init__(self, head=SHA, labels=(), reviews=(), head_after=None, base="main"):
def __init__(self, head=SHA, labels=(), reviews=(), head_after=None, base="main",
labels_after=None, read_error=False, pr_override=None, pr_after=None, post_reply=None):
self.head, self.labels, self.reviews, self.base = head, list(labels), list(reviews), base
self.head_after = head_after
self.posted, self.dismissed, self.reads = [], [], 0
self.head_after, self.labels_after = head_after, labels_after
self.read_error, self.pr_override = read_error, pr_override
self.pr_after, self.post_reply = pr_after, post_reply
self.posted, self.dismissed, self.dismiss_messages, self.reads = [], [], [], 0

def __call__(self, args, payload=None):
if args[:3] == ["api", "-X", "POST"] and args[3].endswith("/reviews"):
self.posted.append(payload)
return json.dumps({"id": 555})
return json.dumps({"id": 555}) if self.post_reply is None else self.post_reply
if args[:3] == ["api", "-X", "PUT"]:
self.dismissed.append(args[3].split("/")[-2])
self.dismiss_messages.append((payload or {}).get("message"))
return "{}"
if args[:2] == ["api", "graphql"]:
# list_reviews reads through GraphQL (for `lastEditedAt`).
Expand All @@ -40,9 +44,16 @@ def __call__(self, args, payload=None):
return json.dumps({"data": {"repository": {"pullRequest": {"reviews": {
"pageInfo": {"hasNextPage": False, "endCursor": None}, "nodes": nodes}}}}})
self.reads += 1
if self.read_error:
raise RuntimeError("gh api: HTTP 502")
if self.pr_after is not None and self.reads > 1:
return json.dumps(self.pr_after)
if self.pr_override is not None:
return json.dumps(self.pr_override)
head = self.head_after if (self.head_after and self.reads > 1) else self.head
labels = self.labels_after if (self.labels_after is not None and self.reads > 1) else self.labels
return json.dumps({"head": {"sha": head}, "base": {"ref": self.base},
"labels": [{"name": n} for n in self.labels]})
"labels": [{"name": n} for n in labels]})


def prior_approval(sha="c" * 40, rid=42):
Expand Down Expand Up @@ -116,6 +127,118 @@ def test_needs_human_review_label_posts_nothing(self):
self.assertEqual((rc, outcome), (0, "needs_human"))
self.assertEqual(fake.posted, [])

# --- the skip-cursor-review veto, re-read live at decide time ---

def test_skip_label_vetoes_all_green_and_withdraws_prior_approval(self):
fake = FakeGitHub(labels=["bug", aa.SKIP_REVIEW_LABEL], reviews=[prior_approval()])
rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"},
threshold="low", poster="cr-bot[bot]")
self.assertEqual((rc, outcome), (0, "vetoed"))
self.assertEqual(fake.posted, [])
self.assertEqual(fake.dismissed, ["42"])
self.assertEqual(self.resolve_calls, [])

def run_with_event_labels(self, fake, event_labels):
with tempfile.TemporaryDirectory() as tmp:
event = os.path.join(tmp, "event.json")
with open(event, "w", encoding="utf-8") as f:
json.dump({"action": "labeled", "label": {"name": "cursor-review"},
"pull_request": {"number": 1, "head": {"sha": SHA},
"labels": [{"name": n} for n in event_labels]}}, f)
with mock.patch.dict(os.environ, {"GITHUB_EVENT_PATH": event}):
return self.run_cmd(fake, {"correctness": "green", "conformance": "green"})

def test_skip_label_is_read_live_not_from_the_event_payload(self):
# Live and payload labels disagree in both directions; only the live
# read may decide. The run started on `labeled: cursor-review` and the
# veto landed mid-axes: only the live read carries it.
fake = FakeGitHub(labels=["cursor-review", aa.SKIP_REVIEW_LABEL])
self.assertEqual(self.run_with_event_labels(fake, ["cursor-review"]), (0, "vetoed"))
self.assertEqual(fake.posted, [])
# The veto was removed mid-axes: a stale payload must not keep it.
fake = FakeGitHub(labels=["cursor-review"])
self.assertEqual(self.run_with_event_labels(fake, ["cursor-review", aa.SKIP_REVIEW_LABEL]),
(0, "approved"))
self.assertEqual(len(fake.posted), 1)

def test_skip_label_outranks_a_moved_head(self):
# A late decide seeing both a moved head and the veto must withdraw the
# approval already standing on the live head, not only stale ones.
fake = FakeGitHub(head="d" * 40, labels=[aa.SKIP_REVIEW_LABEL],
reviews=[prior_approval(), prior_approval("d" * 40, 77)])
rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"})
self.assertEqual((rc, outcome), (0, "vetoed"))
self.assertEqual(fake.posted, [])
self.assertEqual(sorted(fake.dismissed), ["42", "77"])

def test_malformed_label_entries_fail_closed(self):
# Entries `has_label` would skip cannot rule the veto out.
for labels in (["skip-cursor-review"], [{"name": None}], [{"name": "bug"}, 7]):
fake = FakeGitHub(pr_override={"head": {"sha": SHA}, "base": {"ref": "main"}, "labels": labels})
rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"})
self.assertEqual((rc, outcome), (1, "error"), labels)
self.assertEqual(fake.posted, [], labels)

def test_skip_label_match_is_exact(self):
fake = FakeGitHub(labels=["skip-cursor-review-later", "no-skip-cursor-review"])
rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"})
self.assertEqual((rc, outcome), (0, "approved"))
self.assertEqual(len(fake.posted), 1)

def test_label_read_failure_posts_nothing(self):
fake = FakeGitHub(read_error=True, reviews=[prior_approval()])
rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"})
self.assertEqual((rc, outcome), (1, "error"))
self.assertEqual(fake.posted, [])
self.assertEqual(fake.dismissed, ["42"])

def test_missing_label_list_fails_closed(self):
# A PR payload with no `labels` cannot prove the veto absent.
for labels in (None, "skip-cursor-review", 1):
pr = {"head": {"sha": SHA}, "base": {"ref": "main"}}
if labels is not None:
pr["labels"] = labels
fake = FakeGitHub(pr_override=pr)
rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"})
self.assertEqual((rc, outcome), (1, "error"), labels)
self.assertEqual(fake.posted, [], labels)

def test_skip_label_applied_during_post_withdraws(self):
fake = FakeGitHub(labels_after=[aa.SKIP_REVIEW_LABEL])
rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"},
threshold="low", poster="cr-bot[bot]")
self.assertEqual((rc, outcome), (0, "vetoed"))
self.assertEqual(len(fake.posted), 1)
self.assertEqual(fake.dismissed, ["555"])
self.assertEqual(fake.dismiss_messages, [aa.SKIP_REVIEW_MESSAGE])
self.assertEqual(self.resolve_calls, [])

def test_skip_label_during_post_withdraws_every_own_approval(self):
# The veto is PR-wide: an approval this identity already has standing on
# the same head goes too, not only the one just posted.
fake = FakeGitHub(labels_after=[aa.SKIP_REVIEW_LABEL], head_after="d" * 40,
reviews=[prior_approval(SHA, 77)])
rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"})
self.assertEqual((rc, outcome), (0, "vetoed"))
self.assertEqual(fake.dismissed, ["555", "77"])

def test_unreadable_labels_after_post_withdraws_under_its_own_cause(self):
fake = FakeGitHub(pr_after={"head": {"sha": SHA}, "base": {"ref": "main"}},
reviews=[prior_approval(SHA, 77)])
rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"},
threshold="low", poster="cr-bot[bot]")
self.assertEqual((rc, outcome), (1, "error"))
self.assertEqual(fake.dismissed, ["555", "77"])
self.assertEqual(fake.dismiss_messages, [aa.LABELS_UNREADABLE_MESSAGE] * 2)
self.assertEqual(self.resolve_calls, [])

def test_post_without_a_review_id_withdraws_and_fails(self):
for reply in ("<html>proxy error</html>", "null", "{}"):
fake = FakeGitHub(post_reply=reply, reviews=[prior_approval(SHA, 77)])
rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"})
self.assertEqual((rc, outcome), (1, "error"), reply)
self.assertEqual(fake.dismissed, ["77"], reply)

def test_late_superseded_decide_keeps_an_approval_on_the_live_head(self):
# An older run's decide finishing after a newer run approved the new head
# must withdraw only what is stale against that head.
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/cursor-approve.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ name: Cursor approve
# the commit_sha it stamped), posts the approval through
# .github/cursor-review/auto-approve.py approve-external
# (head and base re-read, reviewed-SHA marker, own stale
# approvals withdrawn, never over needs-human-review)
# approvals withdrawn, never over needs-human-review or
# skip-cursor-review, both re-read live at decide time)
# and rewrites the card with the verdicts.
#
# No job here checks out PR code; the only checkout is this repo at
Expand Down
Loading
Loading