Skip to content

fix(scripts): preserve existing labels when auto-closing duplicate issues#76986

Open
AliAltivate wants to merge 1 commit into
anthropics:mainfrom
AliAltivate:fix/auto-close-duplicates-preserve-labels
Open

fix(scripts): preserve existing labels when auto-closing duplicate issues#76986
AliAltivate wants to merge 1 commit into
anthropics:mainfrom
AliAltivate:fix/auto-close-duplicates-preserve-labels

Conversation

@AliAltivate

Copy link
Copy Markdown

What

scripts/auto-close-duplicates.ts closes each detected duplicate with a single PATCH /repos/{owner}/{repo}/issues/{n}:

{
  state: 'closed',
  state_reason: 'duplicate',
  labels: ['duplicate']   // replaces the issue's ENTIRE label set
}

Per GitHub's REST API, the Update an issue endpoint's labels field replaces the full label set — docs: "Pass one or more labels to replace the set of labels on this issue."

So every issue auto-closed as a duplicate has all of its other labels silently wiped (bug, area:*, platform:*, priority, needs-info, …), leaving only duplicate. That destroys triage/analytics metadata on closed issues. This script runs in .github/workflows/auto-close-duplicates.yml.

The fix

Add the duplicate label with the Add labels to an issue endpoint (POST /issues/{n}/labels), which appends to the existing labels — docs: "the labels to add to the issue's existing labels" — and drop labels from the PATCH so it only closes the issue and sets state_reason: 'duplicate':

// append — preserves existing labels
POST  /repos/{owner}/{repo}/issues/{n}/labels   { labels: ['duplicate'] }
// then close
PATCH /repos/{owner}/{repo}/issues/{n}          { state: 'closed', state_reason: 'duplicate' }

The label is added before the close so a partial failure leaves the issue open and retryable, rather than closed without its triage metadata.

Verification

  • The clobber was the only labels reference in the file — single-site change.
  • Transpiles cleanly under bun (the script's runtime).
  • Behavior is grounded in the two official GitHub REST endpoints linked above (replace vs. append). The script mutates production issues, so it isn't run live here; the fix relies on documented endpoint semantics.

🤖 Generated with Claude Code

closeIssueAsDuplicate() closed the issue with a single PATCH whose body
included `labels: ['duplicate']`. GitHub's "Update an issue" endpoint
*replaces* the issue's entire label set with the array it receives
("Pass one or more labels to replace the set of labels on this issue"),
so every auto-closed duplicate had all of its triage labels (bug,
area:*, platform:*, priority, needs-info, ...) silently wiped, leaving
only `duplicate`.

Add the `duplicate` label via the "Add labels to an issue" endpoint
(POST /issues/{n}/labels), which appends to the existing labels, and
drop `labels` from the PATCH so it only closes the issue and sets
state_reason: 'duplicate'. The label is added before the close so a
partial failure leaves the issue open and retryable rather than closed
without its triage metadata.

Co-Authored-By: Claude <noreply@anthropic.com>
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