Skip to content

Commit 98dbc59

Browse files
committed
fix(github-webhook): apply preview limit only to new previews
The preview deployment limit was checked before looking up whether the pull request already had a preview, so once an application reached the limit every `synchronize` event was dropped and existing previews went stale while GitHub still saw a 200. The check now runs inside the branch that creates a preview, uses the schema default of 3 when no limit is configured (instead of 0, which blocked every app without an explicit value) and compares with `>=` so the configured limit is inclusive. Closes Dokploy#4074
1 parent f62e41e commit 98dbc59

2 files changed

Lines changed: 168 additions & 6 deletions

File tree

‎apps/dokploy/__test__/deploy/github-webhook-handler.test.ts‎

Lines changed: 159 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ const mocks = vi.hoisted(() => ({
1212
queueAdd: vi.fn(),
1313
verify: vi.fn(),
1414
shouldDeploy: vi.fn(),
15+
createPreviewDeployment: vi.fn(),
16+
findPreviewDeploymentByApplicationId: vi.fn(),
1517
}));
1618

1719
vi.mock("drizzle-orm", () => ({
@@ -64,10 +66,11 @@ vi.mock("@dokploy/server", () => ({
6466
IS_CLOUD: false,
6567
shouldDeploy: mocks.shouldDeploy,
6668
checkUserRepositoryPermissions: vi.fn(),
67-
createPreviewDeployment: vi.fn(),
69+
createPreviewDeployment: mocks.createPreviewDeployment,
6870
createSecurityBlockedComment: vi.fn(),
6971
findGithubById: vi.fn(),
70-
findPreviewDeploymentByApplicationId: vi.fn(),
72+
findPreviewDeploymentByApplicationId:
73+
mocks.findPreviewDeploymentByApplicationId,
7174
findPreviewDeploymentsByPullRequestId: vi.fn(),
7275
getBitbucketHeaders: vi.fn(() => ({})),
7376
removePreviewDeployment: vi.fn(),
@@ -321,3 +324,157 @@ describe("GitHub app webhook auto-deploy", () => {
321324
expect(res.json).toHaveBeenCalledWith({ message: "No apps to deploy" });
322325
});
323326
});
327+
328+
describe("GitHub app webhook preview deployments", () => {
329+
const createApplication = (
330+
overrides: Record<string, unknown> = {},
331+
): Record<string, unknown> => ({
332+
applicationId: "application-id",
333+
name: "my-app",
334+
serverId: null,
335+
previewLabels: [],
336+
previewLimit: 3,
337+
previewDeployments: [],
338+
previewRequireCollaboratorPermissions: false,
339+
...overrides,
340+
});
341+
342+
const createPreviewDeployments = (total: number) =>
343+
Array.from({ length: total }, (_, index) => ({
344+
previewDeploymentId: `existing-preview-${index}`,
345+
}));
346+
347+
const createPullRequestRequest = (action: string) =>
348+
({
349+
headers: {
350+
"x-hub-signature-256": "sha256=test-signature",
351+
"x-github-event": "pull_request",
352+
},
353+
body: {
354+
installation: {
355+
id: 12345,
356+
},
357+
action,
358+
pull_request: {
359+
id: 987,
360+
number: 42,
361+
title: "feat: add preview",
362+
html_url: "https://github.com/agentHits/dokploy/pull/42",
363+
labels: [],
364+
user: {
365+
login: "agentHits",
366+
},
367+
head: {
368+
ref: "feature",
369+
sha: "abc123",
370+
},
371+
base: {
372+
ref: "main",
373+
},
374+
},
375+
repository: {
376+
name: "dokploy",
377+
owner: {
378+
login: "agentHits",
379+
},
380+
},
381+
},
382+
}) as unknown as NextApiRequest;
383+
384+
beforeEach(() => {
385+
vi.clearAllMocks();
386+
mocks.githubFindFirst.mockResolvedValue({
387+
githubId: "github-provider-id",
388+
githubInstallationId: 12345,
389+
githubWebhookSecret: "webhook-secret",
390+
});
391+
mocks.verify.mockResolvedValue(true);
392+
mocks.queueAdd.mockResolvedValue({ id: "job-id" });
393+
mocks.createPreviewDeployment.mockResolvedValue({
394+
previewDeploymentId: "new-preview-id",
395+
});
396+
mocks.findPreviewDeploymentByApplicationId.mockResolvedValue(undefined);
397+
});
398+
399+
it("redeploys an existing preview even when the limit is reached", async () => {
400+
mocks.applicationsFindMany.mockResolvedValue([
401+
createApplication({
402+
previewLimit: 2,
403+
previewDeployments: createPreviewDeployments(3),
404+
}),
405+
]);
406+
mocks.findPreviewDeploymentByApplicationId.mockResolvedValue({
407+
previewDeploymentId: "existing-preview-0",
408+
});
409+
const res = createResponse();
410+
411+
await handler(createPullRequestRequest("synchronize"), res);
412+
413+
expect(mocks.createPreviewDeployment).not.toHaveBeenCalled();
414+
expect(mocks.queueAdd).toHaveBeenCalledWith(
415+
"deployments",
416+
expect.objectContaining({
417+
applicationId: "application-id",
418+
applicationType: "application-preview",
419+
previewDeploymentId: "existing-preview-0",
420+
type: "deploy",
421+
}),
422+
expect.objectContaining({
423+
removeOnComplete: true,
424+
removeOnFail: true,
425+
}),
426+
);
427+
expect(res.status).toHaveBeenCalledWith(200);
428+
});
429+
430+
it("does not create a new preview once the limit is reached", async () => {
431+
mocks.applicationsFindMany.mockResolvedValue([
432+
createApplication({
433+
previewLimit: 2,
434+
previewDeployments: createPreviewDeployments(2),
435+
}),
436+
]);
437+
const res = createResponse();
438+
439+
await handler(createPullRequestRequest("opened"), res);
440+
441+
expect(mocks.createPreviewDeployment).not.toHaveBeenCalled();
442+
expect(mocks.queueAdd).not.toHaveBeenCalled();
443+
expect(res.status).toHaveBeenCalledWith(200);
444+
});
445+
446+
it("falls back to the default limit when none is configured", async () => {
447+
mocks.applicationsFindMany.mockResolvedValue([
448+
createApplication({
449+
previewLimit: null,
450+
previewDeployments: createPreviewDeployments(2),
451+
}),
452+
]);
453+
const res = createResponse();
454+
455+
await handler(createPullRequestRequest("opened"), res);
456+
457+
expect(mocks.createPreviewDeployment).toHaveBeenCalledWith(
458+
expect.objectContaining({
459+
applicationId: "application-id",
460+
branch: "feature",
461+
pullRequestId: 987,
462+
pullRequestNumber: 42,
463+
}),
464+
);
465+
expect(mocks.queueAdd).toHaveBeenCalledWith(
466+
"deployments",
467+
expect.objectContaining({
468+
applicationId: "application-id",
469+
applicationType: "application-preview",
470+
previewDeploymentId: "new-preview-id",
471+
type: "deploy",
472+
}),
473+
expect.objectContaining({
474+
removeOnComplete: true,
475+
removeOnFail: true,
476+
}),
477+
);
478+
expect(res.status).toHaveBeenCalledWith(200);
479+
});
480+
});

‎apps/dokploy/pages/api/deploy/github.ts‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,17 +484,22 @@ export default async function handler(
484484
if (!hasLabel) continue;
485485
}
486486

487-
const previewLimit = app?.previewLimit || 0;
488-
if (app?.previewDeployments?.length > previewLimit) {
489-
continue;
490-
}
491487
const previewDeploymentResult =
492488
await findPreviewDeploymentByApplicationId(app.applicationId, prId);
493489

494490
let previewDeploymentId =
495491
previewDeploymentResult?.previewDeploymentId || "";
496492

497493
if (!previewDeploymentResult && shouldCreateDeployment) {
494+
// The limit only applies to new previews, existing ones must
495+
// still be redeployed when the pull request is updated.
496+
const previewLimit = app?.previewLimit ?? 3;
497+
if ((app?.previewDeployments?.length ?? 0) >= previewLimit) {
498+
console.warn(
499+
`⚠️ Preview deployment limit (${previewLimit}) reached for ${app.name}, skipping preview for pull request #${prNumber}`,
500+
);
501+
continue;
502+
}
498503
const previewDeployment = await createPreviewDeployment({
499504
applicationId: app.applicationId as string,
500505
branch: prBranch,

0 commit comments

Comments
 (0)