From 8dbcf257c45d63c44f907a1b515bce49aa988b09 Mon Sep 17 00:00:00 2001 From: Andras Bacsai Date: Sun, 2 Oct 2022 09:16:51 +0000 Subject: [PATCH] fix: handle forked repositories --- .../migrations/20221002091630_forked_previews/migration.sql | 2 ++ apps/api/prisma/schema.prisma | 1 + apps/api/src/jobs/deployApplication.ts | 5 +++-- apps/api/src/routes/webhooks/github/handlers.ts | 4 +++- 4 files changed, 9 insertions(+), 3 deletions(-) create mode 100644 apps/api/prisma/migrations/20221002091630_forked_previews/migration.sql diff --git a/apps/api/prisma/migrations/20221002091630_forked_previews/migration.sql b/apps/api/prisma/migrations/20221002091630_forked_previews/migration.sql new file mode 100644 index 000000000..2312c011b --- /dev/null +++ b/apps/api/prisma/migrations/20221002091630_forked_previews/migration.sql @@ -0,0 +1,2 @@ +-- AlterTable +ALTER TABLE "Build" ADD COLUMN "sourceRepository" TEXT; diff --git a/apps/api/prisma/schema.prisma b/apps/api/prisma/schema.prisma index d70f52544..eba0ac215 100644 --- a/apps/api/prisma/schema.prisma +++ b/apps/api/prisma/schema.prisma @@ -247,6 +247,7 @@ model Build { previewApplicationId String? forceRebuild Boolean @default(false) sourceBranch String? + sourceRepository String? branch String? status String? @default("queued") createdAt DateTime @default(now()) diff --git a/apps/api/src/jobs/deployApplication.ts b/apps/api/src/jobs/deployApplication.ts index ba0281b02..a2790aadb 100644 --- a/apps/api/src/jobs/deployApplication.ts +++ b/apps/api/src/jobs/deployApplication.ts @@ -38,7 +38,7 @@ import * as buildpacks from '../lib/buildPacks'; for (const queueBuild of queuedBuilds) { actions.push(async () => { let application = await prisma.application.findUnique({ where: { id: queueBuild.applicationId }, include: { destinationDocker: true, gitSource: { include: { githubApp: true, gitlabApp: true } }, persistentStorage: true, secrets: true, settings: true, teams: true } }) - let { id: buildId, type, sourceBranch = null, pullmergeRequestId = null, previewApplicationId = null, forceRebuild } = queueBuild + let { id: buildId, type, sourceBranch = null, pullmergeRequestId = null, previewApplicationId = null, forceRebuild, sourceRepository = null } = queueBuild application = decryptApplication(application) const originalApplicationId = application.id if (pullmergeRequestId) { @@ -54,7 +54,6 @@ import * as buildpacks from '../lib/buildPacks'; } const { id: applicationId, - repository, name, destinationDocker, destinationDockerId, @@ -77,6 +76,7 @@ import * as buildpacks from '../lib/buildPacks'; } = application let { branch, + repository, buildPack, port, installCommand, @@ -135,6 +135,7 @@ import * as buildpacks from '../lib/buildPacks'; branch = sourceBranch; domain = `${pullmergeRequestId}.${domain}`; imageId = `${applicationId}-${pullmergeRequestId}`; + repository = sourceRepository || repository; } let deployNeeded = true; diff --git a/apps/api/src/routes/webhooks/github/handlers.ts b/apps/api/src/routes/webhooks/github/handlers.ts index 8e8974553..9c52a01af 100644 --- a/apps/api/src/routes/webhooks/github/handlers.ts +++ b/apps/api/src/routes/webhooks/github/handlers.ts @@ -148,7 +148,7 @@ export async function gitHubEvents(request: FastifyRequest): Promi const pullmergeRequestId = body.number.toString(); const pullmergeRequestAction = body.action; const sourceBranch = body.pull_request.head.ref.includes('/') ? body.pull_request.head.ref.split('/')[2] : body.pull_request.head.ref; - console.log({sourceBranch, sourceRepository: body.pull_request.head.repo.full_name}) + const sourceRepository = body.pull_request.head.repo.full_name if (!allowedActions.includes(pullmergeRequestAction)) { throw { status: 500, message: 'Action not allowed.' } } @@ -186,6 +186,7 @@ export async function gitHubEvents(request: FastifyRequest): Promi data: { pullmergeRequestId, sourceBranch, + sourceRepository, customDomain: `${protocol}${pullmergeRequestId}.${getDomain(application.fqdn)}`, application: { connect: { id: application.id } } } @@ -206,6 +207,7 @@ export async function gitHubEvents(request: FastifyRequest): Promi await prisma.build.create({ data: { id: buildId, + sourceRepository, pullmergeRequestId, previewApplicationId, sourceBranch,