| 1 | import { execFile, execFileSync } from 'node:child_process'; |
| 2 | import { mkdtempSync, rmSync, writeFileSync } 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)('squash and rebase merges', () => { |
| 13 | let app: INestApplication; |
| 14 | let origin: string; |
| 15 | let owner: { cookie: string; key: string }; |
| 16 | let work: string; |
| 17 | const username = `merger${Date.now()}`; |
| 18 | let repo: string; |
| 19 | |
| 20 | const api = () => request(app.getHttpServer()); |
| 21 | const pulls = (suffix = '') => |
| 22 | `/api/repositories/${username}/${repo}/pulls${suffix}`; |
| 23 | const git = (...args: string[]) => |
| 24 | execFileSync( |
| 25 | 'git', |
| 26 | [ |
| 27 | '-c', |
| 28 | 'user.name=Branch Author', |
| 29 | '-c', |
| 30 | 'user.email=branch@example.com', |
| 31 | ...args, |
| 32 | ], |
| 33 | { cwd: work, encoding: 'utf8' }, |
| 34 | ).trim(); |
| 35 | // The server runs in this process, so anything that talks to it must not block the event loop. |
| 36 | const remote = (verb: 'push' | 'fetch', ...refs: string[]) => |
| 37 | promisify(execFile)( |
| 38 | 'git', |
| 39 | [ |
| 40 | verb, |
| 41 | '-q', |
| 42 | `${origin.replace('://', `://${username}:${owner.key}@`)}/${username}/${repo}.git`, |
| 43 | ...refs, |
| 44 | ], |
| 45 | { cwd: work, env: { ...process.env, GIT_TERMINAL_PROMPT: '0' } }, |
| 46 | ); |
| 47 | const branch = (name: string, files: Record<string, string>) => { |
| 48 | git('checkout', '-q', '-b', name, 'main'); |
| 49 | for (const [file, content] of Object.entries(files)) { |
| 50 | writeFileSync(path.join(work, file), content); |
| 51 | git('add', file); |
| 52 | git( |
| 53 | 'commit', |
| 54 | '-q', |
| 55 | '-m', |
| 56 | `edit ${file}`, |
| 57 | '-m', |
| 58 | `closes nothing in ${file}`, |
| 59 | ); |
| 60 | } |
| 61 | }; |
| 62 | const open = (head: string) => |
| 63 | api() |
| 64 | .post(pulls()) |
| 65 | .set('cookie', owner.cookie) |
| 66 | .send({ title: `Land ${head}`, base: 'main', head }) |
| 67 | .expect(201) |
| 68 | .then((response) => response.body.number as number); |
| 69 | const merge = (number: number, method: string) => |
| 70 | api() |
| 71 | .post(pulls(`/${number}/merge`)) |
| 72 | .set('cookie', owner.cookie) |
| 73 | .send({ method }); |
| 74 | const mainTip = async () => { |
| 75 | await remote('fetch', 'main:refs/remotes/server/main'); |
| 76 | return 'refs/remotes/server/main'; |
| 77 | }; |
| 78 | |
| 79 | beforeAll(async () => { |
| 80 | ({ app, origin } = await startApp()); |
| 81 | owner = await signUp(app, username); |
| 82 | repo = ( |
| 83 | await api() |
| 84 | .post('/api/repositories') |
| 85 | .set('cookie', owner.cookie) |
| 86 | .send({ name: 'methods', visibility: 'public' }) |
| 87 | .expect(201) |
| 88 | ).body.slug; |
| 89 | |
| 90 | work = mkdtempSync(path.join(tmpdir(), 'ghost-e2e-methods-')); |
| 91 | git('init', '-q', '-b', 'main'); |
| 92 | writeFileSync(path.join(work, 'shared.txt'), 'shared\n'); |
| 93 | git('add', '.'); |
| 94 | git('commit', '-q', '-m', 'first'); |
| 95 | branch('squashed', { 'a.txt': 'a\n', 'b.txt': 'b\n' }); |
| 96 | branch('rebased', { 'c.txt': 'c\n', 'd.txt': 'd\n' }); |
| 97 | branch('edited', { 'e.txt': 'e\n' }); |
| 98 | branch('clashing', { 'shared.txt': 'from the branch\n' }); |
| 99 | git('checkout', '-q', 'main'); |
| 100 | writeFileSync(path.join(work, 'shared.txt'), 'from main\n'); |
| 101 | git('commit', '-q', '-am', 'main moves on'); |
| 102 | await remote('push', 'main', 'squashed', 'edited', 'rebased', 'clashing'); |
| 103 | }, 120_000); |
| 104 | |
| 105 | afterAll(async () => { |
| 106 | await app?.close(); |
| 107 | if (work) rmSync(work, { recursive: true, force: true }); |
| 108 | }); |
| 109 | |
| 110 | it('refuses a method it does not know', async () => { |
| 111 | const number = await open('squashed'); |
| 112 | await merge(number, 'octopus').expect(400); |
| 113 | }); |
| 114 | |
| 115 | it('squashes the request into one commit credited to its author, and keeps its commits readable', async () => { |
| 116 | const { body } = await merge(1, 'squash').expect(201); |
| 117 | const tip = await mainTip(); |
| 118 | |
| 119 | expect(git('rev-parse', tip)).toBe(body.mergeCommitSha); |
| 120 | expect(git('log', '-1', '--format=%P', tip).split(' ')).toHaveLength(1); |
| 121 | expect(git('log', '-1', '--format=%s', tip)).toBe('Land squashed (#1)'); |
| 122 | expect(git('log', '-1', '--format=%ae', tip)).toBe( |
| 123 | `${username}@example.com`, |
| 124 | ); |
| 125 | expect(git('log', '-1', '--format=%b', tip)).toContain( |
| 126 | '* edit a.txt\n\ncloses nothing in a.txt', |
| 127 | ); |
| 128 | expect(git('ls-tree', '--name-only', tip).split('\n')).toEqual([ |
| 129 | 'a.txt', |
| 130 | 'b.txt', |
| 131 | 'shared.txt', |
| 132 | ]); |
| 133 | |
| 134 | const commits = await api().get(pulls('/1/commits')).expect(200); |
| 135 | expect( |
| 136 | commits.body.commits.map((commit: { subject: string }) => commit.subject), |
| 137 | ).toEqual(['edit b.txt', 'edit a.txt']); |
| 138 | const files = await api().get(pulls('/1/files')).expect(200); |
| 139 | expect(files.body.files.map((file: { path: string }) => file.path)).toEqual( |
| 140 | ['a.txt', 'b.txt'], |
| 141 | ); |
| 142 | }); |
| 143 | |
| 144 | it('offers the squash message it would use, and commits the one the merger wrote instead', async () => { |
| 145 | const number = await open('edited'); |
| 146 | const { body: pull } = await api() |
| 147 | .get(pulls(`/${number}`)) |
| 148 | .expect(200); |
| 149 | expect(pull.squash).toEqual({ |
| 150 | title: `Land edited (#${number})`, |
| 151 | message: '* edit e.txt\n\ncloses nothing in e.txt', |
| 152 | }); |
| 153 | |
| 154 | await api() |
| 155 | .post(pulls(`/${number}/merge`)) |
| 156 | .set('cookie', owner.cookie) |
| 157 | .send({ method: 'squash', title: '' }) |
| 158 | .expect(400); |
| 159 | await api() |
| 160 | .post(pulls(`/${number}/merge`)) |
| 161 | .set('cookie', owner.cookie) |
| 162 | .send({ method: 'squash', title: 'Rewritten', message: '' }) |
| 163 | .expect(201); |
| 164 | expect(git('log', '-1', '--format=%B', await mainTip())).toBe('Rewritten'); |
| 165 | expect( |
| 166 | ( |
| 167 | await api() |
| 168 | .get(pulls(`/${number}`)) |
| 169 | .expect(200) |
| 170 | ).body.squash, |
| 171 | ).toBeNull(); |
| 172 | }); |
| 173 | |
| 174 | it('rebases each commit onto the base, keeping its author', async () => { |
| 175 | const number = await open('rebased'); |
| 176 | const before = git('rev-parse', await mainTip()); |
| 177 | |
| 178 | const { body } = await merge(number, 'rebase').expect(201); |
| 179 | const tip = await mainTip(); |
| 180 | expect(git('rev-parse', tip)).toBe(body.mergeCommitSha); |
| 181 | expect(git('log', '--format=%s|%an|%P', `${before}..${tip}`)).toBe( |
| 182 | [ |
| 183 | `edit d.txt|Branch Author|${git('rev-parse', `${tip}^`)}`, |
| 184 | `edit c.txt|Branch Author|${before}`, |
| 185 | ].join('\n'), |
| 186 | ); |
| 187 | |
| 188 | const pull = await api() |
| 189 | .get(pulls(`/${number}`)) |
| 190 | .expect(200); |
| 191 | expect(pull.body.state).toBe('merged'); |
| 192 | expect(pull.body.commitCount).toBe(2); |
| 193 | }); |
| 194 | |
| 195 | it('rejects a rebase that conflicts and names the path', async () => { |
| 196 | const number = await open('clashing'); |
| 197 | const { body } = await merge(number, 'rebase').expect(409); |
| 198 | expect(body.message).toContain('shared.txt'); |
| 199 | expect( |
| 200 | ( |
| 201 | await api() |
| 202 | .get(pulls(`/${number}`)) |
| 203 | .expect(200) |
| 204 | ).body.state, |
| 205 | ).toBe('open'); |
| 206 | }); |
| 207 | |
| 208 | it('refuses a rebase whose head holds nothing but merge commits', async () => { |
| 209 | const tip = git('rev-parse', await mainTip()); |
| 210 | const mergeOnly = git( |
| 211 | 'commit-tree', |
| 212 | `${tip}^{tree}`, |
| 213 | '-p', |
| 214 | tip, |
| 215 | '-p', |
| 216 | `${tip}^`, |
| 217 | '-m', |
| 218 | 'merge only', |
| 219 | ); |
| 220 | await remote('push', `${mergeOnly}:refs/heads/merges-only`); |
| 221 | |
| 222 | const number = await open('merges-only'); |
| 223 | const { body } = await merge(number, 'rebase').expect(409); |
| 224 | expect(body.message).toContain('already contains'); |
| 225 | }); |
| 226 | }); |