| 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 { hasBackends, scopedKey |
| 11 | |
| 12 | describe.skipIf(!hasBackends |
| 13 | 'GitHub REST v3 issues, comments and labels', |
| 14 | () => { |
| 15 | let app: |
| 16 | let work: |
| 17 | let owner: |
| 18 | let stranger: |
| 19 | let pullRequestNumber: |
| 20 | const username = |
| 21 | const repo = |
| 22 | const v3 = |
| 23 | const as = |
| 24 | token ? call. |
| 25 | const get = |
| 26 | as(v3().get |
| 27 | const send = |
| 28 | method: 'post |
| 29 | path: string, |
| 30 | body: unknown, |
| 31 | token?: string |
| 32 | ) => as( |
| 33 | /** The page number each `rel` in a Link header points at. */ |
| 34 | const pagesIn = |
| 35 | Object.fromEntries( |
| 36 | [...link.matchAll |
| 37 | rel, |
| 38 | Number(new URL |
| 39 | ]), |
| 40 | ); |
| 41 | const openIssue = |
| 42 | ( |
| 43 | await send( |
| 44 | 'post', |
| 45 | `${repo}/issues`, |
| 46 | { title, ... |
| 47 | owner.key, |
| 48 | ).expect(201) |
| 49 | ).body; |
| 50 | |
| 51 | beforeAll(async () |
| 52 | let origin: |
| 53 | ({ app, origin |
| 54 | owner = await |
| 55 | stranger = await |
| 56 | const api = |
| 57 | for (const [name |
| 58 | ['public-repo', 'public' |
| 59 | ['secret-repo', 'private' |
| 60 | ]) |
| 61 | await api |
| 62 | .post('/api/repositories') |
| 63 | .set('cookie', |
| 64 | .send({ name |
| 65 | .expect(201); |
| 66 | |
| 67 | // One real pull request, so the issue list has one to mark. |
| 68 | work = mkdtempSync( |
| 69 | const git = |
| 70 | execFileSync( |
| 71 | 'git', |
| 72 | ['-c', 'user.name=E2E' |
| 73 | { cwd: work |
| 74 | ); |
| 75 | git('init', |
| 76 | writeFileSync(path.join |
| 77 | git('add', |
| 78 | git('commit', |
| 79 | git('checkout', |
| 80 | writeFileSync(path.join |
| 81 | git('commit', |
| 82 | // The server runs in this process, so the push must not block the event loop. |
| 83 | await promisify(execFile |
| 84 | 'git', |
| 85 | [ |
| 86 | 'push', |
| 87 | '-q', |
| 88 | `${origin.replace( |
| 89 | 'main', |
| 90 | 'feature', |
| 91 | ], |
| 92 | { cwd: work, |
| 93 | ); |
| 94 | const pull = |
| 95 | .post(`/api/repositories/${username |
| 96 | .set('cookie', |
| 97 | .send({ title: |
| 98 | .expect(201); |
| 99 | pullRequestNumber = pull. |
| 100 | }, 120_000); |
| 101 | |
| 102 | afterAll(async () |
| 103 | await app?.close |
| 104 | if (work) rmSync |
| 105 | }); |
| 106 | |
| 107 | describe('issues', |
| 108 | it('opens an issue with integer ids, creating the labels it names', |
| 109 | const response = |
| 110 | 'post', |
| 111 | `${repo}/issues`, |
| 112 | { |
| 113 | title: 42, |
| 114 | body: 'It broke', |
| 115 | labels: ['fresh-label'], |
| 116 | assignees: [username], |
| 117 | }, |
| 118 | owner.key, |
| 119 | ).expect(201); |
| 120 | const issue = |
| 121 | expect(response.headers |
| 122 | expect(issue).toMatchObject |
| 123 | id: expect.any |
| 124 | title: '42', |
| 125 | state: 'open', |
| 126 | state_reason: null, |
| 127 | user: { login: username |
| 128 | assignee: { login: username |
| 129 | assignees: [{ login: |
| 130 | labels: [ |
| 131 | { |
| 132 | id: expect.any |
| 133 | name: 'fresh-label', |
| 134 | color: 'ededed', |
| 135 | description: null, |
| 136 | default: false, |
| 137 | }, |
| 138 | ], |
| 139 | locked: false, |
| 140 | milestone: null, |
| 141 | comments: 0, |
| 142 | author_association: 'OWNER', |
| 143 | }); |
| 144 | expect(issue.node_id |
| 145 | expect(issue).not |
| 146 | |
| 147 | const graphql = |
| 148 | .post('/api/graphql') |
| 149 | .set('authorization', |
| 150 | .send({ |
| 151 | query: `query { repository(owner: "${username |
| 152 | }) |
| 153 | .expect(200); |
| 154 | expect(graphql.body |
| 155 | }); |
| 156 | |
| 157 | it('drops labels and assignees from someone who cannot triage, as GitHub does', |
| 158 | const issue = |
| 159 | await send( |
| 160 | 'post', |
| 161 | `${repo}/issues`, |
| 162 | { |
| 163 | title: 'From outside', |
| 164 | labels: ['outsider-label'], |
| 165 | assignees: [username], |
| 166 | }, |
| 167 | stranger.key, |
| 168 | ).expect(201) |
| 169 | ).body; |
| 170 | expect(issue.labels |
| 171 | expect(issue.assignees |
| 172 | await get(`${ |
| 173 | }); |
| 174 | |
| 175 | it('refuses anonymous writes and hides a private repository from tokens without repo', |
| 176 | await send('post |
| 177 | 401, |
| 178 | ); |
| 179 | await get(`/repos/ |
| 180 | const publicOnly = |
| 181 | await get(`/repos/ |
| 182 | 404, |
| 183 | ); |
| 184 | await get(`/repos/ |
| 185 | 200, |
| 186 | ); |
| 187 | }); |
| 188 | |
| 189 | it('lists pull requests alongside issues, marked as GitHub marks them', |
| 190 | const { body |
| 191 | `${repo}/issues?state=all&per_page=100`, |
| 192 | ).expect(200); |
| 193 | const pull = |
| 194 | (issue: { |
| 195 | ); |
| 196 | expect(pull).toMatchObject |
| 197 | draft: false, |
| 198 | pull_request: { |
| 199 | url: expect.stringMatching |
| 200 | new RegExp(`/pulls/ |
| 201 | ), |
| 202 | html_url: expect.stringMatching |
| 203 | new RegExp(`/pull/ |
| 204 | ), |
| 205 | merged_at: null, |
| 206 | }, |
| 207 | }); |
| 208 | expect(pull.node_id |
| 209 | }); |
| 210 | |
| 211 | it('pages with page and per_page and links every page in Link', |
| 212 | for (let i |
| 213 | const first = |
| 214 | expect(first.body |
| 215 | const firstLinks = |
| 216 | expect(firstLinks.next |
| 217 | expect(firstLinks.last |
| 218 | expect(firstLinks).not |
| 219 | |
| 220 | const second = |
| 221 | 200, |
| 222 | ); |
| 223 | expect(pagesIn(second |
| 224 | prev: 1, |
| 225 | first: 1, |
| 226 | next: 3, |
| 227 | }); |
| 228 | expect(second.body |
| 229 | |
| 230 | const capped = |
| 231 | expect(capped.headers |
| 232 | }); |
| 233 | |
| 234 | it('filters by creator, assignee and since', |
| 235 | const byStranger = |
| 236 | `${repo}/issues?creator=${username |
| 237 | ).expect(200); |
| 238 | expect( |
| 239 | byStranger.body.map |
| 240 | ).toEqual(['From outside' |
| 241 | const assigned = |
| 242 | expect( |
| 243 | assigned.body.every |
| 244 | (issue: { |
| 245 | ), |
| 246 | ).toBe(true); |
| 247 | const unassigned = |
| 248 | `${repo}/issues?assignee=none&per_page=100`, |
| 249 | ).expect(200); |
| 250 | expect( |
| 251 | unassigned.body.every |
| 252 | (issue: { |
| 253 | ), |
| 254 | ).toBe(true); |
| 255 | const future = |
| 256 | `${repo}/issues?since=2999-01-01T00:00:00Z`, |
| 257 | ).expect(200); |
| 258 | expect(future.body |
| 259 | }); |
| 260 | |
| 261 | it('closes an already closed issue without complaint and refuses a milestone', |
| 262 | const issue = |
| 263 | for (let i |
| 264 | const closed = |
| 265 | 'patch', |
| 266 | `${repo}/issues/${issue |
| 267 | { state: 'closed', |
| 268 | owner.key, |
| 269 | ).expect(200); |
| 270 | expect(closed.body |
| 271 | state: 'closed', |
| 272 | state_reason: 'completed', |
| 273 | closed_by: { login: username |
| 274 | }); |
| 275 | } |
| 276 | const refused = |
| 277 | 'patch', |
| 278 | `${repo}/issues/${issue |
| 279 | { milestone: 1 }, |
| 280 | owner.key, |
| 281 | ).expect(422); |
| 282 | expect(refused.body |
| 283 | { resource: 'Issue', |
| 284 | ]); |
| 285 | }); |
| 286 | |
| 287 | it('edits title, body, labels and assignees in one PATCH', |
| 288 | const issue = |
| 289 | const { body |
| 290 | 'patch', |
| 291 | `${repo}/issues/${issue |
| 292 | { |
| 293 | title: 'After', |
| 294 | body: 'Now with detail', |
| 295 | labels: [{ name: |
| 296 | assignee: username, |
| 297 | }, |
| 298 | owner.key, |
| 299 | ).expect(200); |
| 300 | expect(body).toMatchObject |
| 301 | title: 'After', |
| 302 | body: 'Now with detail', |
| 303 | labels: [{ name: |
| 304 | assignees: [{ login: |
| 305 | }); |
| 306 | }); |
| 307 | |
| 308 | it('answers 404 for a number that does not exist or is not a number', |
| 309 | await get(`${ |
| 310 | await get(`${ |
| 311 | }); |
| 312 | |
| 313 | it('answers an unknown assignee with 422 on the assignees field', |
| 314 | const response = |
| 315 | 'post', |
| 316 | `${repo}/issues`, |
| 317 | { title: 'Ghost assignee', |
| 318 | owner.key, |
| 319 | ).expect(422); |
| 320 | expect(response.body |
| 321 | { resource: 'Issue', |
| 322 | ]); |
| 323 | }); |
| 324 | }); |
| 325 | |
| 326 | describe('assignees', |
| 327 | it('adds with 201 and removes with 200, answering the issue', |
| 328 | const issue = |
| 329 | const added = |
| 330 | 'post', |
| 331 | `${repo}/issues/${issue |
| 332 | { assignees: [username |
| 333 | owner.key, |
| 334 | ).expect(201); |
| 335 | expect(added.body |
| 336 | expect.objectContaining({ |
| 337 | ]); |
| 338 | const removed = |
| 339 | 'delete', |
| 340 | `${repo}/issues/${issue |
| 341 | { assignees: [username |
| 342 | owner.key, |
| 343 | ).expect(200); |
| 344 | expect(removed.body |
| 345 | }); |
| 346 | |
| 347 | it('ignores additions from someone who cannot triage', |
| 348 | const issue = |
| 349 | const response = |
| 350 | 'post', |
| 351 | `${repo}/issues/${issue |
| 352 | { assignees: [username |
| 353 | stranger.key, |
| 354 | ).expect(201); |
| 355 | expect(response.body |
| 356 | }); |
| 357 | |
| 358 | it('lists people with access and checks a login with 204 or 404', |
| 359 | const { body |
| 360 | expect(body.map |
| 361 | username, |
| 362 | ]); |
| 363 | await get(`${ |
| 364 | await get(`${ |
| 365 | const issue = |
| 366 | await get( |
| 367 | `${repo}/issues/${issue |
| 368 | ).expect(204); |
| 369 | }); |
| 370 | }); |
| 371 | |
| 372 | describe('labels', |
| 373 | it('creates, reads, renames and deletes a label by name', |
| 374 | const created = |
| 375 | 'post', |
| 376 | `${repo}/labels`, |
| 377 | { name: 'needs triage', |
| 378 | owner.key, |
| 379 | ).expect(201); |
| 380 | expect(created.headers |
| 381 | expect(created.body |
| 382 | id: expect.any |
| 383 | name: 'needs triage', |
| 384 | color: 'd73a4a', |
| 385 | description: 'Look at me', |
| 386 | default: false, |
| 387 | archived_at: null, |
| 388 | archived_by: null, |
| 389 | }); |
| 390 | expect(created.body |
| 391 | |
| 392 | const duplicate = |
| 393 | 'post', |
| 394 | `${repo}/labels`, |
| 395 | { name: 'needs triage' |
| 396 | owner.key, |
| 397 | ).expect(422); |
| 398 | expect(duplicate.body |
| 399 | message: 'Validation Failed', |
| 400 | errors: [ |
| 401 | { resource: 'Label', |
| 402 | ], |
| 403 | status: '422', |
| 404 | }); |
| 405 | |
| 406 | await get(`${ |
| 407 | const renamed = |
| 408 | 'patch', |
| 409 | `${repo}/labels/needs%20triage`, |
| 410 | { new_name: 'triaged', |
| 411 | owner.key, |
| 412 | ).expect(200); |
| 413 | expect(renamed.body |
| 414 | name: 'triaged', |
| 415 | color: '0e8a16', |
| 416 | }); |
| 417 | await get(`${ |
| 418 | await send('delete |
| 419 | 204, |
| 420 | ); |
| 421 | await get(`${ |
| 422 | }); |
| 423 | |
| 424 | it('defaults a new label to ededed and pages the list', |
| 425 | const { body |
| 426 | 'post', |
| 427 | `${repo}/labels`, |
| 428 | { name: 'plain' |
| 429 | owner.key, |
| 430 | ).expect(201); |
| 431 | expect(body.color |
| 432 | const page = |
| 433 | expect(page.body |
| 434 | expect(pagesIn(page |
| 435 | }); |
| 436 | |
| 437 | it("adds, replaces and removes an issue's labels in every body shape GitHub takes", |
| 438 | const issue = |
| 439 | const at = |
| 440 | const names = |
| 441 | body.map((label |
| 442 | |
| 443 | const added = |
| 444 | expect(names(added |
| 445 | const more = |
| 446 | 'post', |
| 447 | at, |
| 448 | { labels: [{ name |
| 449 | owner.key, |
| 450 | ).expect(200); |
| 451 | expect(names(more |
| 452 | const listed = |
| 453 | expect(names(listed |
| 454 | |
| 455 | const replaced = |
| 456 | 'put', |
| 457 | at, |
| 458 | { labels: ['gamma' |
| 459 | owner.key, |
| 460 | ).expect(200); |
| 461 | expect(names(replaced |
| 462 | |
| 463 | const left = |
| 464 | 200, |
| 465 | ); |
| 466 | expect(names(left |
| 467 | await send('delete |
| 468 | |
| 469 | await send('delete |
| 470 | expect((await get |
| 471 | |
| 472 | const invalid = |
| 473 | 'post', |
| 474 | at, |
| 475 | { labels: [7] |
| 476 | owner.key, |
| 477 | ).expect(422); |
| 478 | expect(invalid.body |
| 479 | { resource: 'Label', |
| 480 | ]); |
| 481 | }); |
| 482 | }); |
| 483 | |
| 484 | describe('comments', |
| 485 | it('creates, reads, edits and deletes a comment by its integer id', |
| 486 | const issue = |
| 487 | const created = |
| 488 | 'post', |
| 489 | `${repo}/issues/${issue |
| 490 | { body: 'First!' |
| 491 | owner.key, |
| 492 | ).expect(201); |
| 493 | const comment = |
| 494 | expect(created.headers |
| 495 | expect(comment).toMatchObject |
| 496 | id: expect.any |
| 497 | body: 'First!', |
| 498 | user: { login: username |
| 499 | author_association: 'OWNER', |
| 500 | issue_url: issue.url |
| 501 | }); |
| 502 | expect(comment.url |
| 503 | new RegExp(`/issues/comments/ |
| 504 | ); |
| 505 | expect(comment.html_url |
| 506 | |
| 507 | await get(`${ |
| 508 | await send( |
| 509 | 'patch', |
| 510 | `${repo}/issues/comments/${comment |
| 511 | { body: 'Not yours' |
| 512 | stranger.key, |
| 513 | ).expect(403); |
| 514 | const edited = |
| 515 | 'patch', |
| 516 | `${repo}/issues/comments/${comment |
| 517 | { body: 'Edited' |
| 518 | owner.key, |
| 519 | ).expect(200); |
| 520 | expect(edited.body |
| 521 | |
| 522 | const listed = |
| 523 | `${repo}/issues/${issue |
| 524 | ).expect(200); |
| 525 | expect(listed.body |
| 526 | comment.id, |
| 527 | ]); |
| 528 | expect( |
| 529 | (await get(` |
| 530 | .comments, |
| 531 | ).toBe(1); |
| 532 | |
| 533 | await send( |
| 534 | 'delete', |
| 535 | `${repo}/issues/comments/${comment |
| 536 | {}, |
| 537 | owner.key, |
| 538 | ).expect(204); |
| 539 | await get(`${ |
| 540 | }); |
| 541 | |
| 542 | it('answers 404 for a comment id from another repository', |
| 543 | const secret = |
| 544 | await send( |
| 545 | 'post', |
| 546 | `/repos/${username}/secret-repo/issues` |
| 547 | { title: 'Hidden' |
| 548 | owner.key, |
| 549 | ).expect(201) |
| 550 | ).body; |
| 551 | const comment = |
| 552 | await send( |
| 553 | 'post', |
| 554 | `/repos/${username}/secret-repo/issues/${ |
| 555 | { body: 'Hidden comment' |
| 556 | owner.key, |
| 557 | ).expect(201) |
| 558 | ).body; |
| 559 | await get(`${ |
| 560 | 404, |
| 561 | ); |
| 562 | }); |
| 563 | |
| 564 | it('lists every comment in the repository, oldest first unless sorted', |
| 565 | const issue = |
| 566 | for (const body |
| 567 | await send( |
| 568 | 'post', |
| 569 | `${repo}/issues/${issue |
| 570 | { body }, |
| 571 | owner.key, |
| 572 | ).expect(201); |
| 573 | const all = |
| 574 | 200, |
| 575 | ); |
| 576 | const bodies = |
| 577 | expect(bodies.indexOf |
| 578 | const newest = |
| 579 | `${repo}/issues/comments?sort=created&per_page=1`, |
| 580 | ).expect(200); |
| 581 | expect(newest.body |
| 582 | const none = |
| 583 | `${repo}/issues/${issue |
| 584 | ).expect(200); |
| 585 | expect(none.body |
| 586 | }); |
| 587 | }); |
| 588 | }, |
| 589 | ); |