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