diff --git a/.github/workflows/cursor-automation.yml b/.github/workflows/cursor-automation.yml index 40f87db0d..dec71771e 100644 --- a/.github/workflows/cursor-automation.yml +++ b/.github/workflows/cursor-automation.yml @@ -355,6 +355,7 @@ jobs: uses: actions/github-script@v9 env: PULL_NUMBER: ${{ needs.route.outputs.pull_number }} + SOURCE_ISSUE_NUMBER: ${{ needs.route.outputs.issue_number }} OWN_ACTORS: ${{ vars.AUTOMATION_OWN_ACTORS || 'binaricat,netcatty-bot,github-actions[bot]' }} with: github-token: ${{ secrets.TRIAGE_GITHUB_TOKEN || secrets.GITHUB_TOKEN }} @@ -365,6 +366,10 @@ jobs: ...context.repo, pull_number: pullNumber, }); + if (pull.state !== 'closed') { + core.warning(`PR #${pullNumber} is no longer closed; skipping source cleanup.`); + return; + } if (!auto.shouldGatePullOnSourceIssueFollowups(pull, { ownActors: process.env.OWN_ACTORS, repository: `${context.repo.owner}/${context.repo.repo}`, @@ -373,7 +378,11 @@ jobs: return; } const issueNumber = auto.extractSourceIssueNumber(pull); - if (!issueNumber) return; + const expectedIssueNumber = Number(process.env.SOURCE_ISSUE_NUMBER); + if (!issueNumber || issueNumber !== expectedIssueNumber) { + core.warning(`PR #${pullNumber} source issue changed while queued; skipping cleanup.`); + return; + } const removeLabel = async (issue_number, name) => { try { await github.rest.issues.removeLabel({ @@ -415,7 +424,7 @@ jobs: contents: read issues: write pull-requests: write - actions: write + actions: read outputs: category: ${{ steps.apply.outputs.category }} should_implement: ${{ steps.apply.outputs.should_implement }} @@ -1212,7 +1221,7 @@ jobs: contents: read issues: write pull-requests: write - actions: read + actions: write outputs: action: ${{ steps.decision.outputs.action }} should_publish: ${{ steps.patch.outputs.should_publish || 'false' }} diff --git a/scripts/cursor-automation.test.cjs b/scripts/cursor-automation.test.cjs index 4cbda2014..f0ac4dd80 100644 --- a/scripts/cursor-automation.test.cjs +++ b/scripts/cursor-automation.test.cjs @@ -853,9 +853,17 @@ test('workflow cleans source labels on automation PR close and dedupes clean not assert.match(workflow, /github\.rest\.issues\.removeLabel/); assert.match(workflow, /github\.rest\.issues\.addLabels/); assert.match(workflow, /not a trusted automation pull request; skipping source cleanup/); + assert.match(workflow, /is no longer closed; skipping source cleanup/); + assert.match(workflow, /source issue changed while queued; skipping cleanup/); assert.match(workflow, /Follow-up changed before the simple reply; dispatched a fresh review/); assert.match(workflow, /is merged; skipped stale follow-up handoff state changes/); assert.match(workflow, /trusted_comment_bodies/); + const classify = workflow.match(/\n classify:\n[\s\S]*?(?=\n cursor_smoke:|\n issue_followup:)/)?.[0] || ''; + const followup = workflow.match(/\n issue_followup:\n[\s\S]*?(?=\n publish_issue_followup:)/)?.[0] || ''; + assert.match(classify, /actions: read/); + assert.doesNotMatch(classify, /actions: write/); + assert.match(followup, /actions: write/); + assert.match(followup, /createWorkflowDispatch/); assert.match(workflow, /cursor-codex-clean-head:/); assert.match(workflow, /Clean handoff already recorded/); });