| 1 | import { execFile, execFileSync } from 'node:child_process'; |
| 2 | import { mkdtempSync, rmSync } from 'node:fs'; |
| 3 | import { tmpdir } from 'node:os'; |
| 4 | import path from 'node:path'; |
| 5 | import { promisify } from 'node:util'; |
| 6 | import type { INestApplication } from '@nestjs/common'; |
| 7 | import request from 'supertest'; |
| 8 | import { afterAll, beforeAll, describe, expect, it } from 'vitest'; |
| 9 | |
| 10 | import { hasBackends, signUp, startApp } from './harness.js'; |
| 11 | |
| 12 | describe.skipIf(!hasBackends)( |
| 13 | 'pull requests outlive the fork they came from', |
| 14 | () => { |
| 15 | let app: INestApplication; |
| 16 | let origin: string; |
| 17 | let upstream: { cookie: string; key: string }; |
| 18 | let forker: { cookie: string; key: string }; |
| 19 | let work: string; |
| 20 | const owner = `upstream${Date.now()}`; |
| 21 | const contributor = `forker${Date.now()}`; |
| 22 | let repo: string; |
| 23 | let fork: string; |
| 24 | |
| 25 | const api = () => request(app.getHttpServer()); |
| 26 | const repos = `/api/repositories/${owner}/`; |
| 27 | const pull = (suffix: string) => `${repos}${repo}/pulls/${suffix}`; |
| 28 | const git = (...args: string[]) => |
| 29 | execFileSync( |
| 30 | 'git', |
| 31 | ['-c', 'user.name=E2E', '-c', 'user.email=e2e@example.com', ...args], |
| 32 | { cwd: work, encoding: 'utf8' }, |
| 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('://', `://${username}:${key}@`)}/${username}/${slug}.git`, |
| 47 | ...refs, |
| 48 | ], |
| 49 | { cwd: work, env: { ...process.env, GIT_TERMINAL_PROMPT: '0' } }, |
| 50 | ); |
| 51 | const deleteFork = () => |
| 52 | api() |
| 53 | .delete(`/api/repositories/${contributor}/${fork}`) |
| 54 | .set('cookie', forker.cookie); |
| 55 | const openPull = (head: string) => |
| 56 | api() |
| 57 | .post(`${repos}${repo}/pulls`) |
| 58 | .set('cookie', forker.cookie) |
| 59 | .send({ title: head, base: 'main', head: `${contributor}:${head}` }) |
| 60 | .expect(201) |
| 61 | .then((response) => response.body.number as number); |
| 62 | |
| 63 | beforeAll(async () => { |
| 64 | ({ app, origin } = await startApp()); |
| 65 | upstream = await signUp(app, owner); |
| 66 | forker = await signUp(app, contributor); |
| 67 | |
| 68 | repo = ( |
| 69 | await api() |
| 70 | .post('/api/repositories') |
| 71 | .set('cookie', upstream.cookie) |
| 72 | .send({ name: 'upstream', visibility: 'public' }) |
| 73 | .expect(201) |
| 74 | ).body.slug; |
| 75 | work = mkdtempSync(path.join(tmpdir(), 'ghost-e2e-fork-')); |
| 76 | git('init', '-q', '-b', 'main'); |
| 77 | git('commit', '-q', '--allow-empty', '-m', 'first'); |
| 78 | await push(owner, upstream.key, repo, 'main'); |
| 79 | |
| 80 | fork = ( |
| 81 | await api() |
| 82 | .post(`${repos}${repo}/fork`) |
| 83 | .set('cookie', forker.cookie) |
| 84 | .send({ name: 'fork', visibility: 'public' }) |
| 85 | .expect(201) |
| 86 | ).body.slug; |
| 87 | for (const branch of ['feature', 'abandoned']) { |
| 88 | git('checkout', '-q', '-b', branch, 'main'); |
| 89 | git('commit', '-q', '--allow-empty', '-m', branch); |
| 90 | } |
| 91 | await push(contributor, forker.key, fork, 'feature', 'abandoned'); |
| 92 | }, 120_000); |
| 93 | |
| 94 | afterAll(async () => { |
| 95 | await app?.close(); |
| 96 | if (work) rmSync(work, { recursive: true, force: true }); |
| 97 | }); |
| 98 | |
| 99 | it('refuses to delete a fork while it heads an open pull request', async () => { |
| 100 | const number = await openPull('feature'); |
| 101 | |
| 102 | const { body } = await deleteFork().expect(409); |
| 103 | expect(body.message).toContain(`${owner}/${repo}#${number}`); |
| 104 | }); |
| 105 | |
| 106 | it('deletes the fork once its pull requests are merged or closed, and keeps them', async () => { |
| 107 | const abandoned = await openPull('abandoned'); |
| 108 | await api() |
| 109 | .post(pull('1/merge')) |
| 110 | .set('cookie', upstream.cookie) |
| 111 | .send({}) |
| 112 | .expect(201); |
| 113 | await api() |
| 114 | .patch(pull(`${abandoned}/close`)) |
| 115 | .set('cookie', forker.cookie) |
| 116 | .expect(200); |
| 117 | |
| 118 | await deleteFork().expect(204); |
| 119 | |
| 120 | const merged = await api().get(pull('1')).expect(200); |
| 121 | expect(merged.body).toMatchObject({ |
| 122 | state: 'merged', |
| 123 | head: { username: null, slug: null, ref: 'feature' }, |
| 124 | commitCount: 1, |
| 125 | }); |
| 126 | const commits = await api().get(pull('1/commits')).expect(200); |
| 127 | expect( |
| 128 | commits.body.commits.map((c: { subject: string }) => c.subject), |
| 129 | ).toEqual(['feature']); |
| 130 | |
| 131 | const closed = await api() |
| 132 | .get(pull(`${abandoned}`)) |
| 133 | .expect(200); |
| 134 | expect(closed.body).toMatchObject({ |
| 135 | state: 'closed', |
| 136 | head: { username: null, ref: 'abandoned' }, |
| 137 | commitCount: 0, |
| 138 | }); |
| 139 | await api() |
| 140 | .get(pull(`${abandoned}/patch`)) |
| 141 | .expect(410); |
| 142 | }); |
| 143 | }, |
| 144 | ); |