(repoId: number)
| 499 | })); |
| 500 | |
| 501 | export const getRepoImage = async (repoId: number): Promise<ArrayBuffer | ServiceError> => sew(async () => { |
| 502 | return await withOptionalAuth(async ({ org, prisma }) => { |
| 503 | const repo = await prisma.repo.findUnique({ |
| 504 | where: { |
| 505 | id: repoId, |
| 506 | orgId: org.id, |
| 507 | }, |
| 508 | include: { |
| 509 | connections: { |
| 510 | include: { |
| 511 | connection: true, |
| 512 | } |
| 513 | } |
| 514 | }, |
| 515 | }); |
| 516 | |
| 517 | if (!repo || !repo.imageUrl) { |
| 518 | return notFound(); |
| 519 | } |
| 520 | |
| 521 | const authHeaders: Record<string, string> = {}; |
| 522 | for (const { connection } of repo.connections) { |
| 523 | try { |
| 524 | if (connection.connectionType === 'github') { |
| 525 | const config = connection.config as unknown as GithubConnectionConfig; |
| 526 | if (config.token) { |
| 527 | const token = await getTokenFromConfig(config.token); |
| 528 | authHeaders['Authorization'] = `token ${token}`; |
| 529 | break; |
| 530 | } |
| 531 | } else if (connection.connectionType === 'gitlab') { |
| 532 | const config = connection.config as unknown as GitlabConnectionConfig; |
| 533 | if (config.token) { |
| 534 | const token = await getTokenFromConfig(config.token); |
| 535 | authHeaders['PRIVATE-TOKEN'] = token; |
| 536 | break; |
| 537 | } |
| 538 | } else if (connection.connectionType === 'gitea') { |
| 539 | const config = connection.config as unknown as GiteaConnectionConfig; |
| 540 | if (config.token) { |
| 541 | const token = await getTokenFromConfig(config.token); |
| 542 | authHeaders['Authorization'] = `token ${token}`; |
| 543 | break; |
| 544 | } |
| 545 | } |
| 546 | } catch (error) { |
| 547 | logger.warn(`Failed to get token for connection ${connection.id}:`, error); |
| 548 | } |
| 549 | } |
| 550 | |
| 551 | try { |
| 552 | const response = await fetch(repo.imageUrl, { |
| 553 | headers: authHeaders, |
| 554 | }); |
| 555 | |
| 556 | if (!response.ok) { |
| 557 | logger.warn(`Failed to fetch image from ${repo.imageUrl}: ${response.status}`); |
| 558 | return notFound(); |
no test coverage detected