Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions apps/dokploy/__test__/deploy/github.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {
extractImageTag,
extractImageTagFromRequest,
} from "@/pages/api/deploy/[refreshToken]";
import { shouldDeployPreviewDeployment } from "@/pages/api/deploy/github";

describe("GitHub Webhook Skip CI", () => {
const mockGithubHeaders = {
Expand Down Expand Up @@ -436,3 +437,36 @@ describe("Docker Image Name and Tag Extraction", () => {
});
});
});

describe("Preview Deployment duplicate prevention", () => {
it("deploys on code-changing pull_request events", () => {
for (const action of ["opened", "synchronize", "reopened"]) {
expect(
shouldDeployPreviewDeployment({
action,
createdPreviewDeployment: false,
}),
).toBe(true);
}
});

it("does NOT redeploy an existing preview on label events", () => {
for (const action of ["labeled", "unlabeled"]) {
expect(
shouldDeployPreviewDeployment({
action,
createdPreviewDeployment: false,
}),
).toBe(false);
}
});

it("deploys on a label event only when it just created the preview", () => {
expect(
shouldDeployPreviewDeployment({
action: "labeled",
createdPreviewDeployment: true,
}),
).toBe(true);
});
});
16 changes: 16 additions & 0 deletions apps/dokploy/drizzle/0170_unique_preview_deployments.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
-- De-duplicate existing rows (keep the oldest per PR/app) before enforcing uniqueness.
DELETE FROM "preview_deployments"
WHERE "previewDeploymentId" IN (
SELECT "previewDeploymentId"
FROM (
SELECT
"previewDeploymentId",
ROW_NUMBER() OVER (
PARTITION BY "applicationId", "pullRequestId"
ORDER BY "createdAt" ASC, "previewDeploymentId" ASC
) AS rn
FROM "preview_deployments"
) ranked
WHERE ranked.rn > 1
);--> statement-breakpoint
ALTER TABLE "preview_deployments" ADD CONSTRAINT "preview_deployments_applicationId_pullRequestId_unique" UNIQUE("applicationId","pullRequestId");
Loading