| 1 | import type { INestApplication } from '@nestjs/common'; |
| 2 | import { execFile, execFileSync |
| 3 | import { mkdtempSync, rmSync |
| 4 | import { tmpdir } |
| 5 | import path from 'node:path |
| 6 | import { promisify } |
| 7 | import request from 'supertest |
| 8 | import { afterAll, beforeAll |
| 9 | |
| 10 | import { type Database, |
| 11 | import { eq } |
| 12 | |
| 13 | import { DATABASE } |
| 14 | import { hasBackends, signUp |
| 15 | |
| 16 | /** The pull request routes and fields gh's own suite (gh-cli.e2e-spec.ts) does not reach: REST pulls, reviews, reopening and merge refusals. */ |
| 17 | describe.skipIf(!hasBackends |
| 18 | let app: |
| 19 | let work: |
| 20 | let pushUrl: |
| 21 | let owner: |
| 22 | let reviewer: |
| 23 | const username = |
| 24 | const repo = |
| 25 | const v3 = |
| 26 | const get = |
| 27 | v3().get(` |
| 28 | const send = |
| 29 | method: 'post |
| 30 | path: string, |
| 31 | body: object, |
| 32 | token = owner. |
| 33 | ) => |
| 34 | v3() |
| 35 | [method](`/api/v3${path |
| 36 | .set('authorization', |
| 37 | .send(body); |
| 38 | const graphql = |
| 39 | const { body |
| 40 | .post('/api/graphql') |
| 41 | .set('authorization', |
| 42 | .send({ query |
| 43 | .expect(200); |
| 44 | return body; |
| 45 | }; |
| 46 | |
| 47 | beforeAll(async () |
| 48 | let origin: |
| 49 | ({ app, origin |
| 50 | owner = await |
| 51 | pushUrl = `${origin |
| 52 | reviewer = await |
| 53 | // GitHub links a commit to an account by verified email only. |
| 54 | await app |
| 55 | .get<Database |
| 56 | .update(schema.user |
| 57 | .set({ emailVerified: |
| 58 | .where(eq(schema |
| 59 | await v3() |
| 60 | .post('/api/repositories') |
| 61 | .set('cookie', |
| 62 | .send({ name: |
| 63 | .expect(201); |
| 64 | |
| 65 | work = mkdtempSync( |
| 66 | const git = |
| 67 | execFileSync( |
| 68 | 'git', |
| 69 | [ |
| 70 | '-c', |
| 71 | 'user.name=E2E', |
| 72 | '-c', |
| 73 | `user.email=${username}@example.com` |
| 74 | ...args, |
| 75 | ], |
| 76 | { cwd: work |
| 77 | ); |
| 78 | git('init', |
| 79 | writeFileSync(path.join |
| 80 | git('add', |
| 81 | git('commit', |
| 82 | // `feature` merges cleanly, `clash` conflicts with what main gains below, `chore` is closed and reopened. |
| 83 | for (const [branch |
| 84 | ['feature', 'two\n |
| 85 | ['clash', 'three\n |
| 86 | ['chore', 'four\n |
| 87 | ]) { |
| 88 | git('checkout', |
| 89 | writeFileSync(path.join |
| 90 | if (branch === |
| 91 | git('add', |
| 92 | git('commit', |
| 93 | } |
| 94 | git('checkout', |
| 95 | writeFileSync(path.join |
| 96 | git('commit', |
| 97 | // The server runs in this process, so the push must not block the event loop. |
| 98 | await promisify(execFile |
| 99 | 'git', |
| 100 | ['push', '-q' |
| 101 | { cwd: work, |
| 102 | ); |
| 103 | }, 120_000); |
| 104 | |
| 105 | afterAll(async () |
| 106 | await app?.close |
| 107 | if (work) rmSync |
| 108 | }); |
| 109 | |
| 110 | it('opens a pull request over REST and answers it in GitHub shape', |
| 111 | const { body, |
| 112 | title: 'Feature', |
| 113 | head: 'feature', |
| 114 | base: 'main', |
| 115 | body: 'Adds a feature', |
| 116 | }).expect(201) |
| 117 | expect(headers.location |
| 118 | expect(body).toMatchObject |
| 119 | id: expect.any |
| 120 | node_id: expect.stringMatching |
| 121 | number: 1, |
| 122 | state: 'open', |
| 123 | title: 'Feature', |
| 124 | body: 'Adds a feature', |
| 125 | draft: false, |
| 126 | merged: false, |
| 127 | mergeable: true, |
| 128 | mergeable_state: 'clean', |
| 129 | commits: 1, |
| 130 | additions: 1, |
| 131 | deletions: 0, |
| 132 | changed_files: 1, |
| 133 | user: expect.objectContaining |
| 134 | head: { |
| 135 | label: `${username} |
| 136 | ref: 'feature', |
| 137 | sha: expect.stringMatching |
| 138 | repo: expect.objectContaining |
| 139 | }, |
| 140 | base: expect.objectContaining |
| 141 | ref: 'main', |
| 142 | sha: expect.stringMatching |
| 143 | }), |
| 144 | }); |
| 145 | expect(body.html_url |
| 146 | // One integer id for the pull request everywhere, as GraphQL's databaseId has always answered. |
| 147 | const { data |
| 148 | repository(owner: "${username}", name: "tools") { pullRequest(number: 1) { databaseId } } |
| 149 | }`); |
| 150 | expect(data.repository |
| 151 | expect((await get |
| 152 | }); |
| 153 | |
| 154 | it('answers a conflicting pull request as not mergeable', |
| 155 | const { body |
| 156 | title: 'Clash', |
| 157 | head: 'clash', |
| 158 | base: 'main', |
| 159 | }).expect(201) |
| 160 | expect(body).toMatchObject |
| 161 | number: 2, |
| 162 | mergeable: false, |
| 163 | mergeable_state: 'dirty', |
| 164 | }); |
| 165 | expect( |
| 166 | (await send(' |
| 167 | ).toMatchObject({ message |
| 168 | }); |
| 169 | |
| 170 | it('lists by state, head and base, and answers 404 for an issue number', |
| 171 | const open = |
| 172 | expect(open.map |
| 173 | expect(open[0]) |
| 174 | expect( |
| 175 | (await get(` |
| 176 | ).toEqual([expect. |
| 177 | expect((await get |
| 178 | expect((await get |
| 179 | [], |
| 180 | ); |
| 181 | await v3() |
| 182 | .post(`/api/repositories/${username |
| 183 | .set('cookie', |
| 184 | .send({ title: |
| 185 | .expect(201); |
| 186 | await get(`${ |
| 187 | }); |
| 188 | |
| 189 | it('answers the diff when Accept asks for it, and the files and commits', |
| 190 | const diff = |
| 191 | .set('accept', |
| 192 | .expect(200); |
| 193 | expect(diff.text |
| 194 | const patch = |
| 195 | .set('accept', |
| 196 | .expect(200); |
| 197 | expect(patch.text |
| 198 | expect(patch.text |
| 199 | expect((await get |
| 200 | expect.objectContaining({ |
| 201 | filename: 'feature.txt', |
| 202 | status: 'added', |
| 203 | additions: 1, |
| 204 | changes: 1, |
| 205 | }), |
| 206 | ]); |
| 207 | expect((await get |
| 208 | expect.objectContaining({ |
| 209 | sha: expect.stringMatching |
| 210 | commit: expect.objectContaining |
| 211 | message: 'Add feature', |
| 212 | author: expect.objectContaining |
| 213 | email: `${username} |
| 214 | date: expect.any |
| 215 | }), |
| 216 | }), |
| 217 | author: expect.objectContaining |
| 218 | committer: expect.objectContaining |
| 219 | }), |
| 220 | ]); |
| 221 | const { data |
| 222 | repository(owner: "${username}", name: "tools") { |
| 223 | pullRequest(number: 1) { |
| 224 | commits(first: 1) { nodes { commit { |
| 225 | messageHeadline authoredDate |
| 226 | authors(first: 1) { nodes { email user { login } } } |
| 227 | } } } |
| 228 | } |
| 229 | } |
| 230 | }`); |
| 231 | expect(data.repository |
| 232 | messageHeadline: 'Add feature', |
| 233 | authoredDate: expect.any |
| 234 | authors: { |
| 235 | nodes: [ |
| 236 | { email: `${username |
| 237 | ], |
| 238 | }, |
| 239 | }); |
| 240 | }); |
| 241 | |
| 242 | it('derives reviewDecision and latestReviews from submitted reviews', |
| 243 | await v3() |
| 244 | .post(`/api/repositories/${username |
| 245 | .set('cookie', |
| 246 | .send({ state: |
| 247 | .expect(201); |
| 248 | await v3() |
| 249 | .post(`/api/repositories/${username |
| 250 | .set('cookie', |
| 251 | .send({ state: |
| 252 | .expect(201); |
| 253 | const { data |
| 254 | repository(owner: "${username}", name: "tools") { |
| 255 | pullRequest(number: 1) { |
| 256 | state merged isDraft reviewDecision mergeable mergeStateStatus |
| 257 | reviews(first: 10) { totalCount nodes { databaseId state author { login } commit { oid messageHeadline } } } |
| 258 | latestReviews(first: 10) { nodes { state } } |
| 259 | } |
| 260 | } |
| 261 | }`); |
| 262 | expect(data.repository |
| 263 | state: 'OPEN', |
| 264 | merged: false, |
| 265 | isDraft: false, |
| 266 | reviewDecision: 'APPROVED', |
| 267 | mergeable: 'MERGEABLE', |
| 268 | mergeStateStatus: 'CLEAN', |
| 269 | reviews: { |
| 270 | totalCount: 2, |
| 271 | nodes: [ |
| 272 | { state: 'COMMENTED', |
| 273 | { |
| 274 | state: 'APPROVED', |
| 275 | commit: { |
| 276 | oid: expect.stringMatching |
| 277 | messageHeadline: 'Add feature', |
| 278 | }, |
| 279 | }, |
| 280 | ], |
| 281 | }, |
| 282 | latestReviews: { nodes: [ |
| 283 | }); |
| 284 | const approved = |
| 285 | const reviews = |
| 286 | expect(reviews).toEqual |
| 287 | expect.objectContaining({ |
| 288 | state: 'COMMENTED', |
| 289 | body: 'Looking', |
| 290 | user: expect.objectContaining |
| 291 | }), |
| 292 | expect.objectContaining({ |
| 293 | id: approved.databaseId |
| 294 | state: 'APPROVED', |
| 295 | commit_id: approved.commit |
| 296 | html_url: expect.stringMatching |
| 297 | }), |
| 298 | ]); |
| 299 | expect( |
| 300 | (await get(` |
| 301 | .body, |
| 302 | ).toEqual(reviews[ |
| 303 | await get(`${ |
| 304 | }); |
| 305 | |
| 306 | it('refuses a merge whose head moved on, then merges and reports it', |
| 307 | await send('put |
| 308 | 409, |
| 309 | ); |
| 310 | await get(`${ |
| 311 | const merged = |
| 312 | await send('put |
| 313 | merge_method: 'squash', |
| 314 | }).expect(200) |
| 315 | ).body; |
| 316 | expect(merged).toMatchObject |
| 317 | await get(`${ |
| 318 | expect((await get |
| 319 | state: 'closed', |
| 320 | merged: true, |
| 321 | merge_commit_sha: merged.sha |
| 322 | merged_by: expect.objectContaining |
| 323 | }); |
| 324 | expect((await get |
| 325 | expect.objectContaining({ |
| 326 | ]); |
| 327 | await send('put |
| 328 | }); |
| 329 | |
| 330 | it('closes and reopens over REST, and never reopens a merged one', |
| 331 | await send('post |
| 332 | title: 'Chore', |
| 333 | head: 'chore', |
| 334 | base: 'main', |
| 335 | }).expect(201) |
| 336 | expect( |
| 337 | (await send(' |
| 338 | .body, |
| 339 | ).toMatchObject({ state |
| 340 | expect( |
| 341 | (await send(' |
| 342 | .body, |
| 343 | ).toMatchObject({ state |
| 344 | await send('patch |
| 345 | await send('patch |
| 346 | const update = |
| 347 | updatePullRequest(input: { pullRequestId: "${(await get( |
| 348 | pullRequest { state } |
| 349 | } |
| 350 | }`); |
| 351 | expect(update.errors |
| 352 | const { errors |
| 353 | reopenPullRequest(input: { pullRequestId: "${(await get( |
| 354 | pullRequest { state } |
| 355 | } |
| 356 | }`); |
| 357 | expect(errors[0]) |
| 358 | }); |
| 359 | |
| 360 | it("resolves a pull request's comment by node id", |
| 361 | const comment = |
| 362 | await send('post |
| 363 | body: 'On a PR', |
| 364 | }).expect(201) |
| 365 | ).body; |
| 366 | const { data |
| 367 | `{ node(id: "${comment.node_id |
| 368 | ); |
| 369 | expect(data.node |
| 370 | }); |
| 371 | |
| 372 | it('refuses a pull request id where an issue is asked for, and the reverse', |
| 373 | const pull = |
| 374 | const issue = |
| 375 | const closeIssue = |
| 376 | closeIssue(input: { issueId: "${pull}" }) { issue { id } } |
| 377 | }`); |
| 378 | expect(closeIssue.errors |
| 379 | const closePull = |
| 380 | closePullRequest(input: { pullRequestId: "${issue}" }) { pullRequest { id } } |
| 381 | }`); |
| 382 | expect(closePull.errors |
| 383 | }); |
| 384 | |
| 385 | it('lists the oldest commits first when a pull request has more than one page of them', |
| 386 | // One past the 250 Ghost reads at once, so the oldest would be cut if the newest were read first. |
| 387 | const git = |
| 388 | execFileSync( |
| 389 | 'git', |
| 390 | ['-c', 'user.name=E2E' |
| 391 | { |
| 392 | cwd: work, |
| 393 | }, |
| 394 | ); |
| 395 | git('checkout', |
| 396 | for (let n |
| 397 | git('commit', |
| 398 | await promisify(execFile |
| 399 | cwd: work, |
| 400 | env: { ...process |
| 401 | }); |
| 402 | const { number |
| 403 | await send('post |
| 404 | title: 'Long', |
| 405 | head: 'long', |
| 406 | base: 'main', |
| 407 | }).expect(201) |
| 408 | ).body; |
| 409 | const commits = |
| 410 | await get(`${ |
| 411 | ).body; |
| 412 | expect(commits[0] |
| 413 | const { data |
| 414 | repository(owner: "${username}", name: "tools") { |
| 415 | pullRequest(number: ${number}) { commits(first: 1) { totalCount nodes { commit { messageHeadline } } } } |
| 416 | } |
| 417 | }`); |
| 418 | expect(data.repository |
| 419 | totalCount: 251, |
| 420 | nodes: [{ commit: |
| 421 | }); |
| 422 | }, 60_000); |
| 423 | }); |