diff --git a/scripts/claim/claim.js b/scripts/claim/claim.js index 04c79e1b9..d9b2ffd45 100644 --- a/scripts/claim/claim.js +++ b/scripts/claim/claim.js @@ -8,6 +8,7 @@ const LABEL_DESCRIPTION = 'Someone is working on this'; const CLAIM_HOURS = 72; const PR_IDLE_DAYS = 14; const HOUR = 3600 * 1000; +const BOT = 'github-actions[bot]'; function parseCommand(body) { const text = String(body || '').trimStart(); @@ -96,6 +97,19 @@ function linkedPulls(timeline, context, number) { return [...seen.values()].sort((a, b) => a.number - b.number); } +// Who the bot's latest "Claimed by @user" reply names, unless the label came off after it. +// GitHub doesn't assign people who aren't collaborators, so for them this reply is the only record. +function recordedClaimant(timeline) { + let who = null; + for (const ev of timeline) { + if (ev.event === 'unlabeled' && ev.label && ev.label.name === LABEL) who = null; + const mine = ev.event === 'commented' && ev.user && ev.user.login === BOT; + const m = mine && /^Claimed by @([A-Za-z0-9-]+)/.exec(ev.body || ''); + if (m) who = m[1]; + } + return who; +} + async function lastPush(github, context, pr) { const { data } = await github.rest.pulls.get({ ...repoArgs(context), pull_number: pr.number }); const { data: commit } = await github.rest.repos.getCommit({ @@ -234,7 +248,10 @@ async function unclaim(github, context) { await react(github, context, 'eyes'); // only the owner releases their own issues return; } - const allowed = assignees.includes(user) || (await isMaintainer(github, context, user)); + let allowed = assignees.includes(user) || (await isMaintainer(github, context, user)); + if (!allowed && assignees.length === 0) { + allowed = recordedClaimant(await getTimeline(github, context, issue.number)) === user; + } if (!allowed) { await react(github, context, 'eyes'); await reply(github, context, `Only the person who claimed this, or a maintainer, can release it, @${user}.`); diff --git a/scripts/claim/claim.test.js b/scripts/claim/claim.test.js index e0803fce6..cd77d1195 100644 --- a/scripts/claim/claim.test.js +++ b/scripts/claim/claim.test.js @@ -54,6 +54,7 @@ function fake(world = {}) { const names = (calls) => calls.map((c) => c[0]); const find = (calls, n) => calls.find((c) => c[0] === n); const labeled = (at) => ({ event: 'labeled', label: { name: 'in progress' }, created_at: at }); +const said = (body, login = 'github-actions[bot]') => ({ event: 'commented', body, user: { login } }); const pr = (number, login = 'other', state = 'open', extra = {}) => ({ event: 'cross-referenced', source: { issue: { @@ -188,6 +189,38 @@ test('unclaim by a maintainer releases, by a stranger does not', async () => { assert.equal(find(w.calls, 'react')[1].content, 'eyes'); }); +test('unclaim by a claimant the bot could not assign releases, by a stranger does not', async () => { + const timeline = [labeled(ago(5 * H)), said('Claimed by @alice through #50. The claim lapses after 14 days without pushes.')]; + let w = fake({ timeline }); + await claim.handleComment({ github: w.github, context: ctx('/unclaim', { labels: ['in progress'] }), now: NOW }); + assert.equal(find(w.calls, 'removeLabel')[1].name, 'in progress'); + assert.equal(find(w.calls, 'removeAssignees'), undefined); + assert.equal(find(w.calls, 'react')[1].content, '+1'); + w = fake({ timeline }); + await claim.handleComment({ github: w.github, context: ctx('/unclaim', { user: 'mallory', labels: ['in progress'] }), now: NOW }); + assert.equal(find(w.calls, 'removeLabel'), undefined); + assert.match(find(w.calls, 'comment')[1].body, /Only the person who claimed this/); + w = fake({ timeline, perms: { carol: 'write' } }); + await claim.handleComment({ github: w.github, context: ctx('/unclaim', { user: 'carol', labels: ['in progress'] }), now: NOW }); + assert.ok(find(w.calls, 'removeLabel')); +}); + +test('only the bot\'s latest claim reply, before any release or assignment, names the claimant', async () => { + const cases = [ + [said('Claimed by @mallory.', 'mallory')], // not the bot + [said('Claimed by @mallory.'), { event: 'unlabeled', label: { name: 'in progress' } }, labeled(ago(H))], // released since + [said('Claimed by @mallory.'), said('Claimed by @bob.')], // taken over + ]; + for (const timeline of cases) { + const w = fake({ timeline }); + await claim.handleComment({ github: w.github, context: ctx('/unclaim', { user: 'mallory', labels: ['in progress'] }), now: NOW }); + assert.equal(find(w.calls, 'removeLabel'), undefined); + } + const w = fake({ timeline: [said('Claimed by @mallory.')] }); // someone is assigned since + await claim.handleComment({ github: w.github, context: ctx('/unclaim', { user: 'mallory', assignees: ['bob'], labels: ['in progress'] }), now: NOW }); + assert.equal(find(w.calls, 'removeLabel'), undefined); +}); + test('unclaim on an owner-assigned issue works only for the owner', async () => { let w = fake(); await claim.handleComment({ github: w.github, context: ctx('/unclaim', { user: 'carol', assignees: ['bherbruck'], labels: ['in progress'] }), now: NOW });