Skip to content

Commit 233fd2d

Browse files
mattmilleraiclaude
andauthored
fix(cursor-approve): approve-external refuses a PR labelled skip-cursor-review (#351)
* fix(cursor-approve): approve-external refuses a PR labelled skip-cursor-review A caller whose workflow concurrency group is keyed on the label name cannot cancel an in-flight decide when `skip-cursor-review` lands mid-axes: the veto starts a run in a different slot, and decide runs under always(). So the veto is now re-checked where the approval is posted. approve-external reads `skip-cursor-review` from the live PR object it already re-reads for head/base/needs-human-review, and treats it exactly like needs-human-review: no APPROVE, this identity's own marked approvals withdrawn, and a new `vetoed` outcome the decide card renders. The post-write re-read withdraws an approval if the veto arrives during the POST. A PR payload with no label list fails closed rather than reading as "no veto". Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> * fix(cursor-approve): the skip-cursor-review veto outranks supersession and withdraws PR-wide Review round on the approve-external veto: - the veto is checked before head/base supersession (pre- and post-POST), so a late decide on a vetoed PR no longer keeps an approval standing on the live head; - a veto landing during the POST withdraws every own marked approval, not just the one just posted; - an unreadable post-POST label list withdraws under its own message and exits red instead of claiming the veto label was applied; - `live_labels_readable` rejects entries `has_label` would silently skip; - the review POST response is validated for an id, as cmd_decide does; - the live-vs-payload test now distinguishes the two sources in both directions. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent f8a0933 commit 233fd2d

7 files changed

Lines changed: 209 additions & 17 deletions

File tree

‎.github/cursor-approve/card.py‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
OUTCOME_NOT_APPROVED = "not_approved"
4747
OUTCOME_SUPERSEDED = "superseded"
4848
OUTCOME_HUMAN = "needs_human"
49+
OUTCOME_VETOED = "vetoed"
4950
OUTCOME_OWN_PR = "own_pr"
5051
OUTCOME_ERROR = "error"
5152

@@ -159,6 +160,7 @@ def render_decide(round_no, max_rounds, sha: str, axes: list, decision, outcome:
159160
else:
160161
why = {
161162
OUTCOME_HUMAN: ["the PR is labelled `needs-human-review`"],
163+
OUTCOME_VETOED: ["vetoed: the PR is labelled `skip-cursor-review`"],
162164
OUTCOME_OWN_PR: ["the approver authored this PR"],
163165
OUTCOME_ERROR: ["the approval could not be posted (see the workflow run)"],
164166
}.get(outcome)

‎.github/cursor-approve/tests/test_card.py‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@ def test_decide_not_approved_lists_reasons_and_missing_axis(self):
6868
self.assertIn("red on correctness", body)
6969
self.assertIn("no red, at most 0 yellow", body)
7070

71+
def test_decide_vetoed_names_the_skip_label(self):
72+
body = card.render_decide("1", "5", SHA, ["correctness"], decision(correctness="green"), "vetoed", "0", "")
73+
self.assertIn("**Result: ❌ Not approved.**", body)
74+
self.assertIn("- vetoed: the PR is labelled `skip-cursor-review`", body)
75+
self.assertNotIn("no decision was reached", body)
76+
7177
def test_malformed_decision_shows_no_result(self):
7278
body = card.render_decide("1", "5", SHA, ["correctness"], {"axes": "junk"}, "not_approved", "0", "")
7379
self.assertIn("| correctness | ⚠️ no result |", body)

‎.github/cursor-review/auto-approve.py‎

Lines changed: 66 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,17 @@ def pr_head_base(pr: dict) -> tuple:
946946
return _str_field(pr, "head", "sha"), _str_field(pr, "base", "ref")
947947

948948

949+
def live_labels_readable(pr: dict) -> bool:
950+
"""Whether the PR payload carries a label list `has_label` can fully read.
951+
`has_label` reads a missing or shapeless `labels` — or skips an entry that is
952+
not a dict with a string `name` — as "no such label", which fails OPEN for a
953+
veto label; a caller that must fail closed checks this first."""
954+
labels = pr.get("labels")
955+
return isinstance(labels, list) and all(
956+
isinstance(label, dict) and isinstance(label.get("name"), str) for label in labels
957+
)
958+
959+
949960
def has_label(pr: dict, name: str) -> bool:
950961
# A truthy non-list `labels` (a number, `true`) is not iterable, and the
951962
# TypeError would escape every caller's `except (RuntimeError, ValueError)`.
@@ -1548,9 +1559,18 @@ def cmd_round_cap(args) -> int:
15481559
# base ride in the body's markers (so `dismiss-stale` withdraws it on a push or
15491560
# retarget like any other auto-approval), this identity's own earlier approvals
15501561
# are withdrawn on every non-approval, and a `needs-human-review` PR is never
1551-
# approved. The outcome is written as the step output `outcome` for the card.
1552-
1553-
EXTERNAL_OUTCOMES = ("approved", "not_approved", "superseded", "needs_human", "own_pr", "error")
1562+
# approved. Nor is a PR labelled `skip-cursor-review`: a caller's label-keyed
1563+
# concurrency group cannot cancel an in-flight decide when that veto lands
1564+
# mid-axes, so decide re-reads it live. The outcome is written as the step
1565+
# output `outcome` for the card.
1566+
1567+
EXTERNAL_OUTCOMES = ("approved", "not_approved", "superseded", "needs_human", "vetoed", "own_pr", "error")
1568+
# cursor-review.yml's veto label (its gate and concurrency slot hardcode it too).
1569+
SKIP_REVIEW_LABEL = "skip-cursor-review"
1570+
SKIP_REVIEW_MESSAGE = f"The PR was labelled `{SKIP_REVIEW_LABEL}` — cursor-approve approval withdrawn."
1571+
LABELS_UNREADABLE_MESSAGE = (
1572+
f"The PR's labels could not be read to rule out `{SKIP_REVIEW_LABEL}` — cursor-approve approval withdrawn."
1573+
)
15541574
EXTERNAL_AXES = ("business", "design", "correctness", "completeness", "conformance")
15551575

15561576

@@ -1601,12 +1621,21 @@ def cmd_approve_external(args) -> int:
16011621
try:
16021622
pr = read_pr(args.repo, args.pr_number)
16031623
live_head, live_base = pr_head_base(pr)
1624+
if not live_labels_readable(pr):
1625+
# The veto below must be read, not assumed absent.
1626+
raise ValueError("the PR payload carries no label list")
16041627
human_review = has_label(pr, HUMAN_REVIEW_LABEL)
1628+
vetoed = has_label(pr, SKIP_REVIEW_LABEL)
16051629
except (RuntimeError, ValueError) as e:
16061630
print(f"::error::Could not read the PR state: {e}")
16071631
withdraw_own_approvals(args)
16081632
return 1
1609-
if live_head != args.commit_sha:
1633+
# The veto is PR-wide, so it outranks `superseded`: that path withdraws only
1634+
# what is stale against the live head, and would leave an approval already
1635+
# standing on the live head in place on a vetoed PR.
1636+
if vetoed:
1637+
outcome, why = "vetoed", f"the PR is labelled `{SKIP_REVIEW_LABEL}`"
1638+
elif live_head != args.commit_sha:
16101639
outcome, why = "superseded", f"the PR head moved from {args.commit_sha[:7]} to {live_head[:7] or '?'}"
16111640
elif live_base != args.base_ref:
16121641
# Same head, different base: the axes diffed against a base the PR no
@@ -1626,6 +1655,8 @@ def cmd_approve_external(args) -> int:
16261655
# is stale against the LIVE head and base, so a decide that finishes
16271656
# late cannot dismiss an approval that run already posted.
16281657
return withdraw_own_approvals(args, STALE_MESSAGE, live_head, live_base)
1658+
if outcome == "vetoed":
1659+
return withdraw_own_approvals(args, SKIP_REVIEW_MESSAGE)
16291660
return withdraw_own_approvals(args)
16301661

16311662
body = render_external_body(args.card_url, args.commit_sha, args.base_ref)
@@ -1636,7 +1667,11 @@ def cmd_approve_external(args) -> int:
16361667
{"commit_id": args.commit_sha, "event": APPROVE, "body": body},
16371668
)
16381669
)
1639-
except RuntimeError as e:
1670+
# As cmd_decide: the id is what a late veto or move dismisses by, so a
1671+
# `gh` that exits 0 without one is an ambiguous POST, not a success.
1672+
if not isinstance(posted, dict) or posted.get("id") is None:
1673+
raise ValueError(f"the review POST returned no review id ({type(posted).__name__})")
1674+
except (RuntimeError, ValueError) as e:
16401675
if "own pull request" in str(e).lower():
16411676
set_output("outcome", "own_pr")
16421677
emit(f"ℹ️ **cursor-approve: skipped** — the approver authored this PR ({e}).")
@@ -1650,17 +1685,39 @@ def cmd_approve_external(args) -> int:
16501685
pr_now = read_pr(args.repo, args.pr_number)
16511686
head_now, base_now = pr_head_base(pr_now)
16521687
labelled_now = has_label(pr_now, HUMAN_REVIEW_LABEL)
1688+
# An unreadable label list cannot clear the veto: withdraw, but under
1689+
# its own cause rather than claiming a label nobody applied.
1690+
labels_unreadable = not live_labels_readable(pr_now)
1691+
vetoed_now = has_label(pr_now, SKIP_REVIEW_LABEL)
16531692
except (RuntimeError, ValueError):
16541693
head_now = base_now = None
1655-
labelled_now = False
1694+
labelled_now = vetoed_now = labels_unreadable = False
16561695
moved = head_now != args.commit_sha or base_now != args.base_ref
1657-
if moved or labelled_now:
1658-
set_output("outcome", "superseded" if moved else "needs_human")
1696+
if moved or labelled_now or vetoed_now or labels_unreadable:
1697+
# Veto first, as before the POST: it is PR-wide.
1698+
if vetoed_now:
1699+
outcome, message = "vetoed", SKIP_REVIEW_MESSAGE
1700+
elif moved:
1701+
outcome, message = "superseded", STALE_MESSAGE
1702+
elif labelled_now:
1703+
outcome, message = "needs_human", HUMAN_REVIEW_MESSAGE
1704+
else:
1705+
outcome, message = "error", LABELS_UNREADABLE_MESSAGE
1706+
set_output("outcome", outcome)
16591707
try:
1660-
dismiss(args.repo, args.pr_number, posted["id"], STALE_MESSAGE if moved else HUMAN_REVIEW_MESSAGE)
1708+
dismiss(args.repo, args.pr_number, posted["id"], message)
16611709
except RuntimeError as e:
16621710
print(f"::error::The PR changed while the approval was posted, and withdrawing it failed: {e}. {DISMISS_PERMISSION_HINT}")
16631711
return 1
1712+
if outcome in ("vetoed", "error"):
1713+
# Not only the review just posted: any other standing approval by
1714+
# this identity (an earlier run's, or one posted concurrently) would
1715+
# still count toward branch protection on a PR that cannot be cleared.
1716+
if withdraw_own_approvals(args, message):
1717+
return 1
1718+
if outcome == "error":
1719+
print(f"::error::{LABELS_UNREADABLE_MESSAGE}")
1720+
return 1
16641721
emit("ℹ️ **cursor-approve: withdrawn** — the PR changed while the approval was being posted.")
16651722
return 0
16661723
set_output("outcome", "approved")

‎.github/cursor-review/tests/test_approve_external.py‎

Lines changed: 128 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,21 @@
1919

2020

2121
class FakeGitHub:
22-
def __init__(self, head=SHA, labels=(), reviews=(), head_after=None, base="main"):
22+
def __init__(self, head=SHA, labels=(), reviews=(), head_after=None, base="main",
23+
labels_after=None, read_error=False, pr_override=None, pr_after=None, post_reply=None):
2324
self.head, self.labels, self.reviews, self.base = head, list(labels), list(reviews), base
24-
self.head_after = head_after
25-
self.posted, self.dismissed, self.reads = [], [], 0
25+
self.head_after, self.labels_after = head_after, labels_after
26+
self.read_error, self.pr_override = read_error, pr_override
27+
self.pr_after, self.post_reply = pr_after, post_reply
28+
self.posted, self.dismissed, self.dismiss_messages, self.reads = [], [], [], 0
2629

2730
def __call__(self, args, payload=None):
2831
if args[:3] == ["api", "-X", "POST"] and args[3].endswith("/reviews"):
2932
self.posted.append(payload)
30-
return json.dumps({"id": 555})
33+
return json.dumps({"id": 555}) if self.post_reply is None else self.post_reply
3134
if args[:3] == ["api", "-X", "PUT"]:
3235
self.dismissed.append(args[3].split("/")[-2])
36+
self.dismiss_messages.append((payload or {}).get("message"))
3337
return "{}"
3438
if args[:2] == ["api", "graphql"]:
3539
# list_reviews reads through GraphQL (for `lastEditedAt`).
@@ -40,9 +44,16 @@ def __call__(self, args, payload=None):
4044
return json.dumps({"data": {"repository": {"pullRequest": {"reviews": {
4145
"pageInfo": {"hasNextPage": False, "endCursor": None}, "nodes": nodes}}}}})
4246
self.reads += 1
47+
if self.read_error:
48+
raise RuntimeError("gh api: HTTP 502")
49+
if self.pr_after is not None and self.reads > 1:
50+
return json.dumps(self.pr_after)
51+
if self.pr_override is not None:
52+
return json.dumps(self.pr_override)
4353
head = self.head_after if (self.head_after and self.reads > 1) else self.head
54+
labels = self.labels_after if (self.labels_after is not None and self.reads > 1) else self.labels
4455
return json.dumps({"head": {"sha": head}, "base": {"ref": self.base},
45-
"labels": [{"name": n} for n in self.labels]})
56+
"labels": [{"name": n} for n in labels]})
4657

4758

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

130+
# --- the skip-cursor-review veto, re-read live at decide time ---
131+
132+
def test_skip_label_vetoes_all_green_and_withdraws_prior_approval(self):
133+
fake = FakeGitHub(labels=["bug", aa.SKIP_REVIEW_LABEL], reviews=[prior_approval()])
134+
rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"},
135+
threshold="low", poster="cr-bot[bot]")
136+
self.assertEqual((rc, outcome), (0, "vetoed"))
137+
self.assertEqual(fake.posted, [])
138+
self.assertEqual(fake.dismissed, ["42"])
139+
self.assertEqual(self.resolve_calls, [])
140+
141+
def run_with_event_labels(self, fake, event_labels):
142+
with tempfile.TemporaryDirectory() as tmp:
143+
event = os.path.join(tmp, "event.json")
144+
with open(event, "w", encoding="utf-8") as f:
145+
json.dump({"action": "labeled", "label": {"name": "cursor-review"},
146+
"pull_request": {"number": 1, "head": {"sha": SHA},
147+
"labels": [{"name": n} for n in event_labels]}}, f)
148+
with mock.patch.dict(os.environ, {"GITHUB_EVENT_PATH": event}):
149+
return self.run_cmd(fake, {"correctness": "green", "conformance": "green"})
150+
151+
def test_skip_label_is_read_live_not_from_the_event_payload(self):
152+
# Live and payload labels disagree in both directions; only the live
153+
# read may decide. The run started on `labeled: cursor-review` and the
154+
# veto landed mid-axes: only the live read carries it.
155+
fake = FakeGitHub(labels=["cursor-review", aa.SKIP_REVIEW_LABEL])
156+
self.assertEqual(self.run_with_event_labels(fake, ["cursor-review"]), (0, "vetoed"))
157+
self.assertEqual(fake.posted, [])
158+
# The veto was removed mid-axes: a stale payload must not keep it.
159+
fake = FakeGitHub(labels=["cursor-review"])
160+
self.assertEqual(self.run_with_event_labels(fake, ["cursor-review", aa.SKIP_REVIEW_LABEL]),
161+
(0, "approved"))
162+
self.assertEqual(len(fake.posted), 1)
163+
164+
def test_skip_label_outranks_a_moved_head(self):
165+
# A late decide seeing both a moved head and the veto must withdraw the
166+
# approval already standing on the live head, not only stale ones.
167+
fake = FakeGitHub(head="d" * 40, labels=[aa.SKIP_REVIEW_LABEL],
168+
reviews=[prior_approval(), prior_approval("d" * 40, 77)])
169+
rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"})
170+
self.assertEqual((rc, outcome), (0, "vetoed"))
171+
self.assertEqual(fake.posted, [])
172+
self.assertEqual(sorted(fake.dismissed), ["42", "77"])
173+
174+
def test_malformed_label_entries_fail_closed(self):
175+
# Entries `has_label` would skip cannot rule the veto out.
176+
for labels in (["skip-cursor-review"], [{"name": None}], [{"name": "bug"}, 7]):
177+
fake = FakeGitHub(pr_override={"head": {"sha": SHA}, "base": {"ref": "main"}, "labels": labels})
178+
rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"})
179+
self.assertEqual((rc, outcome), (1, "error"), labels)
180+
self.assertEqual(fake.posted, [], labels)
181+
182+
def test_skip_label_match_is_exact(self):
183+
fake = FakeGitHub(labels=["skip-cursor-review-later", "no-skip-cursor-review"])
184+
rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"})
185+
self.assertEqual((rc, outcome), (0, "approved"))
186+
self.assertEqual(len(fake.posted), 1)
187+
188+
def test_label_read_failure_posts_nothing(self):
189+
fake = FakeGitHub(read_error=True, reviews=[prior_approval()])
190+
rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"})
191+
self.assertEqual((rc, outcome), (1, "error"))
192+
self.assertEqual(fake.posted, [])
193+
self.assertEqual(fake.dismissed, ["42"])
194+
195+
def test_missing_label_list_fails_closed(self):
196+
# A PR payload with no `labels` cannot prove the veto absent.
197+
for labels in (None, "skip-cursor-review", 1):
198+
pr = {"head": {"sha": SHA}, "base": {"ref": "main"}}
199+
if labels is not None:
200+
pr["labels"] = labels
201+
fake = FakeGitHub(pr_override=pr)
202+
rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"})
203+
self.assertEqual((rc, outcome), (1, "error"), labels)
204+
self.assertEqual(fake.posted, [], labels)
205+
206+
def test_skip_label_applied_during_post_withdraws(self):
207+
fake = FakeGitHub(labels_after=[aa.SKIP_REVIEW_LABEL])
208+
rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"},
209+
threshold="low", poster="cr-bot[bot]")
210+
self.assertEqual((rc, outcome), (0, "vetoed"))
211+
self.assertEqual(len(fake.posted), 1)
212+
self.assertEqual(fake.dismissed, ["555"])
213+
self.assertEqual(fake.dismiss_messages, [aa.SKIP_REVIEW_MESSAGE])
214+
self.assertEqual(self.resolve_calls, [])
215+
216+
def test_skip_label_during_post_withdraws_every_own_approval(self):
217+
# The veto is PR-wide: an approval this identity already has standing on
218+
# the same head goes too, not only the one just posted.
219+
fake = FakeGitHub(labels_after=[aa.SKIP_REVIEW_LABEL], head_after="d" * 40,
220+
reviews=[prior_approval(SHA, 77)])
221+
rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"})
222+
self.assertEqual((rc, outcome), (0, "vetoed"))
223+
self.assertEqual(fake.dismissed, ["555", "77"])
224+
225+
def test_unreadable_labels_after_post_withdraws_under_its_own_cause(self):
226+
fake = FakeGitHub(pr_after={"head": {"sha": SHA}, "base": {"ref": "main"}},
227+
reviews=[prior_approval(SHA, 77)])
228+
rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"},
229+
threshold="low", poster="cr-bot[bot]")
230+
self.assertEqual((rc, outcome), (1, "error"))
231+
self.assertEqual(fake.dismissed, ["555", "77"])
232+
self.assertEqual(fake.dismiss_messages, [aa.LABELS_UNREADABLE_MESSAGE] * 2)
233+
self.assertEqual(self.resolve_calls, [])
234+
235+
def test_post_without_a_review_id_withdraws_and_fails(self):
236+
for reply in ("<html>proxy error</html>", "null", "{}"):
237+
fake = FakeGitHub(post_reply=reply, reviews=[prior_approval(SHA, 77)])
238+
rc, outcome = self.run_cmd(fake, {"correctness": "green", "conformance": "green"})
239+
self.assertEqual((rc, outcome), (1, "error"), reply)
240+
self.assertEqual(fake.dismissed, ["77"], reply)
241+
119242
def test_late_superseded_decide_keeps_an_approval_on_the_live_head(self):
120243
# An older run's decide finishing after a newer run approved the new head
121244
# must withdraw only what is stale against that head.

‎.github/workflows/cursor-approve.yml‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ name: Cursor approve
1919
# the commit_sha it stamped), posts the approval through
2020
# .github/cursor-review/auto-approve.py approve-external
2121
# (head and base re-read, reviewed-SHA marker, own stale
22-
# approvals withdrawn, never over needs-human-review)
22+
# approvals withdrawn, never over needs-human-review or
23+
# skip-cursor-review, both re-read live at decide time)
2324
# and rewrites the card with the verdicts.
2425
#
2526
# No job here checks out PR code; the only checkout is this repo at

0 commit comments

Comments
 (0)