| 1 | import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest'; |
| 2 | import { Test, TestingModule |
| 3 | import { bufferBody } |
| 4 | import { execFileSync } |
| 5 | import { mkdtempSync, rmSync |
| 6 | import { tmpdir } |
| 7 | import path from 'node:path |
| 8 | import { PassThrough } |
| 9 | import { buffer as |
| 10 | |
| 11 | import { PackProcessService } |
| 12 | import { RefAdvertisementService } |
| 13 | import { RepositoryMaterializerService } |
| 14 | import { PushTransactionService } |
| 15 | import { RepositoryContributionService } |
| 16 | import { CodeSearchService } |
| 17 | import { IssueReferencesService } |
| 18 | import { PullRefsService } |
| 19 | import { PullRequestPushesService } |
| 20 | import { |
| 21 | ProtectedRefError, |
| 22 | UnsupportedGitServiceError, |
| 23 | } from '../lib/git/git.errors.js'; |
| 24 | import { StorageQuotaService } |
| 25 | import { DATABASE } |
| 26 | import { GitService } |
| 27 | |
| 28 | // Verification runs real git over a real pack; it has its own spec, and these bodies are stand-ins. |
| 29 | vi.mock('../lib/git/protocol/verify-push.js' |
| 30 | withVerifiedPack: ( |
| 31 | { body, packOffset |
| 32 | commit: (pack |
| 33 | ) => commit( |
| 34 | })); |
| 35 | |
| 36 | describe('GitService', () |
| 37 | let service: |
| 38 | const refAdvertisement = |
| 39 | advertise: vi.fn |
| 40 | }; |
| 41 | const packProcess = |
| 42 | streamUploadPack: vi.fn |
| 43 | streamReceivePack: vi.fn |
| 44 | }; |
| 45 | const materializer = |
| 46 | const pushTransaction = |
| 47 | commitPush: vi |
| 48 | .fn() |
| 49 | .mockResolvedValue({ seq: |
| 50 | }; |
| 51 | const contributions = |
| 52 | sync: vi.fn |
| 53 | }; |
| 54 | |
| 55 | const codeSearch = |
| 56 | const pullRefs = |
| 57 | const pullRequestPushes = |
| 58 | recordPush: vi.fn |
| 59 | }; |
| 60 | const references = |
| 61 | const published = |
| 62 | const runningImports = |
| 63 | const pushedAt = |
| 64 | const db = |
| 65 | insert: () => |
| 66 | update: () => |
| 67 | select: () => |
| 68 | }; |
| 69 | const logged = |
| 70 | const quota = |
| 71 | reserve: vi.fn |
| 72 | ( |
| 73 | _account: unknown, |
| 74 | _kind: unknown, |
| 75 | _bytes: number, |
| 76 | insert: (tx: |
| 77 | ) => |
| 78 | insert({ |
| 79 | insert: () => |
| 80 | values: (row: |
| 81 | onConflictDoNothing: () => |
| 82 | }), |
| 83 | }), |
| 84 | }), |
| 85 | ), |
| 86 | }; |
| 87 | const repository = |
| 88 | id: 'repo_ghost', |
| 89 | defaultBranch: null, |
| 90 | visibility: 'public' as |
| 91 | ownerId: 'user_owner', |
| 92 | organizationId: null, |
| 93 | parentRepositoryId: null, |
| 94 | }; |
| 95 | |
| 96 | beforeEach(async () |
| 97 | vi.clearAllMocks(); |
| 98 | const module: |
| 99 | providers: [ |
| 100 | GitService, |
| 101 | { provide: RefAdvertisementService, |
| 102 | { provide: PackProcessService, |
| 103 | { provide: PushTransactionService, |
| 104 | { provide: RepositoryMaterializerService, |
| 105 | { provide: RepositoryContributionService, |
| 106 | { provide: CodeSearchService, |
| 107 | { provide: IssueReferencesService, |
| 108 | { provide: PullRefsService, |
| 109 | { provide: PullRequestPushesService, |
| 110 | { provide: StorageQuotaService, |
| 111 | { provide: DATABASE, |
| 112 | ], |
| 113 | }).compile(); |
| 114 | |
| 115 | service = module. |
| 116 | }); |
| 117 | |
| 118 | it('should be defined', |
| 119 | expect(service).toBeDefined |
| 120 | }); |
| 121 | |
| 122 | it('materializes the cache before advertising refs', |
| 123 | const { headers |
| 124 | repository: { id: ' |
| 125 | service: 'git-upload-pack', |
| 126 | }); |
| 127 | |
| 128 | expect(headers['Content-Type |
| 129 | 'application/x-git-upload-pack-advertisement', |
| 130 | ); |
| 131 | expect(materializer.open |
| 132 | id: 'repo_ghost', |
| 133 | defaultBranch: null, |
| 134 | }); |
| 135 | expect(refAdvertisement.advertise |
| 136 | repoDirectory: '/repos/ghost.git', |
| 137 | service: 'git-upload-pack', |
| 138 | }); |
| 139 | }); |
| 140 | |
| 141 | it('rejects the dumb protocol', |
| 142 | await expect( |
| 143 | service.advertiseRefs({ |
| 144 | repository: { id: ' |
| 145 | service: '', |
| 146 | }), |
| 147 | ).rejects.toBeInstanceOf( |
| 148 | }); |
| 149 | |
| 150 | it('reads the command section before doing any slower work', |
| 151 | materializer.open.mockImplementationOnce |
| 152 | expect(pushTransaction.commitPush |
| 153 | }); |
| 154 | |
| 155 | await service.receivePack |
| 156 | repository, |
| 157 | body: bufferBody(receivePackBody |
| 158 | }); |
| 159 | |
| 160 | expect(materializer.open |
| 161 | }); |
| 162 | |
| 163 | it('answers the pre-push probe without touching the log', |
| 164 | const { headers |
| 165 | repository, |
| 166 | body: bufferBody(Buffer |
| 167 | }); |
| 168 | |
| 169 | expect(headers['Content-Type |
| 170 | 'application/x-git-receive-pack-result', |
| 171 | ); |
| 172 | expect(pushTransaction.commitPush |
| 173 | expect(packProcess.streamReceivePack |
| 174 | }); |
| 175 | |
| 176 | it('commits to the log before touching the local repository', |
| 177 | const { headers |
| 178 | repository, |
| 179 | body: bufferBody(receivePackBody |
| 180 | }); |
| 181 | |
| 182 | expect(headers['Content-Type |
| 183 | 'application/x-git-receive-pack-result', |
| 184 | ); |
| 185 | expect(pushTransaction.commitPush |
| 186 | expect.objectContaining({ |
| 187 | ); |
| 188 | |
| 189 | const [{ transitions |
| 190 | expect(transitions[0] |
| 191 | |
| 192 | const materializeOrder = |
| 193 | const commitOrder = |
| 194 | const spawnOrder = |
| 195 | packProcess.streamReceivePack.mock |
| 196 | expect(materializeOrder).toBeLessThan |
| 197 | expect(commitOrder).toBeLessThan |
| 198 | }); |
| 199 | |
| 200 | it('bills the pack to its account inside the quota reservation', |
| 201 | await service.receivePack |
| 202 | repository: { ...repository |
| 203 | body: bufferBody(receivePackBody |
| 204 | }); |
| 205 | |
| 206 | expect(quota.reserve |
| 207 | { userId: 'user_owner' |
| 208 | 'fork', |
| 209 | 'PACKDATA'.length, |
| 210 | expect.any(Function |
| 211 | ); |
| 212 | const [{ ulid |
| 213 | expect(logged).toHaveBeenCalledWith |
| 214 | repositoryId: 'repo_ghost', |
| 215 | ulid, |
| 216 | size: 'PACKDATA'.length, |
| 217 | }); |
| 218 | }); |
| 219 | |
| 220 | it('bills a layer the log committed when its transaction fails', |
| 221 | const billed = |
| 222 | const insert = |
| 223 | values: (row: |
| 224 | } as never); |
| 225 | quota.reserve.mockImplementationOnce |
| 226 | await quota.reserve |
| 227 | throw new Error( |
| 228 | }); |
| 229 | |
| 230 | await expect( |
| 231 | service.receivePack({ |
| 232 | ).rejects.toThrow( |
| 233 | |
| 234 | const [{ ulid |
| 235 | expect(billed).toHaveBeenCalledWith |
| 236 | repositoryId: 'repo_ghost', |
| 237 | ulid, |
| 238 | size: 'PACKDATA'.length, |
| 239 | }); |
| 240 | insert.mockRestore(); |
| 241 | }); |
| 242 | |
| 243 | it('bills nothing when the quota refuses the push before the log commits', |
| 244 | const insert = |
| 245 | quota.reserve.mockRejectedValueOnce |
| 246 | |
| 247 | await expect( |
| 248 | service.receivePack({ |
| 249 | ).rejects.toThrow( |
| 250 | |
| 251 | expect(pushTransaction.commitPush |
| 252 | expect(insert).not |
| 253 | insert.mockRestore(); |
| 254 | }); |
| 255 | |
| 256 | it('refuses a push to a pull request ref before it reaches the log, telling the client why', |
| 257 | const { body |
| 258 | repository, |
| 259 | body: bufferBody( |
| 260 | receivePackBody(undefined, undefined, |
| 261 | ), |
| 262 | apiKeyId: 'key_someone', |
| 263 | }); |
| 264 | |
| 265 | expect((await readAll |
| 266 | `ng refs/pull/1/head ${new ProtectedRefError |
| 267 | ); |
| 268 | expect(pushTransaction.commitPush |
| 269 | expect(packProcess.streamReceivePack |
| 270 | }); |
| 271 | |
| 272 | it('throws the refusal to a client that asked for no report', |
| 273 | await expect( |
| 274 | service.receivePack({ |
| 275 | repository, |
| 276 | body: bufferBody( |
| 277 | receivePackBody(undefined, undefined, |
| 278 | ), |
| 279 | }), |
| 280 | ).rejects.toBeInstanceOf( |
| 281 | }); |
| 282 | |
| 283 | it('lets an error that is not a refusal through', |
| 284 | materializer.open.mockRejectedValueOnce |
| 285 | |
| 286 | await expect( |
| 287 | service.receivePack({ |
| 288 | repository, |
| 289 | body: bufferBody(receivePackBody |
| 290 | }), |
| 291 | ).rejects.toThrow( |
| 292 | }); |
| 293 | |
| 294 | it('lets a running import write pull request refs with its own key', |
| 295 | runningImports.mockResolvedValueOnce([{ |
| 296 | |
| 297 | await service.receivePack |
| 298 | repository, |
| 299 | body: bufferBody( |
| 300 | receivePackBody(undefined, undefined, |
| 301 | ), |
| 302 | apiKeyId: 'key_import', |
| 303 | }); |
| 304 | |
| 305 | expect(pushTransaction.commitPush |
| 306 | }); |
| 307 | |
| 308 | it('refuses pull request refs to a push with no key, without asking the database', |
| 309 | const { body |
| 310 | repository, |
| 311 | body: bufferBody( |
| 312 | receivePackBody(undefined, undefined, |
| 313 | ), |
| 314 | }); |
| 315 | |
| 316 | expect((await readAll |
| 317 | expect(runningImports).not |
| 318 | }); |
| 319 | |
| 320 | it('indexes contributions once the pushed pack finishes streaming', |
| 321 | const { body |
| 322 | repository, |
| 323 | body: bufferBody(receivePackBody |
| 324 | }); |
| 325 | |
| 326 | expect(contributions.sync |
| 327 | |
| 328 | body.emit('close |
| 329 | await new Promise |
| 330 | |
| 331 | expect(contributions.sync |
| 332 | repositoryId: 'repo_ghost', |
| 333 | repoDirectory: '/repos/ghost.git', |
| 334 | }); |
| 335 | expect(codeSearch.indexInBackground |
| 336 | repositoryId: 'repo_ghost', |
| 337 | isPublic: true, |
| 338 | repoDirectory: '/repos/ghost.git', |
| 339 | }); |
| 340 | expect(pullRefs.syncAfterPush |
| 341 | repositoryId: 'repo_ghost', |
| 342 | transitions: [expect.objectContaining |
| 343 | }); |
| 344 | expect(pullRequestPushes.recordPush |
| 345 | expect.objectContaining({ |
| 346 | repositoryId: 'repo_ghost', |
| 347 | transitions: [expect.objectContaining |
| 348 | }), |
| 349 | ); |
| 350 | expect(pushedAt).toHaveBeenCalledWith |
| 351 | expect(references.closeFromCommits |
| 352 | }); |
| 353 | |
| 354 | describe('closing referenced issues', |
| 355 | let directory: |
| 356 | const git = |
| 357 | execFileSync('git', |
| 358 | encoding: 'utf8', |
| 359 | env: { |
| 360 | ...process.env, |
| 361 | GIT_AUTHOR_NAME: 'a', |
| 362 | GIT_AUTHOR_EMAIL: 'a@example.com', |
| 363 | GIT_COMMITTER_NAME: 'a', |
| 364 | GIT_COMMITTER_EMAIL: 'a@example.com', |
| 365 | }, |
| 366 | }).trim(); |
| 367 | |
| 368 | beforeEach(() => |
| 369 | directory = mkdtempSync( |
| 370 | git('init', |
| 371 | git('commit', |
| 372 | git('commit', |
| 373 | materializer.open.mockResolvedValueOnce |
| 374 | }); |
| 375 | |
| 376 | afterEach(() => |
| 377 | |
| 378 | async function push |
| 379 | const { body |
| 380 | repository, |
| 381 | body: bufferBody( |
| 382 | receivePackBody( |
| 383 | git('rev-parse', |
| 384 | git('rev-parse', |
| 385 | ref, |
| 386 | ), |
| 387 | ), |
| 388 | pushedBy: 'user_pusher', |
| 389 | }); |
| 390 | body.emit('close |
| 391 | await vi.waitFor |
| 392 | } |
| 393 | |
| 394 | it('hands the commits a default-branch push added to the reference index', |
| 395 | await push('refs/heads/main |
| 396 | |
| 397 | const sha = |
| 398 | await vi.waitFor |
| 399 | expect(references.closeFromCommits |
| 400 | repository: { id: ' |
| 401 | actorId: 'user_pusher', |
| 402 | commits: [expect.objectContaining |
| 403 | }), |
| 404 | ); |
| 405 | }); |
| 406 | |
| 407 | it('publishes a push event with the commits the push added', |
| 408 | await push('refs/heads/feature |
| 409 | |
| 410 | const [before, |
| 411 | git('rev-parse', |
| 412 | git('rev-parse', |
| 413 | ]; |
| 414 | await vi.waitFor |
| 415 | expect(published).toHaveBeenCalledWith |
| 416 | type: 'push', |
| 417 | repositoryId: 'repo_ghost', |
| 418 | actorId: 'user_pusher', |
| 419 | payload: { |
| 420 | ref: 'refs/heads/feature', |
| 421 | before, |
| 422 | after, |
| 423 | commits: [ |
| 424 | expect.objectContaining({ |
| 425 | sha: after, |
| 426 | subject: 'second', |
| 427 | body: 'Fixes #1', |
| 428 | authorName: 'a', |
| 429 | }), |
| 430 | ], |
| 431 | }, |
| 432 | }), |
| 433 | ); |
| 434 | }); |
| 435 | |
| 436 | it('publishes nothing for a ref outside branches and tags', |
| 437 | await push('refs/notes/commits |
| 438 | await new Promise |
| 439 | |
| 440 | expect(published).not |
| 441 | expect.objectContaining({ |
| 442 | ); |
| 443 | }); |
| 444 | |
| 445 | it('ignores pushes to other branches', |
| 446 | await push('refs/heads/feature |
| 447 | // nothing to wait for when nothing happens, so give the hook time to have run |
| 448 | await new Promise |
| 449 | |
| 450 | expect(references.closeFromCommits |
| 451 | }); |
| 452 | }); |
| 453 | }); |
| 454 | |
| 455 | function receivePackBody( |
| 456 | old = '0' |
| 457 | next = '55ff3318cbb1ad74a1e1a1e6f4bd91f4b5a9c0d2' |
| 458 | ref = 'refs/heads/main' |
| 459 | capabilities = 'report-status' |
| 460 | ) { |
| 461 | const command = |
| 462 | const length = |
| 463 | return Buffer.concat |
| 464 | Buffer.from(length |
| 465 | Buffer.from('PACKDATA |
| 466 | ]); |
| 467 | } |