diff --git a/scripts/claim/claim.js b/scripts/claim/claim.js index ef9c366dd..04c79e1b9 100644 --- a/scripts/claim/claim.js +++ b/scripts/claim/claim.js @@ -79,14 +79,21 @@ function getTimeline(github, context, number) { }); } -// Open pull requests that mention the issue, from the timeline's cross-references. -function linkedPulls(timeline) { +// Cross-references discover candidates, not claims: even a comment or a "Not in this PR" +// mention creates one. Count only open PRs in this repo whose title/body actually claim the +// issue, using the same rule as handlePull. Timeline order is not PR-number order. +function linkedPulls(timeline, context, number) { + const repo = `${context.repo.owner}/${context.repo.repo}`.toLowerCase(); const seen = new Map(); for (const ev of timeline) { const src = ev.event === 'cross-referenced' && ev.source && ev.source.issue; - if (src && src.pull_request && src.state === 'open') seen.set(src.number, src); + if (src && src.pull_request && src.state === 'open' && + src.repository?.full_name?.toLowerCase() === repo && + referencedIssues(src.title, src.body).includes(number)) { + seen.set(src.number, src); + } } - return [...seen.values()]; + return [...seen.values()].sort((a, b) => a.number - b.number); } async function lastPush(github, context, pr) { @@ -102,7 +109,7 @@ async function lastPush(github, context, pr) { async function claimStatus(github, context, issue, now) { const timeline = await getTimeline(github, context, issue.number); const since = await labeledAt(github, context, issue, timeline); - const pulls = linkedPulls(timeline); + const pulls = linkedPulls(timeline, context, issue.number); if (pulls.length === 0) { const expires = since + CLAIM_HOURS * HOUR; return { pulls, since, expires, release: now >= expires, reason: 'no PR' }; @@ -184,7 +191,7 @@ async function claim(github, context, now) { } else { // Not claimed, but a draft PR may already be the claim (CONTRIBUTING.md, "Claim = a draft PR"). const timeline = await getTimeline(github, context, n); - const other = linkedPulls(timeline).find((p) => p.user && p.user.login !== user); + const other = linkedPulls(timeline, context, n).find((p) => p.user && p.user.login !== user); if (other) { await react(github, context, 'eyes'); await reply(github, context, `#${other.number} already covers this one.`); @@ -298,7 +305,7 @@ async function handlePull({ github, context }) { } // An older open PR for the issue wins (lowest number), even if it never recorded its claim. const timeline = await getTimeline(github, context, n); - const older = linkedPulls(timeline).find((p) => p.number < pull.number && p.user && p.user.login !== author); + const older = linkedPulls(timeline, context, n).find((p) => p.number < pull.number && p.user && p.user.login !== author); if (older) { await github.rest.issues.createComment({ ...repoArgs(context), diff --git a/scripts/claim/claim.test.js b/scripts/claim/claim.test.js index 424e0178d..e0803fce6 100644 --- a/scripts/claim/claim.test.js +++ b/scripts/claim/claim.test.js @@ -54,9 +54,13 @@ 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 pr = (number, login = 'other', state = 'open') => ({ +const pr = (number, login = 'other', state = 'open', extra = {}) => ({ event: 'cross-referenced', - source: { issue: { number, state, pull_request: {}, user: { login } } }, + source: { issue: { + number, state, pull_request: {}, user: { login }, + title: 'Work (#7)', body: '', repository: { full_name: 'bherbruck/solidcraft' }, + ...extra, + } }, }); function ctx(body, { assignees = [], labels = [], user = 'alice', extra = {} } = {}) { @@ -146,6 +150,15 @@ test('an unclaimed issue with someone else\'s open PR points at the PR', async ( assert.match(find(calls, 'comment')[1].body, /#42/); }); +test('a body cross-reference does not block a comment claim', async () => { + const { github, calls } = fake({ + timeline: [pr(42, 'bob', 'open', { title: 'Another fix (#8)', body: 'Not in this PR: #7.' })], + }); + await claim.handleComment({ github, context: ctx('/claim'), now: NOW }); + assert.deepEqual(find(calls, 'addAssignees')?.[1].assignees, ['alice']); + assert.match(find(calls, 'comment')[1].body, /^Claimed by @alice/); +}); + test('bots, locked issues, closed issues, PRs and other comments are ignored', async () => { const { github, calls } = fake(); const bot = ctx('/claim'); @@ -219,6 +232,16 @@ test('sweep ignores a closed PR', async () => { assert.deepEqual(await claim.sweep({ github: w.github, context: { repo }, now: NOW }), [7]); }); +test('an incidental PR mention does not extend a claim past 72 hours', async () => { + const w = fake({ + issues: [issue(7)], + timeline: [labeled(ago(80 * H)), pr(9, 'bob', 'open', { title: 'Other work', body: 'See #7.' })], + pushed: ago(H), + }); + assert.deepEqual(await claim.sweep({ github: w.github, context: { repo }, now: NOW }), [7]); + assert.match(find(w.calls, 'comment')[1].body, /No draft PR showed up within 72 h/); +}); + test('sweep never touches owner- or maintainer-claimed issues', async () => { const w = fake({ issues: [issue(1, ['bherbruck']), issue(2, ['carol']), issue(3, ['bob', 'carol'])], @@ -274,11 +297,49 @@ test('a PR on an issue someone else holds comments on the PR and changes nothing }); test('the lowest-numbered open PR wins an unrecorded claim', async () => { - const { github, calls } = fake({ byNumber: { 7: openIssue(7) }, timeline: [pr(42, 'bob')] }); + const { github, calls } = fake({ + byNumber: { 7: openIssue(7) }, + timeline: [pr(45, 'carol'), pr(42, 'bob')], + }); assert.deepEqual(await claim.handlePull({ github, context: pullCtx(50, 'Thing (#7)') }), []); assert.match(find(calls, 'comment')[1].body, /#42 claimed #7 first/); }); +test('a PR mentioning an issue outside its claim does not block the actual fix', async () => { + const { github, calls } = fake({ + byNumber: { 6608: openIssue(6608) }, + timeline: [pr(6381, 'bob', 'open', { + title: 'Cylinder reference edges (#6380)', + body: 'Closes #6380.\n\n## Not in this PR\n\nCanvas value boxes are tracked by #6608.', + })], + }); + assert.deepEqual(await claim.handlePull({ github, context: pullCtx(6661, 'Canvas boxes (#6608)') }), [6608]); + assert.deepEqual(find(calls, 'addAssignees')[1].assignees, ['alice']); + assert.equal(find(calls, 'comment')[1].issue_number, 6608); +}); + +test('a closing keyword in the older PR body still reserves the issue', async () => { + const { github, calls } = fake({ + byNumber: { 7: openIssue(7) }, + timeline: [pr(42, 'bob', 'open', { title: 'Earlier fix', body: 'Resolves #7.' })], + }); + assert.deepEqual(await claim.handlePull({ github, context: pullCtx(50, 'Thing (#7)') }), []); + assert.match(find(calls, 'comment')[1].body, /#42 claimed #7 first/); + assert.equal(find(calls, 'addAssignees'), undefined); +}); + +test('a PR in another repository cannot reserve a local issue with the same number', async () => { + const { github, calls } = fake({ + byNumber: { 7: openIssue(7) }, + timeline: [pr(42, 'bob', 'open', { + repository: { full_name: 'someone/other-project' }, + body: 'Fixes #7. Related: bherbruck/solidcraft#7.', + })], + }); + assert.deepEqual(await claim.handlePull({ github, context: pullCtx(50, 'Thing (#7)') }), [7]); + assert.deepEqual(find(calls, 'addAssignees')[1].assignees, ['alice']); +}); + test('the claimant\'s own PR just makes sure the label is there', async () => { const { github, calls } = fake({ byNumber: { 7: openIssue(7, ['alice']) } }); assert.deepEqual(await claim.handlePull({ github, context: pullCtx(50, 'Thing (#7)') }), []);