|
| 1 | +const test = require('node:test'); |
| 2 | +const assert = require('node:assert/strict'); |
| 3 | + |
| 4 | +const contributorPrReply = require('./contributor-pr-reply'); |
| 5 | +const { LABEL_COMMUNITY_REVIEW, RTIBBLESBOT_USERNAME } = require('./constants'); |
| 6 | + |
| 7 | +const AUTHOR = 'outsider'; |
| 8 | + |
| 9 | +function fakeContext(author = AUTHOR) { |
| 10 | + return { |
| 11 | + repo: { owner: 'learningequality', repo: 'kolibri' }, |
| 12 | + payload: { |
| 13 | + pull_request: { |
| 14 | + number: 42, |
| 15 | + html_url: 'https://github.com/learningequality/kolibri/pull/42', |
| 16 | + title: 'Fix it', |
| 17 | + user: { login: author }, |
| 18 | + }, |
| 19 | + }, |
| 20 | + }; |
| 21 | +} |
| 22 | + |
| 23 | +function fakeCore() { |
| 24 | + const outputs = {}; |
| 25 | + const warnings = []; |
| 26 | + const failures = []; |
| 27 | + return { |
| 28 | + outputs, |
| 29 | + warnings, |
| 30 | + failures, |
| 31 | + info() {}, |
| 32 | + warning(message) { |
| 33 | + warnings.push(message); |
| 34 | + }, |
| 35 | + setFailed(message) { |
| 36 | + failures.push(message); |
| 37 | + }, |
| 38 | + setOutput(name, value) { |
| 39 | + outputs[name] = value; |
| 40 | + }, |
| 41 | + }; |
| 42 | +} |
| 43 | + |
| 44 | +function fakeGithub(linkedIssues = [], { graphqlError } = {}) { |
| 45 | + const calls = { comments: [], updates: [], reviewers: [], labels: [], order: [] }; |
| 46 | + return { |
| 47 | + calls, |
| 48 | + graphql: async () => { |
| 49 | + if (graphqlError) { |
| 50 | + throw new Error(graphqlError); |
| 51 | + } |
| 52 | + return { |
| 53 | + repository: { |
| 54 | + pullRequest: { |
| 55 | + closingIssuesReferences: { |
| 56 | + nodes: linkedIssues.map(issue => ({ |
| 57 | + number: issue.number, |
| 58 | + title: `Issue ${issue.number}`, |
| 59 | + url: `https://github.com/learningequality/kolibri/issues/${issue.number}`, |
| 60 | + assignees: { nodes: issue.assignees.map(login => ({ login })) }, |
| 61 | + })), |
| 62 | + }, |
| 63 | + }, |
| 64 | + }, |
| 65 | + }; |
| 66 | + }, |
| 67 | + rest: { |
| 68 | + issues: { |
| 69 | + createComment: async ({ issue_number, body }) => { |
| 70 | + calls.comments.push({ issue_number, body }); |
| 71 | + calls.order.push('comment'); |
| 72 | + return { data: { html_url: 'https://github.com/learningequality/kolibri/pull/42#c1' } }; |
| 73 | + }, |
| 74 | + addLabels: async ({ labels }) => { |
| 75 | + calls.labels.push(...labels); |
| 76 | + calls.order.push('label'); |
| 77 | + }, |
| 78 | + }, |
| 79 | + pulls: { |
| 80 | + update: async ({ pull_number, state }) => { |
| 81 | + calls.updates.push({ pull_number, state }); |
| 82 | + calls.order.push('close'); |
| 83 | + }, |
| 84 | + requestReviewers: async ({ reviewers }) => { |
| 85 | + calls.reviewers.push(...reviewers); |
| 86 | + calls.order.push('review'); |
| 87 | + }, |
| 88 | + }, |
| 89 | + }, |
| 90 | + }; |
| 91 | +} |
| 92 | + |
| 93 | +async function run(github, context = fakeContext()) { |
| 94 | + const core = fakeCore(); |
| 95 | + await contributorPrReply({ github, context, core }); |
| 96 | + return core; |
| 97 | +} |
| 98 | + |
| 99 | +test('a linked issue assigned to someone else closes the pull request', async () => { |
| 100 | + const github = fakeGithub([{ number: 7, assignees: ['someone-else'] }]); |
| 101 | + const core = await run(github); |
| 102 | + |
| 103 | + assert.deepEqual(github.calls.updates, [{ pull_number: 42, state: 'closed' }]); |
| 104 | + assert.equal(github.calls.comments.length, 1); |
| 105 | + assert.match(github.calls.comments[0].body, /issues\/7 is assigned to someone else/); |
| 106 | + assert.match(core.outputs.slack_notification, /Closed/); |
| 107 | + assert.deepEqual(github.calls.reviewers, []); |
| 108 | + assert.deepEqual(github.calls.labels, []); |
| 109 | + assert.deepEqual(core.failures, []); |
| 110 | +}); |
| 111 | + |
| 112 | +test('the closing message replaces the standard reply', async () => { |
| 113 | + const github = fakeGithub([{ number: 7, assignees: ['someone-else'] }]); |
| 114 | + await run(github); |
| 115 | + |
| 116 | + assert.doesNotMatch(github.calls.comments[0].body, /For the review process to begin/); |
| 117 | +}); |
| 118 | + |
| 119 | +test('a failed close still reports to Slack', async () => { |
| 120 | + const github = fakeGithub([{ number: 7, assignees: ['someone-else'] }]); |
| 121 | + github.rest.pulls.update = async () => { |
| 122 | + throw new Error('Resource not accessible'); |
| 123 | + }; |
| 124 | + const core = await run(github); |
| 125 | + |
| 126 | + assert.equal(github.calls.comments.length, 1); |
| 127 | + assert.match(core.outputs.slack_notification, /Failed to close/); |
| 128 | + assert.equal(core.failures.length, 1); |
| 129 | +}); |
| 130 | + |
| 131 | +test('a cross-repo linked issue is named by its url', async () => { |
| 132 | + const github = fakeGithub([{ number: 7, assignees: ['someone-else'] }]); |
| 133 | + const core = await run(github); |
| 134 | + |
| 135 | + assert.match(github.calls.comments[0].body, /https:\/\/github\.com\/.+\/issues\/7/); |
| 136 | + assert.match(core.outputs.slack_notification, /issues\/7/); |
| 137 | +}); |
| 138 | + |
| 139 | +test('a linked issue assigned to the author requests review and adds the label', async () => { |
| 140 | + const github = fakeGithub([{ number: 7, assignees: [AUTHOR] }]); |
| 141 | + const core = await run(github); |
| 142 | + |
| 143 | + assert.deepEqual(github.calls.reviewers, [RTIBBLESBOT_USERNAME]); |
| 144 | + assert.deepEqual(github.calls.labels, [LABEL_COMMUNITY_REVIEW]); |
| 145 | + assert.deepEqual(github.calls.updates, []); |
| 146 | + assert.match(github.calls.comments[0].body, /For the review process to begin/); |
| 147 | + assert.match(core.outputs.slack_notification, /Reply sent/); |
| 148 | +}); |
| 149 | + |
| 150 | +test('the reply is sent before the review request and the label', async () => { |
| 151 | + const github = fakeGithub([{ number: 7, assignees: [AUTHOR] }]); |
| 152 | + await run(github); |
| 153 | + |
| 154 | + assert.deepEqual(github.calls.order, ['comment', 'review', 'label']); |
| 155 | +}); |
| 156 | + |
| 157 | +test('a failed review request still leaves the reply in place', async () => { |
| 158 | + const github = fakeGithub([{ number: 7, assignees: [AUTHOR] }]); |
| 159 | + github.rest.pulls.requestReviewers = async () => { |
| 160 | + throw new Error('Reviewer cannot be requested'); |
| 161 | + }; |
| 162 | + const core = await run(github); |
| 163 | + |
| 164 | + assert.equal(github.calls.comments.length, 1); |
| 165 | + assert.equal(core.failures.length, 1); |
| 166 | + assert.match(core.outputs.slack_notification, /Reply sent/); |
| 167 | +}); |
| 168 | + |
| 169 | +test('the author counts as assigned alongside other assignees', async () => { |
| 170 | + const github = fakeGithub([{ number: 7, assignees: ['someone-else', AUTHOR] }]); |
| 171 | + await run(github); |
| 172 | + |
| 173 | + assert.deepEqual(github.calls.updates, []); |
| 174 | + assert.deepEqual(github.calls.reviewers, [RTIBBLESBOT_USERNAME]); |
| 175 | +}); |
| 176 | + |
| 177 | +test('an issue assigned to the author outweighs another assigned elsewhere', async () => { |
| 178 | + const github = fakeGithub([ |
| 179 | + { number: 7, assignees: ['someone-else'] }, |
| 180 | + { number: 8, assignees: [AUTHOR] }, |
| 181 | + ]); |
| 182 | + await run(github); |
| 183 | + |
| 184 | + assert.deepEqual(github.calls.updates, []); |
| 185 | + assert.deepEqual(github.calls.reviewers, [RTIBBLESBOT_USERNAME]); |
| 186 | +}); |
| 187 | + |
| 188 | +test('no linked issue leaves the pull request alone', async () => { |
| 189 | + const github = fakeGithub([]); |
| 190 | + const core = await run(github); |
| 191 | + |
| 192 | + assert.deepEqual(github.calls.updates, []); |
| 193 | + assert.deepEqual(github.calls.reviewers, []); |
| 194 | + assert.deepEqual(github.calls.labels, []); |
| 195 | + assert.equal(github.calls.comments.length, 1); |
| 196 | + assert.match(core.outputs.slack_notification, /Reply sent/); |
| 197 | +}); |
| 198 | + |
| 199 | +test('a linked issue with no assignee leaves the pull request alone', async () => { |
| 200 | + const github = fakeGithub([{ number: 7, assignees: [] }]); |
| 201 | + await run(github); |
| 202 | + |
| 203 | + assert.deepEqual(github.calls.updates, []); |
| 204 | + assert.deepEqual(github.calls.reviewers, []); |
| 205 | + assert.deepEqual(github.calls.labels, []); |
| 206 | +}); |
| 207 | + |
| 208 | +test('an unlinked pull request is asked to link an issue', async () => { |
| 209 | + const github = fakeGithub([]); |
| 210 | + await run(github); |
| 211 | + |
| 212 | + assert.match(github.calls.comments[0].body, /link one under \*\*References\*\*/); |
| 213 | +}); |
| 214 | + |
| 215 | +test('a linked pull request is not asked to link an issue', async () => { |
| 216 | + const github = fakeGithub([{ number: 7, assignees: [] }]); |
| 217 | + await run(github); |
| 218 | + |
| 219 | + assert.doesNotMatch(github.calls.comments[0].body, /link one under/); |
| 220 | +}); |
| 221 | + |
| 222 | +test('a failed lookup falls back to the standard reply', async () => { |
| 223 | + const github = fakeGithub([], { graphqlError: 'API down' }); |
| 224 | + const core = await run(github); |
| 225 | + |
| 226 | + assert.deepEqual(github.calls.updates, []); |
| 227 | + assert.deepEqual(github.calls.reviewers, []); |
| 228 | + assert.equal(github.calls.comments.length, 1); |
| 229 | + assert.equal(core.warnings.length, 1); |
| 230 | + assert.match(core.warnings[0], /API down/); |
| 231 | + assert.deepEqual(core.failures, []); |
| 232 | +}); |
| 233 | + |
| 234 | +test('a failed lookup does not ask the author to link an issue', async () => { |
| 235 | + const github = fakeGithub([], { graphqlError: 'API down' }); |
| 236 | + await run(github); |
| 237 | + |
| 238 | + assert.doesNotMatch(github.calls.comments[0].body, /link one under/); |
| 239 | +}); |
0 commit comments