Repository navigation
ci: guard good-first-issue/help-wanted labels against duplicate PRs - #574
Conversation
Agentic contribution tools (Cursor background agents and similar) poll GitHub's search API on `is:open no:assignee label:"good first issue"` — they don't read claim comments. When 15 issues got both labels in one burst, two different tools opened PRs for the same issue within 50 minutes of each other because neither tool's feed reflected the other's claim (public PRs #572, #573 against the same issue #560). Removing the labels the instant a closing PR opens is the lever that actually keeps an issue off those feeds; a comment changes nothing they read. Labels are restored, and only the exact ones this workflow itself removed, if that PR is later closed without merging. Co-Authored-By: Claude Sonnet 5 <[email protected]>
|
| Filename | Overview |
|---|---|
| .github/scripts/contributor_label_guard.py | Implements parsing and issue-label mutation, but permits untrusted suppression, restores labels while sibling claims remain active, and loses restoration ownership when references are edited. |
| .github/workflows/contributor-label-guard.yml | Safely checks out the base commit, but intentionally grants issues:write to a pull_request_target path whose mutation targets are selected by untrusted PR metadata. |
| tests/unit/test_contributor_label_guard.py | Covers parser and isolated timeline decisions but omits authorization, overlapping-claim, and edited-reference lifecycle scenarios. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[Fork or repository PR event] --> B[Read current PR title and body]
B --> C[Parse closing issue references]
C --> D{Event action}
D -->|opened or reopened| E[Fetch each referenced issue]
E --> F[Remove guarded labels]
D -->|closed and unmerged| G[Fetch current issue and timeline]
G --> H[Find latest guarded-label event]
H --> I[Restore labels attributed to workflow bot]
D -->|closed and merged| J[Leave labels absent]
Prompt To Fix All With AI
Fix the following 3 code review issues. Work through them one at a time, proposing concise fixes.
---
### Issue 1 of 3
.github/scripts/contributor_label_guard.py:337-344
**Untrusted metadata deletes issue labels**
When a fork author opens or reopens a PR containing closing references to arbitrary same-repository issues, this path passes those references directly to the privileged label-removal handler without checking author association, assignment, or approval, causing unrelated guarded issues to disappear from contributor feeds. **How this was verified:** The `pull_request_target` workflow grants `issues: write` and passes fork-controlled title and body references directly to `_handle_claim`.
### Issue 2 of 3
.github/scripts/contributor_label_guard.py:291-296
**Overlapping claims restore labels early**
When two open PRs reference the same issue and either closes unmerged, the second claim has recorded no ownership because the labels were already absent, while this release path attributes the original removal only to the shared bot identity and restores the labels, causing an issue with an active PR to reappear in contributor feeds.
### Issue 3 of 3
.github/scripts/contributor_label_guard.py:337-349
**Edited references orphan removed labels**
When an author removes or replaces a closing reference before closing an unmerged PR, this path re-parses only the current title and body and never releases the originally referenced issue, causing its guarded labels to remain absent indefinitely.
Reviews (1): Last reviewed commit: "ci: guard good-first-issue/help-wanted l..." | Re-trigger Greptile
| issue_numbers = resolve_pr_closing_issues(title, body, repo) | ||
| if not issue_numbers: | ||
| print(f"no-op: PR #{pr_number} has no closing-keyword issue references") | ||
| return 0 | ||
|
|
||
| if action in ("opened", "reopened"): | ||
| for number in issue_numbers: | ||
| _handle_claim(repo, number, token) |
There was a problem hiding this comment.
Untrusted metadata deletes issue labels
When a fork author opens or reopens a PR containing closing references to arbitrary same-repository issues, this path passes those references directly to the privileged label-removal handler without checking author association, assignment, or approval, causing unrelated guarded issues to disappear from contributor feeds. How this was verified: The pull_request_target workflow grants issues: write and passes fork-controlled title and body references directly to _handle_claim.
Prompt To Fix With AI
This is a comment left during a code review.
Path: .github/scripts/contributor_label_guard.py
Line: 337-344
Comment:
**Untrusted metadata deletes issue labels**
When a fork author opens or reopens a PR containing closing references to arbitrary same-repository issues, this path passes those references directly to the privileged label-removal handler without checking author association, assignment, or approval, causing unrelated guarded issues to disappear from contributor feeds. **How this was verified:** The `pull_request_target` workflow grants `issues: write` and passes fork-controlled title and body references directly to `_handle_claim`.
How can I resolve this? If you propose a fix, please make it concise.| events = fetch_issue_timeline(repo, number, token) | ||
| to_restore = labels_to_restore(events, current, actor_login=actor_login) | ||
| if not to_restore: | ||
| print(f"no-op: nothing to restore on issue #{number}") | ||
| return | ||
| result = add_labels(repo, number, to_restore, token) |
There was a problem hiding this comment.
Overlapping claims restore labels early
When two open PRs reference the same issue and either closes unmerged, the second claim has recorded no ownership because the labels were already absent, while this release path attributes the original removal only to the shared bot identity and restores the labels, causing an issue with an active PR to reappear in contributor feeds.
Prompt To Fix With AI
This is a comment left during a code review.
Path: .github/scripts/contributor_label_guard.py
Line: 291-296
Comment:
**Overlapping claims restore labels early**
When two open PRs reference the same issue and either closes unmerged, the second claim has recorded no ownership because the labels were already absent, while this release path attributes the original removal only to the shared bot identity and restores the labels, causing an issue with an active PR to reappear in contributor feeds.
How can I resolve this? If you propose a fix, please make it concise.| issue_numbers = resolve_pr_closing_issues(title, body, repo) | ||
| if not issue_numbers: | ||
| print(f"no-op: PR #{pr_number} has no closing-keyword issue references") | ||
| return 0 | ||
|
|
||
| if action in ("opened", "reopened"): | ||
| for number in issue_numbers: | ||
| _handle_claim(repo, number, token) | ||
| elif merged: | ||
| print(f"no-op: PR #{pr_number} merged — issue(s) {issue_numbers} close on merge") | ||
| else: | ||
| for number in issue_numbers: | ||
| _handle_release(repo, number, token, actor_login) |
There was a problem hiding this comment.
Edited references orphan removed labels
When an author removes or replaces a closing reference before closing an unmerged PR, this path re-parses only the current title and body and never releases the originally referenced issue, causing its guarded labels to remain absent indefinitely.
Prompt To Fix With AI
This is a comment left during a code review.
Path: .github/scripts/contributor_label_guard.py
Line: 337-349
Comment:
**Edited references orphan removed labels**
When an author removes or replaces a closing reference before closing an unmerged PR, this path re-parses only the current title and body and never releases the originally referenced issue, causing its guarded labels to remain absent indefinitely.
How can I resolve this? If you propose a fix, please make it concise.
Agentic contribution tools (Cursor background agents and similar) poll GitHub's search API on
is:open no:assignee label:"good first issue". They don't read claim comments, only the label feed — so a claim comment doesn't stop a second tool from picking up the same issue.Summary
pull_request_targetopened/reopened, if the PR's title/body references an issue with a closing keyword (closes,fixes,resolves, ...), removegood first issueandhelp wantedfrom that issue — taking it off the polling feeds immediately.pull_request_targetclosedwithout merge, restore exactly the labels this workflow removed. On merge, leave them off (the issue closes anyway)..github/scripts/contributor_label_guard.pyas plain, network-free functions (same convention as the existingarchive_traffic.py); the workflow YAML just calls it.Real incident this addresses
15 issues were labeled
good first issue+help wantedin one burst. Two different agentic tools opened PRs for the same issue (#560) within 50 minutes of each other — #572 and #573 — because neither tool's feed reflected the other's claim. One had to be closed, wasting both sides' work.Security
This has to run as
pull_request_target(notpull_request) because fork PRs underpull_requestget a read-onlyGITHUB_TOKENand can't write labels — exactly the case that matters here. Sincepull_request_targetruns with base-repo write permissions even for a fork PR, the workflow:ref: ${{ github.event.pull_request.base.sha }}), never the PR head.pip install/execution of anything from the PR branch.issues: write,pull-requests: read,contents: read.Exact-restore mechanism
The script is intentionally stateless — no side file, DB row, or hidden issue comment. On restore it reads the issue's own timeline (
GET /issues/{n}/timeline) and, per guarded label, finds that label's most recentlabeled/unlabeledevent. A label is restored only if the most recent event is anunlabeledevent whose actor is this workflow's bot identity and the label isn't currently on the issue. This is exact (never adds a label the issue never had) and self-corrects if a human manually re-labels in between — theirlabeledevent becomes the most recent one and the workflow no longer treats the label as its own to restore. Reasoning for picking this over a hidden HTML-comment marker is documented in the module docstring.No-op safety
Every step is written to no-op rather than fail the run: label already absent (filtered out before any API call), issue number not found (404), issue number referring to a PR rather than an issue (
pull_requestkey on the issues-API payload), issue already closed._api_request()returns(status, payload)instead of raising on HTTP error responses so every caller decides what a given status means for its case.Tests / Verification
tests/unit/test_contributor_label_guard.py(30 tests): all closing-keyword verb forms, multiple refs in one body, multiple distinct keywords,owner/repo#Nmatching this repo vs. a different repo, mixed case, no-reference PRs, keyword-adjacent-but-no-#Ntext ("this fixes the typo... #113: Cost screen — replace table-only view with a real chart #123" elsewhere) correctly not matching, keyword-substring-inside-another-word ("enclosed docs: document how to upgrade TokenJam (docs + CLI help) #99") not matching, and the restore-only-what-was-removed logic (including: label already present, most-recent event is a human re-label, different actor, malformed timeline entries).ruff check .github/scripts/contributor_label_guard.py tests/unit/test_contributor_label_guard.py— clean.mypy .github/scripts/contributor_label_guard.py— clean.pytest tests/unit/— 3023 passed, 2 skipped (full suite, unrelated to this change).yaml.safe_load— valid.What's NOT in this PR
🤖 Generated with Claude Code