* Builds a repository-file document from a fetched (non-raw) file response. Returns * null for binary, oversized, or empty files so they are not indexed.
( apiBase: string, encodedProject: string, host: string, projectPath: string, ref: string, path: string, file: GitLabFile )
| 320 | * null for binary, oversized, or empty files so they are not indexed. |
| 321 | */ |
| 322 | function fileToDocument( |
| 323 | apiBase: string, |
| 324 | encodedProject: string, |
| 325 | host: string, |
| 326 | projectPath: string, |
| 327 | ref: string, |
| 328 | path: string, |
| 329 | file: GitLabFile |
| 330 | ): ExternalDocument | null { |
| 331 | const blobSha = file.blob_id?.trim() |
| 332 | if (!blobSha) return null |
| 333 | |
| 334 | const title = path.split('/').pop() || path |
| 335 | const skippedForSize = (size: number): ExternalDocument => { |
| 336 | logger.info('Skipping oversized GitLab file', { path, size }) |
| 337 | return markSkipped( |
| 338 | { |
| 339 | externalId: `${FILE_PREFIX}${path}`, |
| 340 | title, |
| 341 | content: '', |
| 342 | mimeType: 'text/plain', |
| 343 | sourceUrl: buildFileSourceUrl(apiBase, encodedProject, host, projectPath, ref, path), |
| 344 | contentHash: buildFileContentHash(encodedProject, path, blobSha), |
| 345 | metadata: { contentType: 'file', title, path, size }, |
| 346 | }, |
| 347 | sizeLimitSkipReason(MAX_FILE_SIZE) |
| 348 | ) |
| 349 | } |
| 350 | |
| 351 | if (typeof file.size === 'number' && file.size > MAX_FILE_SIZE) { |
| 352 | return skippedForSize(file.size) |
| 353 | } |
| 354 | |
| 355 | const raw = typeof file.content === 'string' ? file.content : '' |
| 356 | const buffer = file.encoding === 'base64' ? Buffer.from(raw, 'base64') : Buffer.from(raw, 'utf8') |
| 357 | if (isBinaryBuffer(buffer)) { |
| 358 | logger.info('Skipping binary GitLab file', { path }) |
| 359 | return null |
| 360 | } |
| 361 | if (buffer.byteLength > MAX_FILE_SIZE) { |
| 362 | return skippedForSize(buffer.byteLength) |
| 363 | } |
| 364 | |
| 365 | const content = buffer.toString('utf8') |
| 366 | const body = composeBody(title, content) |
| 367 | if (!body.trim()) return null |
| 368 | |
| 369 | return { |
| 370 | externalId: `${FILE_PREFIX}${path}`, |
| 371 | title, |
| 372 | content: body, |
| 373 | contentDeferred: false, |
| 374 | mimeType: 'text/plain', |
| 375 | sourceUrl: buildFileSourceUrl(apiBase, encodedProject, host, projectPath, ref, path), |
| 376 | contentHash: buildFileContentHash(encodedProject, path, blobSha), |
| 377 | metadata: { |
| 378 | contentType: 'file', |
| 379 | title, |
no test coverage detected