| 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 | |
| 10 | import { PackProcessService } |
| 11 | import { RefAdvertisementService } |
| 12 | import { RepositoryMaterializerService } |
| 13 | import { PushTransactionService } |
| 14 | import { RepositoryStorageService } |
| 15 | import { RepositoryContributionService } |
| 16 | import { CodeSearchService } |
| 17 | import { IssueReferencesService } |
| 18 | import { UnsupportedGitServiceError } |
| 19 | import { DATABASE } |
| 20 | import { GitService } |
| 21 | |
| 22 | describe('GitService', () |
| 23 | let service: |
| 24 | const storage = |
| 25 | getRepoPath: vi.fn |
| 26 | }; |
| 27 | const refAdvertisement = |
| 28 | advertise: vi.fn |
| 29 | }; |
| 30 | const packProcess = |
| 31 | streamUploadPack: vi.fn |
| 32 | streamReceivePack: vi.fn |
| 33 | }; |
| 34 | const materializer = |
| 35 | const pushTransaction = |
| 36 | commitPush: vi |
| 37 | .fn() |
| 38 | .mockResolvedValue({ seq: |
| 39 | }; |
| 40 | const contributions = |
| 41 | sync: vi.fn |
| 42 | }; |
| 43 | |
| 44 | const codeSearch = |
| 45 | const references = |
| 46 | const published = |
| 47 | const db = |
| 48 | |
| 49 | beforeEach(async () |
| 50 | vi.clearAllMocks(); |
| 51 | const module: |
| 52 | providers: [ |
| 53 | GitService, |
| 54 | { provide: RepositoryStorageService, |
| 55 | { provide: RefAdvertisementService, |
| 56 | { provide: PackProcessService, |
| 57 | { provide: PushTransactionService, |
| 58 | { provide: RepositoryMaterializerService, |
| 59 | { provide: RepositoryContributionService, |
| 60 | { provide: CodeSearchService, |
| 61 | { provide: IssueReferencesService, |
| 62 | { provide: DATABASE, |
| 63 | ], |
| 64 | }).compile(); |
| 65 | |
| 66 | service = module. |
| 67 | }); |
| 68 | |
| 69 | it('should be defined', |
| 70 | expect(service).toBeDefined |
| 71 | }); |
| 72 | |
| 73 | it('materializes the cache before advertising refs', |
| 74 | const { headers |
| 75 | repositoryId: 'repo_ghost', |
| 76 | defaultBranch: null, |
| 77 | service: 'git-upload-pack', |
| 78 | }); |
| 79 | |
| 80 | expect(headers['Content-Type |
| 81 | 'application/x-git-upload-pack-advertisement', |
| 82 | ); |
| 83 | expect(materializer.materialize |
| 84 | 'repo_ghost', |
| 85 | '/repos/ghost.git', |
| 86 | null, |
| 87 | ); |
| 88 | expect(refAdvertisement.advertise |
| 89 | repoDirectory: '/repos/ghost.git', |
| 90 | service: 'git-upload-pack', |
| 91 | }); |
| 92 | }); |
| 93 | |
| 94 | it('rejects the dumb protocol', |
| 95 | await expect( |
| 96 | service.advertiseRefs({ |
| 97 | repositoryId: 'repo_ghost', |
| 98 | defaultBranch: null, |
| 99 | service: '', |
| 100 | }), |
| 101 | ).rejects.toBeInstanceOf( |
| 102 | }); |
| 103 | |
| 104 | it('reads the command section before doing any slower work', |
| 105 | materializer.materialize.mockImplementationOnce |
| 106 | expect(pushTransaction.commitPush |
| 107 | }); |
| 108 | |
| 109 | await service.receivePack |
| 110 | repositoryId: 'repo_ghost', |
| 111 | defaultBranch: null, |
| 112 | isPublic: true, |
| 113 | body: bufferBody(receivePackBody |
| 114 | }); |
| 115 | |
| 116 | expect(materializer.materialize |
| 117 | }); |
| 118 | |
| 119 | it('answers the pre-push probe without touching the log', |
| 120 | const { headers |
| 121 | repositoryId: 'repo_ghost', |
| 122 | defaultBranch: null, |
| 123 | isPublic: true, |
| 124 | body: bufferBody(Buffer |
| 125 | }); |
| 126 | |
| 127 | expect(headers['Content-Type |
| 128 | 'application/x-git-receive-pack-result', |
| 129 | ); |
| 130 | expect(pushTransaction.commitPush |
| 131 | expect(packProcess.streamReceivePack |
| 132 | }); |
| 133 | |
| 134 | it('commits to the log before touching the local repository', |
| 135 | const { headers |
| 136 | repositoryId: 'repo_ghost', |
| 137 | defaultBranch: null, |
| 138 | isPublic: true, |
| 139 | body: bufferBody(receivePackBody |
| 140 | }); |
| 141 | |
| 142 | expect(headers['Content-Type |
| 143 | 'application/x-git-receive-pack-result', |
| 144 | ); |
| 145 | expect(pushTransaction.commitPush |
| 146 | expect.objectContaining({ |
| 147 | ); |
| 148 | |
| 149 | const [{ transitions |
| 150 | expect(transitions[0] |
| 151 | |
| 152 | const materializeOrder = |
| 153 | materializer.materialize.mock |
| 154 | const commitOrder = |
| 155 | const spawnOrder = |
| 156 | packProcess.streamReceivePack.mock |
| 157 | expect(materializeOrder).toBeLessThan |
| 158 | expect(commitOrder).toBeLessThan |
| 159 | }); |
| 160 | |
| 161 | it('indexes contributions once the pushed pack finishes streaming', |
| 162 | const { body |
| 163 | repositoryId: 'repo_ghost', |
| 164 | defaultBranch: null, |
| 165 | isPublic: true, |
| 166 | body: bufferBody(receivePackBody |
| 167 | }); |
| 168 | |
| 169 | expect(contributions.sync |
| 170 | |
| 171 | body.emit('close |
| 172 | await new Promise |
| 173 | |
| 174 | expect(contributions.sync |
| 175 | repositoryId: 'repo_ghost', |
| 176 | repoDirectory: '/repos/ghost.git', |
| 177 | }); |
| 178 | expect(codeSearch.indexInBackground |
| 179 | repositoryId: 'repo_ghost', |
| 180 | isPublic: true, |
| 181 | repoDirectory: '/repos/ghost.git', |
| 182 | }); |
| 183 | // creating a branch closes nothing |
| 184 | expect(references.closeFromCommits |
| 185 | }); |
| 186 | |
| 187 | describe('closing referenced issues', |
| 188 | let directory: |
| 189 | const git = |
| 190 | execFileSync('git', |
| 191 | encoding: 'utf8', |
| 192 | env: { |
| 193 | ...process.env, |
| 194 | GIT_AUTHOR_NAME: 'a', |
| 195 | GIT_AUTHOR_EMAIL: 'a@example.com', |
| 196 | GIT_COMMITTER_NAME: 'a', |
| 197 | GIT_COMMITTER_EMAIL: 'a@example.com', |
| 198 | }, |
| 199 | }).trim(); |
| 200 | |
| 201 | beforeEach(() => |
| 202 | directory = mkdtempSync( |
| 203 | git('init', |
| 204 | git('commit', |
| 205 | git('commit', |
| 206 | storage.getRepoPath.mockResolvedValueOnce |
| 207 | }); |
| 208 | |
| 209 | afterEach(() => |
| 210 | |
| 211 | async function push |
| 212 | const { body |
| 213 | repositoryId: 'repo_ghost', |
| 214 | defaultBranch: null, |
| 215 | isPublic: true, |
| 216 | body: bufferBody( |
| 217 | receivePackBody( |
| 218 | git('rev-parse', |
| 219 | git('rev-parse', |
| 220 | ref, |
| 221 | ), |
| 222 | ), |
| 223 | pushedBy: 'user_pusher', |
| 224 | }); |
| 225 | body.emit('close |
| 226 | await vi.waitFor |
| 227 | } |
| 228 | |
| 229 | it('hands the commits a default-branch push added to the reference index', |
| 230 | await push('refs/heads/main |
| 231 | |
| 232 | const sha = |
| 233 | await vi.waitFor |
| 234 | expect(references.closeFromCommits |
| 235 | repository: { id: ' |
| 236 | actorId: 'user_pusher', |
| 237 | commits: [expect.objectContaining |
| 238 | }), |
| 239 | ); |
| 240 | }); |
| 241 | |
| 242 | it('publishes a push event with the commits the push added', |
| 243 | await push('refs/heads/feature |
| 244 | |
| 245 | const [before, |
| 246 | git('rev-parse', |
| 247 | git('rev-parse', |
| 248 | ]; |
| 249 | await vi.waitFor |
| 250 | expect(published).toHaveBeenCalledWith |
| 251 | type: 'push', |
| 252 | repositoryId: 'repo_ghost', |
| 253 | actorId: 'user_pusher', |
| 254 | payload: { |
| 255 | ref: 'refs/heads/feature', |
| 256 | before, |
| 257 | after, |
| 258 | commits: [ |
| 259 | expect.objectContaining({ |
| 260 | sha: after, |
| 261 | subject: 'second', |
| 262 | body: 'Fixes #1', |
| 263 | authorName: 'a', |
| 264 | }), |
| 265 | ], |
| 266 | }, |
| 267 | }), |
| 268 | ); |
| 269 | }); |
| 270 | |
| 271 | it('publishes nothing for a ref outside branches and tags', |
| 272 | await push('refs/pull/1/head |
| 273 | await new Promise |
| 274 | |
| 275 | expect(published).not |
| 276 | expect.objectContaining({ |
| 277 | ); |
| 278 | }); |
| 279 | |
| 280 | it('ignores pushes to other branches', |
| 281 | await push('refs/heads/feature |
| 282 | // nothing to wait for when nothing happens, so give the hook time to have run |
| 283 | await new Promise |
| 284 | |
| 285 | expect(references.closeFromCommits |
| 286 | }); |
| 287 | }); |
| 288 | }); |
| 289 | |
| 290 | function receivePackBody( |
| 291 | old = '0' |
| 292 | next = '55ff3318cbb1ad74a1e1a1e6f4bd91f4b5a9c0d2' |
| 293 | ref = 'refs/heads/main' |
| 294 | ) { |
| 295 | const command = |
| 296 | const length = |
| 297 | return Buffer.concat |
| 298 | Buffer.from(length |
| 299 | Buffer.from('PACKDATA |
| 300 | ]); |
| 301 | } |