| 1 | import { execFile, execFileSync } from 'node:child_process'; |
| 2 | import { existsSync, mkdtempSync |
| 3 | import { tmpdir } |
| 4 | import path from 'node:path |
| 5 | import { promisify } |
| 6 | import type { INestApplication |
| 7 | import request from 'supertest |
| 8 | import { afterAll, beforeAll |
| 9 | |
| 10 | import { hasBackends, signUp |
| 11 | |
| 12 | const SOURCE = ' |
| 13 | // Cloned once per machine; the suite only ever reads it. |
| 14 | const CLONE = path |
| 15 | |
| 16 | type TimelineItem |
| 17 | kind: 'comment |
| 18 | event?: { |
| 19 | type: string |
| 20 | sourceNumber: number |
| 21 | commitSha: string |
| 22 | }; |
| 23 | source?: { |
| 24 | sourceType?: string |
| 25 | commitSha?: string |
| 26 | }; |
| 27 | |
| 28 | // Also needs network access to GitHub. |
| 29 | describe.skipIf(!hasBackends |
| 30 | 'issue and pull request references against a real repository', |
| 31 | () => { |
| 32 | let app: |
| 33 | let origin: |
| 34 | let cookie: |
| 35 | let work: |
| 36 | const username = |
| 37 | let repo: |
| 38 | |
| 39 | const api = |
| 40 | const at = |
| 41 | `/api/repositories/${username}/${ |
| 42 | const gitEnv = |
| 43 | ...process.env, |
| 44 | GIT_AUTHOR_NAME: 'E2E', |
| 45 | GIT_AUTHOR_EMAIL: `${username} |
| 46 | GIT_COMMITTER_NAME: 'E2E', |
| 47 | GIT_COMMITTER_EMAIL: `${username} |
| 48 | GIT_TERMINAL_PROMPT: '0', |
| 49 | }); |
| 50 | const git = |
| 51 | execFileSync('git', |
| 52 | cwd: work, |
| 53 | encoding: 'utf8', |
| 54 | env: gitEnv(), |
| 55 | }).trim(); |
| 56 | // The server runs in this process, so anything that talks to it must not block the event loop. |
| 57 | const remote = |
| 58 | promisify(execFile)('git |
| 59 | const commit = |
| 60 | git('commit', |
| 61 | |
| 62 | const issue = |
| 63 | api() |
| 64 | .get(at(` |
| 65 | .set('cookie', |
| 66 | .expect(200) |
| 67 | .then((response) |
| 68 | const timeline = |
| 69 | api() |
| 70 | .get(at(` |
| 71 | .set('cookie', |
| 72 | .expect(200) |
| 73 | .then((response) |
| 74 | const openIssue = |
| 75 | api() |
| 76 | .post(at(' |
| 77 | .set('cookie', |
| 78 | .send({ title |
| 79 | .expect(201) |
| 80 | .then((response) |
| 81 | |
| 82 | beforeAll(async () |
| 83 | ({ app, origin |
| 84 | const account = |
| 85 | cookie = account. |
| 86 | |
| 87 | const created = |
| 88 | .post('/api/repositories') |
| 89 | .set('cookie', |
| 90 | .send({ name: |
| 91 | .expect(201); |
| 92 | repo = created. |
| 93 | |
| 94 | if (!existsSync(CLONE |
| 95 | execFileSync('git', |
| 96 | } |
| 97 | work = mkdtempSync( |
| 98 | execFileSync('git', |
| 99 | git( |
| 100 | 'remote', |
| 101 | 'add', |
| 102 | 'ghost', |
| 103 | `${origin.replace( |
| 104 | ); |
| 105 | await remote('push |
| 106 | }, 180_000); |
| 107 | |
| 108 | afterAll(async () |
| 109 | await app?.close |
| 110 | if (work) rmSync |
| 111 | }); |
| 112 | |
| 113 | it('numbers issues and pull requests from one sequence, and keeps pull requests out of the issue list', |
| 114 | expect(await openIssue |
| 115 | expect(await openIssue |
| 116 | expect(await openIssue |
| 117 | |
| 118 | git('checkout', |
| 119 | commit('Tidy navbar spacing\n\nFixes #2' |
| 120 | await remote('push |
| 121 | // not the default branch, so nothing closes yet |
| 122 | await new Promise |
| 123 | expect((await issue |
| 124 | |
| 125 | const pull = |
| 126 | .post(at(' |
| 127 | .set('cookie', |
| 128 | .send({ |
| 129 | title: 'Fix the navbar', |
| 130 | body: 'Closes #1. Related to #3.\n\n`#2` in code is not a reference.' |
| 131 | base: 'main', |
| 132 | head: 'fix-navbar', |
| 133 | }) |
| 134 | .expect(201); |
| 135 | expect(pull.body |
| 136 | |
| 137 | expect(await issue |
| 138 | const listed = |
| 139 | .get(at(' |
| 140 | .set('cookie', |
| 141 | .expect(200); |
| 142 | expect( |
| 143 | listed.body.issues |
| 144 | ).toEqual([3, |
| 145 | }); |
| 146 | |
| 147 | it('shows where an issue was mentioned', |
| 148 | await api() |
| 149 | .post(at(' |
| 150 | .set('cookie', |
| 151 | .send({ body: |
| 152 | .expect(201); |
| 153 | |
| 154 | expect(await timeline |
| 155 | expect.objectContaining({ |
| 156 | kind: 'reference', |
| 157 | sourceType: 'issue', |
| 158 | source: expect.objectContaining |
| 159 | }), |
| 160 | ); |
| 161 | expect(await timeline |
| 162 | expect.objectContaining({ |
| 163 | kind: 'reference', |
| 164 | source: expect.objectContaining |
| 165 | }), |
| 166 | ); |
| 167 | // inside backticks, so the pull request never mentioned #2 |
| 168 | expect( |
| 169 | (await timeline(2 |
| 170 | ).toEqual([]); |
| 171 | }); |
| 172 | |
| 173 | it('closes what the pull request and its commits promise when it merges into the default branch', |
| 174 | await api() |
| 175 | .post(at(' |
| 176 | .set('cookie', |
| 177 | .send({}) |
| 178 | .expect(201); |
| 179 | |
| 180 | const pull = |
| 181 | expect(pull.body |
| 182 | expect( |
| 183 | (await timeline(4 |
| 184 | ).toBe(true); |
| 185 | |
| 186 | for (const number |
| 187 | expect(await issue |
| 188 | expect(await timeline |
| 189 | expect.objectContaining({ |
| 190 | kind: 'event', |
| 191 | event: expect.objectContaining |
| 192 | }), |
| 193 | ); |
| 194 | } |
| 195 | // "Related to" is a mention, not a promise |
| 196 | expect((await issue |
| 197 | }); |
| 198 | |
| 199 | it('closes an issue from a commit pushed straight to the default branch', |
| 200 | git('checkout', |
| 201 | await remote('pull |
| 202 | commit('Repair footer links (resolves #3)'); |
| 203 | const sha = |
| 204 | await remote('push |
| 205 | |
| 206 | await vi.waitFor |
| 207 | async () => |
| 208 | { timeout: 15_000, interval |
| 209 | ); |
| 210 | expect(await timeline |
| 211 | expect.objectContaining({ |
| 212 | kind: 'event', |
| 213 | event: expect.objectContaining |
| 214 | }), |
| 215 | ); |
| 216 | expect(await timeline |
| 217 | expect.objectContaining({ |
| 218 | kind: 'reference', |
| 219 | sourceType: 'commit', |
| 220 | commitSha: sha, |
| 221 | }), |
| 222 | ); |
| 223 | }); |
| 224 | }, |
| 225 | ); |