diff --git a/.github/workflows/contributor-pr-reply.yml b/.github/workflows/contributor-pr-reply.yml index cb22b75..0a9a5ae 100644 --- a/.github/workflows/contributor-pr-reply.yml +++ b/.github/workflows/contributor-pr-reply.yml @@ -1,4 +1,4 @@ -name: Send reply on a new contributor pull request +name: Link issues and reply on a new pull request on: workflow_call: secrets: @@ -24,9 +24,8 @@ jobs: username: ${{ github.event.pull_request.user.login }} author_association: ${{ github.event.pull_request.author_association }} send-reply: - name: Send reply + name: Link issues and send reply needs: [check-if-contributor] - if: ${{ needs.check-if-contributor.outputs.is_contributor == 'true' }} runs-on: ubuntu-latest concurrency: group: contributor-pr-reply-${{ github.event.pull_request.number }}-${{ github.event.action }} @@ -54,11 +53,14 @@ jobs: - name: Run script id: script uses: actions/github-script@v9 + env: + IS_CONTRIBUTOR: ${{ needs.check-if-contributor.outputs.is_contributor }} with: github-token: ${{ steps.generate-token.outputs.token }} script: | const script = require('./scripts/contributor-pr-reply.js'); - return await script({ github, context, core }); + const contributor = process.env.IS_CONTRIBUTOR === 'true'; + return await script({ github, context, core, contributor }); - name: Send Slack notification if: ${{ !cancelled() && steps.script.outputs.slack_notification != '' }} uses: slackapi/slack-github-action@v4.0.0 diff --git a/docs/community-automations.md b/docs/community-automations.md index 4f2c1f9..620c973 100644 --- a/docs/community-automations.md +++ b/docs/community-automations.md @@ -79,6 +79,8 @@ When nothing is linked, the automation reads the description for open issues in - otherwise, the one mentioned issue assigned to the author; the reply names it - several mentioned issues assigned to the author link none; the reply names them and asks which one +For everyone else, including maintainers and bots, it only applies the first rule: off the default branch, with nothing linked yet, it links every issue after a closing keyword. Nothing is replied, labelled, or closed. + It runs on `opened`, and again on `edited` when the description changes. An edit sends no reply: it closes, labels, or removes `unlinked` by the rules above, and starts review only on a pull request that carried `unlinked`. The weekly `unassign-inactive-issues` run closes every open pull request labelled `unlinked` for longer than `PR_UNLINKED_MAX_DAYS` days, except a close contributor's. It re-reads the links first, because a link added from the Development sidebar fires no `edited` event: a pull request now linked to its author's issue loses the label and starts review instead. diff --git a/docs/testing-automations.md b/docs/testing-automations.md index 2b4679d..dffecce 100644 --- a/docs/testing-automations.md +++ b/docs/testing-automations.md @@ -18,8 +18,9 @@ the `if:` condition on each job did not match, so check the event first. Some automations run only when `is-contributor` is true, meaning the author is not a bot, an org owner, or in a `MAINTAINER_TEAMS` team (see `scripts/constants.js`). A maintainer cannot trigger -them, so they skip. These are `review-requested`, `pull-request-label`, `contributor-pr-reply`, and -`contributor-issue-comment`, plus `holiday-message` when enabled. +them, so they skip. These are `review-requested`, `pull-request-label`, `contributor-pr-reply` +(except linking off the default branch), and `contributor-issue-comment`, plus `holiday-message` +when enabled. Use a second GitHub account that is not in the organization. It needs no permissions, secrets, or app. To open a pull request, fork `test-actions` and open one back to it. `pull_request_target` then diff --git a/scripts/contributor-pr-reply.js b/scripts/contributor-pr-reply.js index e1774b9..300cd21 100644 --- a/scripts/contributor-pr-reply.js +++ b/scripts/contributor-pr-reply.js @@ -5,7 +5,13 @@ const { BOT_MESSAGE_LINK_WHICH_ISSUE, } = require('./constants'); const { sendBotMessage, getLinkedIssues, escapeIssueTitleForSlackMessage } = require('./utils'); -const { linkReferences, linkStatus, planLink, applyLink } = require('./pull-request-link'); +const { + isDefaultBase, + linkReferences, + linkStatus, + planLink, + applyLink, +} = require('./pull-request-link'); function slackLine(context, text) { const { repo } = context.repo; @@ -54,7 +60,16 @@ async function act({ github, context, core }, plan, link) { } } -module.exports = async ({ github, context, core }) => { +async function linkClosingReferences({ github, context, core }) { + const { owner, repo } = context.repo; + const pullRequest = context.payload.pull_request; + if (isDefaultBase(pullRequest)) return; + const lookup = await getLinkedIssues(pullRequest.number, { github, context, core }); + if (lookup === null || lookup.length) return; + await linkReferences(owner, repo, pullRequest, { github, core }, { contributor: false }); +} + +module.exports = async ({ github, context, core, contributor }) => { try { const { owner, repo } = context.repo; const pullRequest = context.payload.pull_request; @@ -68,6 +83,11 @@ module.exports = async ({ github, context, core }) => { return; } + if (!contributor) { + await linkClosingReferences({ github, context, core }); + return; + } + const plan = async link => planLink(owner, repo, pullRequest, link, { github, core }, { opening }); @@ -81,7 +101,7 @@ module.exports = async ({ github, context, core }) => { const found = lookup.length ? { linked: lookup, named: null, ambiguous: [] } - : await linkReferences(owner, repo, pullRequest, { github, core }); + : await linkReferences(owner, repo, pullRequest, { github, core }, { contributor: true }); const link = linkStatus(found.linked, author); const first = await plan(link); if (opening && first.action !== 'close') { diff --git a/scripts/contributor-pr-reply.test.js b/scripts/contributor-pr-reply.test.js index 656d418..fd46ff8 100644 --- a/scripts/contributor-pr-reply.test.js +++ b/scripts/contributor-pr-reply.test.js @@ -171,13 +171,15 @@ function fakeGithub( }; } -async function run(github, context = fakeContext()) { +async function run(github, context = fakeContext(), { contributor = true } = {}) { context.payload.pull_request.labels.forEach(({ name }) => github.current.add(name)); const core = fakeCore(); - await contributorPrReply({ github, context, core }); + await contributorPrReply({ github, context, core, contributor }); return core; } +const asMaintainer = (github, context) => run(github, context, { contributor: false }); + test('a linked issue assigned to someone else closes the pull request', async () => { const github = fakeGithub([{ number: 7, assignees: ['someone-else'] }]); const core = await run(github); @@ -546,3 +548,49 @@ test('an edit mentioning the issue assigned to the author links it and starts re assert.deepEqual(github.calls.removed, [LABEL_UNLINKED]); assert.deepEqual(github.calls.reviewers, [RTIBBLESBOT_USERNAME]); }); + +test("a maintainer's closing keyword on another base links the issue and nothing else", async () => { + const github = fakeGithub([], { referenced: { 7: { assignees: ['someone-else'] } } }); + const core = await asMaintainer(github, describing('Fixes #7', { base: 'release-v0.20.x' })); + + assert.deepEqual(github.calls.links, [{ issueId: 'I_7', pullRequestIds: ['PR_42'] }]); + assert.deepEqual(github.calls.comments, []); + assert.deepEqual(github.calls.updates, []); + assert.deepEqual(github.calls.labels, []); + assert.deepEqual(github.calls.reviewers, []); + assert.equal(core.outputs.slack_notification, ''); + assert.deepEqual(core.failures, []); +}); + +test("a maintainer's edit adding a closing keyword on another base links the issue", async () => { + const github = fakeGithub([], { referenced: { 7: { assignees: [] } } }); + await asMaintainer(github, edited(AUTHOR, { body: 'Closes #7', base: 'release-v0.20.x' })); + + assert.equal(github.calls.links.length, 1); + assert.deepEqual(github.calls.updates, []); +}); + +test("a maintainer's mention without a closing keyword is not linked", async () => { + const github = fakeGithub([], { referenced: { 7: { assignees: [AUTHOR] } } }); + await asMaintainer(github, describing('See #7', { base: 'release-v0.20.x' })); + + assert.deepEqual(github.calls.links, []); + assert.deepEqual(github.calls.labels, []); +}); + +test("a maintainer's pull request that already links an issue links no more", async () => { + const github = fakeGithub([{ number: 8, assignees: [] }], { + referenced: { 7: { assignees: [] } }, + }); + await asMaintainer(github, describing('Fixes #7', { base: 'release-v0.20.x' })); + + assert.deepEqual(github.calls.links, []); +}); + +test("a maintainer's pull request to the default branch makes no lookup", async () => { + const github = fakeGithub([], { graphqlError: 'unexpected lookup' }); + const core = await asMaintainer(github, describing('Fixes #7')); + + assert.deepEqual(core.warnings, []); + assert.deepEqual(github.calls.comments, []); +}); diff --git a/scripts/pull-request-link.js b/scripts/pull-request-link.js index 9fa6aa4..de391fe 100644 --- a/scripts/pull-request-link.js +++ b/scripts/pull-request-link.js @@ -14,20 +14,28 @@ const { addClosingIssueReference, } = require('./utils'); -function chooseReferences(issues, author, defaultBase) { +function isDefaultBase(pullRequest) { + return pullRequest.base.ref === pullRequest.base.repo.default_branch; +} + +function chooseReferences(issues, author, defaultBase, contributor) { const closing = defaultBase ? [] : issues.filter(issue => issue.keyword); - if (closing.length) return { link: closing, named: false, ambiguous: [] }; + if (closing.length || !contributor) return { link: closing, named: false, ambiguous: [] }; const assigned = issues.filter(issue => issue.assignees.includes(author)); if (assigned.length === 1) return { link: assigned, named: true, ambiguous: [] }; return { link: [], named: false, ambiguous: assigned.length > 1 ? assigned : [] }; } -async function linkReferences(owner, repo, pullRequest, { github, core }) { +async function linkReferences(owner, repo, pullRequest, { github, core }, { contributor }) { const references = findIssueReferences(pullRequest.body, owner, repo); if (!references.length) return { linked: [], named: null, ambiguous: [] }; const issues = await getReferencedIssues(owner, repo, references, { github, core }); - const defaultBase = pullRequest.base.ref === pullRequest.base.repo.default_branch; - const { link, named, ambiguous } = chooseReferences(issues, pullRequest.user.login, defaultBase); + const { link, named, ambiguous } = chooseReferences( + issues, + pullRequest.user.login, + isDefaultBase(pullRequest), + contributor, + ); const linked = []; for (const issue of link) { try { @@ -119,4 +127,4 @@ async function applyLink(owner, repo, number, plan, { github, core }) { return null; } -module.exports = { linkReferences, linkStatus, planLink, applyLink }; +module.exports = { isDefaultBase, linkReferences, linkStatus, planLink, applyLink };