From 00dcb5ec10c95b3c3e93ba3ca512fe3af4e5b014 Mon Sep 17 00:00:00 2001 From: Turtle Date: Mon, 16 Nov 2026 15:56:40 +0800 Subject: [PATCH] fix(ci): close stale pull requests before commenting --- .github/stale-pull-requests/remind.mjs | 9 +++------ .github/stale-pull-requests/remind.test.mjs | 18 +++++++++++++----- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/.github/stale-pull-requests/remind.mjs b/.github/stale-pull-requests/remind.mjs index 0b863dc865..2545ec49c2 100644 --- a/.github/stale-pull-requests/remind.mjs +++ b/.github/stale-pull-requests/remind.mjs @@ -121,14 +121,11 @@ export async function processStalePullRequests({ github, context, core, now = ne const days = dueStage({ createdAt: pull.created_at, timeline, now }) if (days === undefined) continue if (posted > 0) await sleep(COMMENT_INTERVAL_MS) + // Closing first keeps a failed close from repeating the closing comment on later runs. + if (days === CLOSE_DAYS) await github.rest.pulls.update({ owner, repo, pull_number: pull.number, state: 'closed' }) await github.rest.issues.createComment({ owner, repo, issue_number: pull.number, body: stageBody(pull.user.login, days) }) posted++ - if (days === CLOSE_DAYS) { - await github.rest.pulls.update({ owner, repo, pull_number: pull.number, state: 'closed' }) - core.info(`#${pull.number}: closed after ${days} days without activity`) - } else { - core.info(`#${pull.number}: posted the ${days}-day reminder`) - } + core.info(days === CLOSE_DAYS ? `#${pull.number}: closed after ${days} days without activity` : `#${pull.number}: posted the ${days}-day reminder`) } catch (error) { failed++ core.warning(`#${pull.number}: ${error.message}`) diff --git a/.github/stale-pull-requests/remind.test.mjs b/.github/stale-pull-requests/remind.test.mjs index be430e6576..b8573b7c70 100644 --- a/.github/stale-pull-requests/remind.test.mjs +++ b/.github/stale-pull-requests/remind.test.mjs @@ -63,13 +63,20 @@ test('reminders and closing mention the author and skip exempt and bot-authored pull(6), pull(5), pull(7, { created_at: daysAgo(30) }), + pull(8, { created_at: daysAgo(30) }), ] const comments = [] const closed = [] const timelines = [] const github = { rest: { - pulls: { list: 'pulls', update: async params => closed.push(params) }, + pulls: { + list: 'pulls', + update: async params => { + if (params.pull_number === 8) throw new Error('close failed') + closed.push(params) + }, + }, issues: { listEventsForTimeline: 'timeline', createComment: async params => { @@ -81,7 +88,7 @@ test('reminders and closing mention the author and skip exempt and bot-authored paginate: async (method, params) => { if (method === 'pulls') return pulls timelines.push(params.issue_number) - return params.issue_number === 7 ? [reminder(9, 14)] : [] + return [7, 8].includes(params.issue_number) ? [reminder(9, 14)] : [] }, } const sleeps = [] @@ -89,14 +96,15 @@ test('reminders and closing mention the author and skip exempt and bot-authored const failures = [] const core = { info() {}, warning: message => warnings.push(message), setFailed: message => failures.push(message) } await processStalePullRequests({ github, context: { repo: { owner: 'o', repo: 'r' } }, core, now, sleep: async ms => sleeps.push(ms) }) - assert.deepEqual(timelines, [1, 4, 6, 5, 7]) - assert.deepEqual(warnings, ['#6: locked']) + assert.deepEqual(timelines, [1, 4, 6, 5, 7, 8]) + // A failed close posts no closing comment, so the next run retries without repeating it. + assert.deepEqual(warnings, ['#6: locked', '#8: close failed']) assert.equal(failures.length, 1) assert.deepEqual(comments.map(({ issue_number }) => issue_number), [1, 4, 5, 7]) assert.match(comments[0].body, /^\n@author This pull request has had no activity for 14 days\./) assert.match(comments[3].body, /^\n@author This pull request has had no activity for 21 days and is now closed\./) assert.deepEqual(closed, [{ owner: 'o', repo: 'r', pull_number: 7, state: 'closed' }]) - assert.deepEqual(sleeps, [1000, 1000, 1000, 1000]) + assert.deepEqual(sleeps, [1000, 1000, 1000, 1000, 1000]) }) test('a run posts at most 100 reminders', async () => {