From ac49235916937252c1e22b4e97e9c3430c9ad337 Mon Sep 17 00:00:00 2001 From: Vyacheslav Shcherbinin Date: Sat, 25 Jan 2025 10:51:22 +0700 Subject: [PATCH] Pull Request actions filter --- apps/dokploy/pages/api/deploy/github.ts | 141 ++++++++++++------------ 1 file changed, 72 insertions(+), 69 deletions(-) diff --git a/apps/dokploy/pages/api/deploy/github.ts b/apps/dokploy/pages/api/deploy/github.ts index ff7a9221..0cb7cf36 100644 --- a/apps/dokploy/pages/api/deploy/github.ts +++ b/apps/dokploy/pages/api/deploy/github.ts @@ -201,79 +201,82 @@ export default async function handler( res.status(200).json({ message: "Preview Deployment Closed" }); return; } + // opened or synchronize or reopened - const repository = githubBody?.repository?.name; - const deploymentHash = githubBody?.pull_request?.head?.sha; - const branch = githubBody?.pull_request?.base?.ref; - const owner = githubBody?.repository?.owner?.login; + if (githubBody?.action === "opened" || githubBody?.action === "synchronize" || githubBody?.action === "reopened") { + const repository = githubBody?.repository?.name; + const deploymentHash = githubBody?.pull_request?.head?.sha; + const branch = githubBody?.pull_request?.base?.ref; + const owner = githubBody?.repository?.owner?.login; - const apps = await db.query.applications.findMany({ - where: and( - eq(applications.sourceType, "github"), - eq(applications.repository, repository), - eq(applications.branch, branch), - eq(applications.isPreviewDeploymentsActive, true), - eq(applications.owner, owner), - ), - with: { - previewDeployments: true, - }, - }); - - const prBranch = githubBody?.pull_request?.head?.ref; - - const prNumber = githubBody?.pull_request?.number; - const prTitle = githubBody?.pull_request?.title; - const prURL = githubBody?.pull_request?.html_url; - - for (const app of apps) { - const previewLimit = app?.previewLimit || 0; - if (app?.previewDeployments?.length > previewLimit) { - continue; - } - const previewDeploymentResult = - await findPreviewDeploymentByApplicationId(app.applicationId, prId); - - let previewDeploymentId = - previewDeploymentResult?.previewDeploymentId || ""; - - if (!previewDeploymentResult) { - const previewDeployment = await createPreviewDeployment({ - applicationId: app.applicationId as string, - branch: prBranch, - pullRequestId: prId, - pullRequestNumber: prNumber, - pullRequestTitle: prTitle, - pullRequestURL: prURL, - }); - previewDeploymentId = previewDeployment.previewDeploymentId; - } - - const jobData: DeploymentJob = { - applicationId: app.applicationId as string, - titleLog: "Preview Deployment", - descriptionLog: `Hash: ${deploymentHash}`, - type: "deploy", - applicationType: "application-preview", - server: !!app.serverId, - previewDeploymentId, - }; - - if (IS_CLOUD && app.serverId) { - jobData.serverId = app.serverId; - await deploy(jobData); - return true; - } - await myQueue.add( - "deployments", - { ...jobData }, - { - removeOnComplete: true, - removeOnFail: true, + const apps = await db.query.applications.findMany({ + where: and( + eq(applications.sourceType, "github"), + eq(applications.repository, repository), + eq(applications.branch, branch), + eq(applications.isPreviewDeploymentsActive, true), + eq(applications.owner, owner), + ), + with: { + previewDeployments: true, }, - ); + }); + + const prBranch = githubBody?.pull_request?.head?.ref; + + const prNumber = githubBody?.pull_request?.number; + const prTitle = githubBody?.pull_request?.title; + const prURL = githubBody?.pull_request?.html_url; + + for (const app of apps) { + const previewLimit = app?.previewLimit || 0; + if (app?.previewDeployments?.length > previewLimit) { + continue; + } + const previewDeploymentResult = + await findPreviewDeploymentByApplicationId(app.applicationId, prId); + + let previewDeploymentId = + previewDeploymentResult?.previewDeploymentId || ""; + + if (!previewDeploymentResult) { + const previewDeployment = await createPreviewDeployment({ + applicationId: app.applicationId as string, + branch: prBranch, + pullRequestId: prId, + pullRequestNumber: prNumber, + pullRequestTitle: prTitle, + pullRequestURL: prURL, + }); + previewDeploymentId = previewDeployment.previewDeploymentId; + } + + const jobData: DeploymentJob = { + applicationId: app.applicationId as string, + titleLog: "Preview Deployment", + descriptionLog: `Hash: ${deploymentHash}`, + type: "deploy", + applicationType: "application-preview", + server: !!app.serverId, + previewDeploymentId, + }; + + if (IS_CLOUD && app.serverId) { + jobData.serverId = app.serverId; + await deploy(jobData); + return true; + } + await myQueue.add( + "deployments", + { ...jobData }, + { + removeOnComplete: true, + removeOnFail: true, + }, + ); + } + return res.status(200).json({ message: "Apps Deployed" }); } - return res.status(200).json({ message: "Apps Deployed" }); } return res.status(400).json({ message: "No Actions matched" });