| 1 | import { execFile, execFileSync } from 'node:child_process'; |
| 2 | import { mkdtempSync, rmSync |
| 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 | describe.skipIf(!hasBackends |
| 13 | 'pull requests outlive the fork they came from', |
| 14 | () => { |
| 15 | let app: |
| 16 | let origin: |
| 17 | let upstream: |
| 18 | let forker: |
| 19 | let work: |
| 20 | const owner = |
| 21 | const contributor = |
| 22 | let repo: |
| 23 | let fork: |
| 24 | |
| 25 | const api = |
| 26 | const repos = |
| 27 | const pull = |
| 28 | const git = |
| 29 | execFileSync( |
| 30 | 'git', |
| 31 | ['-c', 'user.name=E2E' |
| 32 | { cwd: work, |
| 33 | ).trim(); |
| 34 | // The server runs in this process, so anything that talks to it must not block the event loop. |
| 35 | const push = |
| 36 | username: string, |
| 37 | key: string, |
| 38 | slug: string, |
| 39 | ...refs: string |
| 40 | ) => |
| 41 | promisify(execFile)( |
| 42 | 'git', |
| 43 | [ |
| 44 | 'push', |
| 45 | '-q', |
| 46 | `${origin.replace( |
| 47 | ...refs, |
| 48 | ], |
| 49 | { cwd: work, |
| 50 | ); |
| 51 | const deleteFork = |
| 52 | api() |
| 53 | .delete(`/api/repositories/${contributor |
| 54 | .set('cookie', |
| 55 | const openPull = |
| 56 | api() |
| 57 | .post(`${repos |
| 58 | .set('cookie', |
| 59 | .send({ title: |
| 60 | .expect(201) |
| 61 | .then((response) |
| 62 | |
| 63 | beforeAll(async () |
| 64 | ({ app, origin |
| 65 | upstream = await |
| 66 | forker = await |
| 67 | |
| 68 | repo = ( |
| 69 | await api() |
| 70 | .post('/api/repositories') |
| 71 | .set('cookie', |
| 72 | .send({ name: |
| 73 | .expect(201) |
| 74 | ).body.slug; |
| 75 | work = mkdtempSync( |
| 76 | git('init', |
| 77 | git('commit', |
| 78 | await push(owner |
| 79 | |
| 80 | fork = ( |
| 81 | await api() |
| 82 | .post(`${repos |
| 83 | .set('cookie', |
| 84 | .send({ name: |
| 85 | .expect(201) |
| 86 | ).body.slug; |
| 87 | for (const branch |
| 88 | git('checkout', |
| 89 | git('commit', |
| 90 | } |
| 91 | await push(contributor |
| 92 | }, 120_000); |
| 93 | |
| 94 | afterAll(async () |
| 95 | await app?.close |
| 96 | if (work) rmSync |
| 97 | }); |
| 98 | |
| 99 | it('refuses to delete a fork while it heads an open pull request', |
| 100 | const number = |
| 101 | |
| 102 | const { body |
| 103 | expect(body.message |
| 104 | }); |
| 105 | |
| 106 | it('deletes the fork once its pull requests are merged or closed, and keeps them', |
| 107 | const abandoned = |
| 108 | await api() |
| 109 | .post(pull(' |
| 110 | .set('cookie', |
| 111 | .send({}) |
| 112 | .expect(201); |
| 113 | await api() |
| 114 | .patch(pull(` |
| 115 | .set('cookie', |
| 116 | .expect(200); |
| 117 | |
| 118 | await deleteFork().expect |
| 119 | |
| 120 | const merged = |
| 121 | expect(merged.body |
| 122 | state: 'merged', |
| 123 | head: { username: null |
| 124 | commitCount: 1, |
| 125 | }); |
| 126 | const commits = |
| 127 | expect( |
| 128 | commits.body.commits |
| 129 | ).toEqual(['feature' |
| 130 | |
| 131 | const closed = |
| 132 | .get(pull(` |
| 133 | .expect(200); |
| 134 | expect(closed.body |
| 135 | state: 'closed', |
| 136 | head: { username: null |
| 137 | commitCount: 0, |
| 138 | }); |
| 139 | await api() |
| 140 | .get(pull(` |
| 141 | .expect(410); |
| 142 | }); |
| 143 | }, |
| 144 | ); |