@@ -7,13 +7,16 @@ import { CodeHostType, isServiceError } from "@/lib/utils";
77import { prisma } from "@/prisma" ;
88import { render } from "@react-email/components" ;
99import * as Sentry from '@sentry/nextjs' ;
10- import { decrypt , encrypt , generateApiKey , hashSecret } from "@sourcebot/crypto" ;
10+ import { decrypt , encrypt , generateApiKey , hashSecret , getTokenFromConfig } from "@sourcebot/crypto" ;
1111import { ConnectionSyncStatus , OrgRole , Prisma , RepoIndexingStatus , StripeSubscriptionStatus , Org , ApiKey } from "@sourcebot/db" ;
1212import { ConnectionConfig } from "@sourcebot/schemas/v3/connection.type" ;
1313import { gerritSchema } from "@sourcebot/schemas/v3/gerrit.schema" ;
1414import { giteaSchema } from "@sourcebot/schemas/v3/gitea.schema" ;
1515import { githubSchema } from "@sourcebot/schemas/v3/github.schema" ;
1616import { gitlabSchema } from "@sourcebot/schemas/v3/gitlab.schema" ;
17+ import { GithubConnectionConfig } from "@sourcebot/schemas/v3/github.type" ;
18+ import { GitlabConnectionConfig } from "@sourcebot/schemas/v3/gitlab.type" ;
19+ import { GiteaConnectionConfig } from "@sourcebot/schemas/v3/gitea.type" ;
1720import Ajv from "ajv" ;
1821import { StatusCodes } from "http-status-codes" ;
1922import { cookies , headers } from "next/headers" ;
@@ -1712,6 +1715,76 @@ export const getSearchContexts = async (domain: string) => sew(() =>
17121715 } , /* minRequiredRole = */ OrgRole . GUEST ) , /* allowSingleTenantUnauthedAccess = */ true
17131716 ) ) ;
17141717
1718+ export const getRepoImage = async ( repoId : number , domain : string ) : Promise < ArrayBuffer | ServiceError > => sew ( async ( ) => {
1719+ return await withAuth ( async ( userId ) => {
1720+ return await withOrgMembership ( userId , domain , async ( { org } ) => {
1721+ const repo = await prisma . repo . findUnique ( {
1722+ where : {
1723+ id : repoId ,
1724+ orgId : org . id ,
1725+ } ,
1726+ include : {
1727+ connections : {
1728+ include : {
1729+ connection : true ,
1730+ }
1731+ }
1732+ }
1733+ } ) ;
1734+
1735+ if ( ! repo || ! repo . imageUrl ) {
1736+ return notFound ( ) ;
1737+ }
1738+
1739+ const authHeaders : Record < string , string > = { } ;
1740+ for ( const { connection } of repo . connections ) {
1741+ try {
1742+ if ( connection . connectionType === 'github' ) {
1743+ const config = connection . config as unknown as GithubConnectionConfig ;
1744+ if ( config . token ) {
1745+ const token = await getTokenFromConfig ( config . token , connection . orgId , prisma ) ;
1746+ authHeaders [ 'Authorization' ] = `token ${ token } ` ;
1747+ break ;
1748+ }
1749+ } else if ( connection . connectionType === 'gitlab' ) {
1750+ const config = connection . config as unknown as GitlabConnectionConfig ;
1751+ if ( config . token ) {
1752+ const token = await getTokenFromConfig ( config . token , connection . orgId , prisma ) ;
1753+ authHeaders [ 'PRIVATE-TOKEN' ] = token ;
1754+ break ;
1755+ }
1756+ } else if ( connection . connectionType === 'gitea' ) {
1757+ const config = connection . config as unknown as GiteaConnectionConfig ;
1758+ if ( config . token ) {
1759+ const token = await getTokenFromConfig ( config . token , connection . orgId , prisma ) ;
1760+ authHeaders [ 'Authorization' ] = `token ${ token } ` ;
1761+ break ;
1762+ }
1763+ }
1764+ } catch ( error ) {
1765+ logger . warn ( `Failed to get token for connection ${ connection . id } :` , error ) ;
1766+ }
1767+ }
1768+
1769+ try {
1770+ const response = await fetch ( repo . imageUrl , {
1771+ headers : authHeaders ,
1772+ } ) ;
1773+
1774+ if ( ! response . ok ) {
1775+ logger . warn ( `Failed to fetch image from ${ repo . imageUrl } : ${ response . status } ` ) ;
1776+ return notFound ( ) ;
1777+ }
1778+
1779+ const imageBuffer = await response . arrayBuffer ( ) ;
1780+ return imageBuffer ;
1781+ } catch ( error ) {
1782+ logger . error ( `Error proxying image for repo ${ repoId } :` , error ) ;
1783+ return notFound ( ) ;
1784+ }
1785+ } , /* minRequiredRole = */ OrgRole . GUEST ) ;
1786+ } , /* allowSingleTenantUnauthedAccess = */ true ) ;
1787+ } ) ;
17151788
17161789////// Helpers ///////
17171790
0 commit comments