Skip to content

ci: guard good-first-issue/help-wanted labels against duplicate PRs - #574

Merged
anshss merged 1 commit into
mainfrom
ci/contributor-label-guard
Jul 25, 2026
Merged

anshss merged 1 commit into
mainfrom
ci/contributor-label-guard

Conversation

@anshss

@anshss anshss commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

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

  • On pull_request_target opened/reopened, if the PR's title/body references an issue with a closing keyword (closes, fixes, resolves, ...), remove good first issue and help wanted from that issue — taking it off the polling feeds immediately.
  • On pull_request_target closed without merge, restore exactly the labels this workflow removed. On merge, leave them off (the issue closes anyway).
  • Parsing + decision logic lives in .github/scripts/contributor_label_guard.py as plain, network-free functions (same convention as the existing archive_traffic.py); the workflow YAML just calls it.

Real incident this addresses

15 issues were labeled good first issue + help wanted in 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 (not pull_request) because fork PRs under pull_request get a read-only GITHUB_TOKEN and can't write labels — exactly the case that matters here. Since pull_request_target runs with base-repo write permissions even for a fork PR, the workflow:

  • Checks out the base commit only (ref: ${{ github.event.pull_request.base.sha }}), never the PR head.
  • Runs no PR-authored code — the script treats the PR title/body purely as untrusted text to regex-match, no pip install/execution of anything from the PR branch.
  • Requests only issues: write, pull-requests: read, contents: read.
  • A comment block at the top of the workflow file states these constraints explicitly so a future edit doesn't casually add a PR-head checkout.

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 recent labeled/unlabeled event. A label is restored only if the most recent event is an unlabeled event 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 — their labeled event 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_request key 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#N matching this repo vs. a different repo, mixed case, no-reference PRs, keyword-adjacent-but-no-#N text ("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).
  • Workflow YAML parsed with yaml.safe_load — valid.

What's NOT in this PR

  • Auto-assigning the contributor to the issue — GitHub only permits assigning users with write access or who've already commented on the issue, so it silently fails for the exact outside-contributor case this targets. Label removal has no such permission edge case.
  • A "contribution bar" footer on issue bodies — a separate, deliberately deferred manual decision.
  • Any change to existing issue labels or to CONTRIBUTING/AGENTS.md.

🤖 Generated with Claude Code

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]>
@anshss
anshss requested a review from anilmurty as a code owner July 25, 2026 10:48
@greptile-apps

greptile-apps Bot commented Jul 25, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

Adds a privileged contributor-label guard workflow and its network-free parsing and decision helpers.

  • Removes good-first-issue and help-wanted labels when an opened or reopened PR contains a same-repository closing reference.
  • Uses issue timeline events to restore bot-removed labels when an unmerged PR closes.
  • Adds unit coverage for closing-reference parsing and label removal/restoration decisions.

Confidence Score: 2/5

This PR should not merge until arbitrary fork-authored label suppression and the overlapping and edited claim lifecycle failures are addressed.

The privileged event path lets untrusted PR text remove labels from arbitrary guarded issues, while the stateless release logic can either restore labels despite another active PR or permanently fail to restore them after a reference edit.

Files Needing Attention: .github/scripts/contributor_label_guard.py and .github/workflows/contributor-label-guard.yml

Security Review

A fork author can place arbitrary closing references in PR metadata and cause the issues: write pull_request_target workflow to remove guarded labels from unrelated issues. How this was verified: The opened/reopened event path passes parsed title/body references directly to label deletion using the base repository token without an author or legitimacy guard.

Important Files Changed

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]
Loading
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

Comment on lines +337 to +344
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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 security 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.

Comment on lines +291 to +296
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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 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.

Comment on lines +337 to +349
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)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 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.

@anshss
anshss merged commit 76e72e2 into main Jul 25, 2026
5 checks passed
@anilmurty
anilmurty deleted the ci/contributor-label-guard branch July 29, 2026 23:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant