Skip to content

Commit d85ff58

Browse files
authored
fix(review): record structured acceptance evidence (#5579)
Signed-off-by: Jim Meyer <jimeyer@nvidia.com>
1 parent 025f999 commit d85ff58

6 files changed

Lines changed: 232 additions & 44 deletions

File tree

‎.agents/skills/create-github-pr/SKILL.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ gh issue view ISSUE_NUMBER --repo OWNER/REPO --json number,url,labels
2828
```
2929

3030
Authors with verified `WRITE`, `MAINTAIN`, or `ADMIN` access may proceed without
31-
`state:accepted`; keep the issue link and note the missing label. Everyone else
31+
`state:accepted`; keep the issue link. Do not add access or acceptance evidence
32+
to the PR description. Everyone else
3233
must link at least one target-repository issue and have `state:accepted` on each.
3334
Otherwise, preserve local work and report which issue needs acceptance. A direct
3435
request, roadmap placement, other labels, or vouch status does not bypass this gate.

‎CONTRIBUTING.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -507,7 +507,7 @@ Authors with `WRITE`, `MAINTAIN`, or `ADMIN` access may open a PR that only fixe
507507

508508
### Automatic Acceptance Labeling
509509

510-
The existing [review-readiness workflow](.github/workflows/review-readiness.yml) checks [accepted work](#accepted-work) when a PR is opened and adds `state:accepted` if it qualifies. This includes drafts and PRs awaiting area/topic classification. Reviewer requests still wait until a PR is ready and has an area label.
510+
The existing [review-readiness workflow](.github/workflows/review-readiness.yml) checks [accepted work](#accepted-work) when a PR is opened and adds `state:accepted` if it qualifies. The label event's `intent.rationale` contains JSON with `schema: openshell-acceptance-v1` and a `basis`: `maintainer`, `repository_permission` (with `permission`), or `accepted_issue` (with issue number and author/assignee `relationship`). This includes drafts and PRs awaiting area/topic classification. Reviewer requests still wait until a PR is ready and has an area label.
511511

512512
This is an additive check at creation, not continuous reconciliation: later edits, permission changes, and issue-label or assignment changes do not refresh or remove the PR label. The shared library is read-only; review-readiness applies the label. The label does not replace the issue-acceptance gate or merge approvals.
513513

‎tasks/scripts/check_review_readiness.py‎

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,14 @@
2727
from pathlib import Path
2828

2929
from check_maintainer_approval import parse_maintainers
30-
from label_accepted_work import ACCEPTED_LABEL, check_pr_accepted_work
30+
from label_accepted_work import ACCEPTED_LABEL, evaluate_pr_accepted_work
3131

3232
HEADER = ["Name", "GitHub ID", "Company/Organization"]
3333
LABEL = re.compile(r"(?:area|topic):[a-z0-9]+(?:[-:][a-z0-9]+)*")
3434
PROFILE = re.compile(r"\[@([a-z0-9-]+)\]\(https://github\.com/([a-z0-9-]+)/?\)", re.I)
35+
ADD_LABEL = """mutation($input: AddLabelsToLabelableInput!) {
36+
addLabelsToLabelable(input: $input) { labelable { __typename } }
37+
}"""
3538

3639

3740
@dataclass(frozen=True)
@@ -212,6 +215,28 @@ def _routing_key(pr: dict):
212215
)
213216

214217

218+
def _add_accepted_label(repository: str, pr: dict, evidence: dict) -> None:
219+
label = _api(f"repos/{repository}/labels/state%3Aaccepted")
220+
rationale = json.dumps(
221+
{"schema": "openshell-acceptance-v1", **evidence}, separators=(",", ":")
222+
)
223+
result = _api(
224+
"graphql",
225+
method="POST",
226+
data={
227+
"query": ADD_LABEL,
228+
"variables": {
229+
"input": {
230+
"labelableId": pr["node_id"],
231+
"labels": [{"labelId": label["node_id"], "rationale": rationale}],
232+
}
233+
},
234+
},
235+
)
236+
if result.get("errors") or not result.get("data", {}).get("addLabelsToLabelable"):
237+
raise ValueError("Could not apply accepted-work label with evidence")
238+
239+
215240
def check_review_readiness(
216241
categories: dict[str, set[str]],
217242
repository: str,
@@ -235,17 +260,14 @@ def check_review_readiness(
235260
add_labels = ()
236261
labels = {label["name"] for label in pr["labels"]}
237262
if label_accepted_work:
238-
accepted = check_pr_accepted_work(
263+
evidence = evaluate_pr_accepted_work(
239264
repository, pr, maintainers or set(), api=_api
240265
)
266+
accepted = evidence is not None
241267
if accepted and ACCEPTED_LABEL not in labels:
242268
add_labels = (ACCEPTED_LABEL,)
243269
if apply:
244-
_api(
245-
f"repos/{repository}/issues/{number}/labels",
246-
method="POST",
247-
data={"labels": list(add_labels)},
248-
)
270+
_add_accepted_label(repository, pr, evidence)
249271
if pr["draft"] or not any(label.startswith("area:") for label in labels):
250272
if pr["draft"] and not label_accepted_work:
251273
return None

‎tasks/scripts/check_review_readiness_test.py‎

Lines changed: 106 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -417,11 +417,74 @@ def test_retry_after_partial_failure_uses_updated_github_metadata(self):
417417

418418

419419
class AcceptedWorkIntegrationTests(unittest.TestCase):
420+
def test_accepted_label_event_carries_structured_rationale(self):
421+
with (
422+
mock.patch.object(
423+
routing,
424+
"_api",
425+
side_effect=[
426+
{**pr(draft=True), "node_id": "PR_node"},
427+
{"node_id": "label_node"},
428+
{
429+
"data": {
430+
"addLabelsToLabelable": {
431+
"labelable": {"__typename": "PullRequest"}
432+
}
433+
}
434+
},
435+
],
436+
) as api,
437+
mock.patch.object(
438+
routing,
439+
"evaluate_pr_accepted_work",
440+
return_value={"basis": "repository_permission", "permission": "admin"},
441+
),
442+
):
443+
routing.check_review_readiness(
444+
CATEGORIES,
445+
REPO,
446+
42,
447+
apply=True,
448+
label_accepted_work=True,
449+
maintainers={"maintainer"},
450+
)
451+
request = api.call_args.kwargs["data"]
452+
label = request["variables"]["input"]["labels"][0]
453+
self.assertEqual(request["variables"]["input"]["labelableId"], "PR_node")
454+
self.assertEqual(label["labelId"], "label_node")
455+
self.assertEqual(
456+
json.loads(label["rationale"]),
457+
{
458+
"schema": "openshell-acceptance-v1",
459+
"basis": "repository_permission",
460+
"permission": "admin",
461+
},
462+
)
463+
464+
def test_label_mutation_graphql_error_is_not_silent(self):
465+
with (
466+
mock.patch.object(
467+
routing,
468+
"_api",
469+
side_effect=[
470+
{"node_id": "label_node"},
471+
{"errors": [{"message": "denied"}]},
472+
],
473+
),
474+
self.assertRaisesRegex(ValueError, "Could not apply"),
475+
):
476+
routing._add_accepted_label(
477+
REPO, {"node_id": "PR_node"}, {"basis": "maintainer"}
478+
)
479+
420480
def run_check(self, value, *, accepted=True, apply=True, opened=True):
421481
with (
422-
mock.patch.object(routing, "_api", side_effect=[value, None]) as api,
482+
mock.patch.object(routing, "_api", side_effect=[value]) as api,
483+
mock.patch.object(routing, "_add_accepted_label") as add_label,
423484
mock.patch.object(
424-
routing, "check_pr_accepted_work", return_value=accepted
485+
routing,
486+
"evaluate_pr_accepted_work",
487+
return_value={"basis": "maintainer"} if accepted else None,
425488
) as check,
426489
mock.patch.object(
427490
routing, "_load_reviewers", return_value=reviewers()
@@ -435,29 +498,29 @@ def run_check(self, value, *, accepted=True, apply=True, opened=True):
435498
label_accepted_work=opened,
436499
maintainers={"maintainer"},
437500
)
501+
if (
502+
value["state"] == "open"
503+
and opened
504+
and accepted
505+
and apply
506+
and not any(label["name"] == "state:accepted" for label in value["labels"])
507+
):
508+
add_label.assert_called_once_with(REPO, value, {"basis": "maintainer"})
509+
else:
510+
add_label.assert_not_called()
438511
return plan, api, check, load
439512

440513
def test_new_draft_is_labeled_without_requesting_reviewers(self):
441514
plan, api, check, load = self.run_check(pr(draft=True))
442515
self.assertEqual(plan, routing.ReviewPlan((), (), True, ("state:accepted",)))
443516
check.assert_called_once_with(REPO, pr(draft=True), {"maintainer"}, api=api)
444-
api.assert_has_calls(
445-
[
446-
mock.call(PULL),
447-
mock.call(
448-
"repos/owner/repo/issues/42/labels",
449-
method="POST",
450-
data={"labels": ["state:accepted"]},
451-
),
452-
]
453-
)
454-
self.assertEqual(api.call_count, 2)
517+
api.assert_called_once_with(PULL)
455518
load.assert_not_called()
456519

457520
def test_new_ready_pr_can_be_labeled_before_area_classification(self):
458521
plan, api, _, load = self.run_check(pr())
459522
self.assertEqual(plan.add_labels, ("state:accepted",))
460-
self.assertEqual(api.call_count, 2)
523+
self.assertEqual(api.call_count, 1)
461524
load.assert_not_called()
462525

463526
def test_nonqualifying_pr_is_not_labeled(self):
@@ -496,9 +559,14 @@ def test_closed_pr_is_never_classified_or_labeled(self):
496559
def test_labels_and_requests_reviewers_for_a_classified_ready_pr(self):
497560
with (
498561
mock.patch.object(
499-
routing, "_api", side_effect=[pr("area:build"), None, None]
562+
routing, "_api", side_effect=[pr("area:build"), None]
500563
) as api,
501-
mock.patch.object(routing, "check_pr_accepted_work", return_value=True),
564+
mock.patch.object(routing, "_add_accepted_label"),
565+
mock.patch.object(
566+
routing,
567+
"evaluate_pr_accepted_work",
568+
return_value={"basis": "maintainer"},
569+
),
502570
mock.patch.object(routing, "_load_reviewers", return_value=reviewers()),
503571
):
504572
plan = routing.check_review_readiness(
@@ -522,7 +590,7 @@ def test_lookup_failure_does_not_label_or_request_reviewers(self):
522590
mock.patch.object(routing, "_api", return_value=pr("area:build")) as api,
523591
mock.patch.object(
524592
routing,
525-
"check_pr_accepted_work",
593+
"evaluate_pr_accepted_work",
526594
side_effect=ValueError("lookup failed"),
527595
),
528596
self.assertRaisesRegex(ValueError, "lookup failed"),
@@ -538,9 +606,12 @@ def test_lookup_failure_does_not_label_or_request_reviewers(self):
538606
api.assert_called_once_with(PULL)
539607

540608
def test_real_library_qualifies_a_maintainer_without_permission_reads(self):
541-
with mock.patch.object(
542-
routing, "_api", side_effect=[pr(author="MAINTAINER", draft=True), None]
543-
) as api:
609+
with (
610+
mock.patch.object(
611+
routing, "_api", side_effect=[pr(author="MAINTAINER", draft=True)]
612+
) as api,
613+
mock.patch.object(routing, "_add_accepted_label"),
614+
):
544615
plan = routing.check_review_readiness(
545616
CATEGORIES,
546617
REPO,
@@ -551,16 +622,21 @@ def test_real_library_qualifies_a_maintainer_without_permission_reads(self):
551622
)
552623
assert plan is not None
553624
self.assertTrue(plan.accepted_work)
554-
self.assertEqual(api.call_count, 2)
625+
self.assertEqual(api.call_count, 1)
555626

556627
def test_label_result_survives_the_refresh_before_reviewer_removal(self):
557628
before = pr("area:build", requested=("alice", "dave"))
558629
after = pr("area:build", "state:accepted", requested=("alice", "dave"))
559630
with (
560631
mock.patch.object(
561-
routing, "_api", side_effect=[before, None, after, None]
632+
routing, "_api", side_effect=[before, after, None]
562633
) as api,
563-
mock.patch.object(routing, "check_pr_accepted_work", return_value=True),
634+
mock.patch.object(routing, "_add_accepted_label"),
635+
mock.patch.object(
636+
routing,
637+
"evaluate_pr_accepted_work",
638+
return_value={"basis": "maintainer"},
639+
),
564640
mock.patch.object(
565641
routing,
566642
"_load_reviewers",
@@ -586,8 +662,13 @@ def test_label_result_survives_the_refresh_before_reviewer_removal(self):
586662
def test_label_write_failure_does_not_request_reviewers(self):
587663
error = subprocess.CalledProcessError(1, "gh", stderr="(HTTP 403)")
588664
with (
589-
mock.patch.object(routing, "_api", side_effect=[pr("area:build"), error]),
590-
mock.patch.object(routing, "check_pr_accepted_work", return_value=True),
665+
mock.patch.object(routing, "_api", side_effect=[pr("area:build")]),
666+
mock.patch.object(routing, "_add_accepted_label", side_effect=error),
667+
mock.patch.object(
668+
routing,
669+
"evaluate_pr_accepted_work",
670+
return_value={"basis": "maintainer"},
671+
),
591672
mock.patch.object(routing, "_load_reviewers") as load,
592673
self.assertRaises(subprocess.CalledProcessError),
593674
):

‎tasks/scripts/label_accepted_work.py‎

Lines changed: 42 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class ClosingIssue:
4242
accepted: bool
4343
author: str | None
4444
assignees: frozenset[str] = frozenset()
45+
number: int | None = None
4546

4647

4748
def is_accepted_work(
@@ -53,20 +54,43 @@ def is_accepted_work(
5354
closing_issues: Iterable[ClosingIssue] = (),
5455
) -> bool:
5556
"""Classify supplied facts without IO; the triggering actor is irrelevant."""
57+
return (
58+
acceptance_evidence(
59+
author,
60+
repository=repository,
61+
maintainers=maintainers,
62+
permission=permission,
63+
closing_issues=closing_issues,
64+
)
65+
is not None
66+
)
67+
68+
69+
def acceptance_evidence(
70+
author, *, repository, maintainers, permission, closing_issues=()
71+
):
72+
"""Return the first qualifying basis, or None when work is not accepted."""
5673
author = author.lower()
5774
if author in {login.lower() for login in maintainers}:
58-
return True
75+
return {"basis": "maintainer"}
5976
if permission in WRITE_PERMISSIONS:
60-
return True
77+
return {"basis": "repository_permission", "permission": permission}
6178
for issue in closing_issues:
6279
if issue.repository.lower() != repository.lower() or not issue.accepted:
6380
continue
64-
participants = {login.lower() for login in issue.assignees}
65-
if issue.author:
66-
participants.add(issue.author.lower())
67-
if author in participants:
68-
return True
69-
return False
81+
if issue.author and issue.author.lower() == author:
82+
return {
83+
"basis": "accepted_issue",
84+
"issue": issue.number,
85+
"relationship": "author",
86+
}
87+
if author in {login.lower() for login in issue.assignees}:
88+
return {
89+
"basis": "accepted_issue",
90+
"issue": issue.number,
91+
"relationship": "assignee",
92+
}
93+
return None
7094

7195

7296
def _closing_issue_facts(
@@ -106,6 +130,7 @@ def _closing_issue_facts(
106130
accepted=any(label["name"] == ACCEPTED_LABEL for label in labels),
107131
author=issue["user"]["login"] if issue["user"] else None,
108132
assignees=frozenset(user["login"] for user in issue["assignees"]),
133+
number=reference["number"],
109134
)
110135
if not connection["pageInfo"]["hasNextPage"]:
111136
return
@@ -118,7 +143,14 @@ def _closing_issue_facts(
118143
def check_pr_accepted_work(
119144
repository: str, pr: dict, maintainers: set[str], *, api: Callable
120145
) -> bool:
121-
"""Load current facts using a read-only API callable and classify this PR.
146+
"""Load current facts using a read-only API callable and classify this PR."""
147+
return evaluate_pr_accepted_work(repository, pr, maintainers, api=api) is not None
148+
149+
150+
def evaluate_pr_accepted_work(
151+
repository: str, pr: dict, maintainers: set[str], *, api: Callable
152+
):
153+
"""Load current facts and return the qualifying acceptance evidence.
122154
123155
Callers supply maintainer membership from a trusted merged roster. A 404
124156
collaborator lookup means no repository access; other lookup errors propagate.
@@ -137,7 +169,7 @@ def check_pr_accepted_work(
137169
except subprocess.CalledProcessError as exc:
138170
if "(HTTP 404)" not in (exc.stderr or ""):
139171
raise
140-
return is_accepted_work(
172+
return acceptance_evidence(
141173
author,
142174
repository=repository,
143175
maintainers=maintainers,

0 commit comments

Comments
 (0)