|
| 1 | +import { describe, beforeEach, afterEach, test, expect } from 'vitest'; |
| 2 | +import fs from 'fs'; |
| 3 | +import nock from 'nock'; |
| 4 | +import path from 'path'; |
| 5 | + |
| 6 | +import { setupProbot, teardownProbot } from '../testHelpers'; |
| 7 | + |
| 8 | +const issueCommentPayload = JSON.parse( |
| 9 | + fs.readFileSync( |
| 10 | + path.join(__dirname, '../fixtures/issue_comment.json'), |
| 11 | + 'utf-8', |
| 12 | + ), |
| 13 | +); |
| 14 | + |
| 15 | +const pullRequestPayload = JSON.parse( |
| 16 | + fs.readFileSync( |
| 17 | + path.join(__dirname, '../fixtures/pull_request.json'), |
| 18 | + 'utf-8', |
| 19 | + ), |
| 20 | +); |
| 21 | + |
| 22 | +describe('Issue Comment Auto-Assign', () => { |
| 23 | + let probot: any; |
| 24 | + |
| 25 | + beforeEach(() => { |
| 26 | + probot = setupProbot(); |
| 27 | + }); |
| 28 | + |
| 29 | + afterEach(() => { |
| 30 | + teardownProbot(); |
| 31 | + }); |
| 32 | + |
| 33 | + test('assigns maintainer when they comment on a PR', async () => { |
| 34 | + const mock = nock('https://api.github.com') |
| 35 | + .post('/app/installations/2/access_tokens') |
| 36 | + .reply(200, { |
| 37 | + token: 'test', |
| 38 | + permissions: { |
| 39 | + pull_requests: 'write', |
| 40 | + }, |
| 41 | + }) |
| 42 | + .get('/repos/your-repo/your-repo-name/pulls/1') |
| 43 | + .reply(200, pullRequestPayload.pull_request) |
| 44 | + .get('/orgs/actualbudget/members/maintainer-user') |
| 45 | + .reply(204) |
| 46 | + .post( |
| 47 | + '/repos/your-repo/your-repo-name/issues/1/assignees', |
| 48 | + (body: any) => { |
| 49 | + expect(body).toMatchObject({ assignees: ['maintainer-user'] }); |
| 50 | + return true; |
| 51 | + }, |
| 52 | + ) |
| 53 | + .reply(200); |
| 54 | + |
| 55 | + await probot.receive({ |
| 56 | + name: 'issue_comment', |
| 57 | + payload: issueCommentPayload, |
| 58 | + }); |
| 59 | + |
| 60 | + expect(mock.pendingMocks()).toStrictEqual([]); |
| 61 | + }); |
| 62 | + |
| 63 | + test('does not assign non-maintainer when they comment on a PR', async () => { |
| 64 | + const mock = nock('https://api.github.com') |
| 65 | + .post('/app/installations/2/access_tokens') |
| 66 | + .reply(200, { |
| 67 | + token: 'test', |
| 68 | + permissions: { |
| 69 | + pull_requests: 'write', |
| 70 | + }, |
| 71 | + }) |
| 72 | + .get('/repos/your-repo/your-repo-name/pulls/1') |
| 73 | + .reply(200, pullRequestPayload.pull_request) |
| 74 | + .get('/orgs/actualbudget/members/maintainer-user') |
| 75 | + .reply(404); |
| 76 | + |
| 77 | + const errorMock = nock('https://api.github.com') |
| 78 | + .post('/repos/your-repo/your-repo-name/issues/1/assignees') |
| 79 | + .reply(() => { |
| 80 | + throw new Error('Assignee should not be called for non-maintainers'); |
| 81 | + }); |
| 82 | + |
| 83 | + await probot.receive({ |
| 84 | + name: 'issue_comment', |
| 85 | + payload: issueCommentPayload, |
| 86 | + }); |
| 87 | + |
| 88 | + expect(mock.pendingMocks()).toStrictEqual([]); |
| 89 | + expect(errorMock.isDone()).toBe(false); |
| 90 | + }); |
| 91 | + |
| 92 | + test('ignores comments on issues (not PRs)', async () => { |
| 93 | + const issueOnlyPayload = { |
| 94 | + ...issueCommentPayload, |
| 95 | + issue: { |
| 96 | + ...issueCommentPayload.issue, |
| 97 | + pull_request: undefined, |
| 98 | + }, |
| 99 | + }; |
| 100 | + |
| 101 | + const errorMock = nock('https://api.github.com') |
| 102 | + .get('/orgs/actualbudget/members/maintainer-user') |
| 103 | + .reply(() => { |
| 104 | + throw new Error('Org membership should not be checked for issues'); |
| 105 | + }); |
| 106 | + |
| 107 | + await probot.receive({ |
| 108 | + name: 'issue_comment', |
| 109 | + payload: issueOnlyPayload, |
| 110 | + }); |
| 111 | + |
| 112 | + expect(errorMock.isDone()).toBe(false); |
| 113 | + }); |
| 114 | +}); |
0 commit comments