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
48 changes: 48 additions & 0 deletions apps/dokploy/__test__/deploy/normalize-changed-files.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { describe, expect, it } from "vitest";
import { normalizeChangedFilesFromCommits } from "../../../../packages/server/src/utils/watch-paths/normalize-changed-files";

describe("normalizeChangedFilesFromCommits", () => {
it("merges added, modified, and removed files from commit payloads", () => {
expect(
normalizeChangedFilesFromCommits([
{
added: ["docker-compose.dokploy.yml"],
modified: ["steam/src/app.ts"],
removed: ["old/file.ts"],
},
]),
).toEqual([
"docker-compose.dokploy.yml",
"steam/src/app.ts",
"old/file.ts",
]);
});

it("ignores missing and non-string file entries instead of returning nullish values", () => {
expect(
normalizeChangedFilesFromCommits([
{
added: undefined,
modified: [undefined, "steam/src/app.ts", ""],
removed: [
null,
"shared/migrations/0007_csfloat_buy_attempt_flows.ts",
],
},
undefined,
{
added: ["steamCSFloat/src/csfloatScanning.ts"],
},
] as any),
).toEqual([
"steam/src/app.ts",
"shared/migrations/0007_csfloat_buy_attempt_flows.ts",
"steamCSFloat/src/csfloatScanning.ts",
]);
});

it("returns an empty list when commits are missing", () => {
expect(normalizeChangedFilesFromCommits(undefined as any)).toEqual([]);
expect(normalizeChangedFilesFromCommits([])).toEqual([]);
});
});
20 changes: 20 additions & 0 deletions apps/dokploy/__test__/deploy/watch-paths.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { describe, expect, it } from "vitest";
import { shouldDeploy } from "../../../../packages/server/src/utils/watch-paths/should-deploy";

describe("shouldDeploy", () => {
it("returns false instead of throwing when modified files are missing", () => {
expect(() => shouldDeploy(["steam/**"], undefined as any)).not.toThrow();
expect(shouldDeploy(["steam/**"], undefined as any)).toBe(false);
});

it("returns false instead of throwing when modified files contain nullish entries only", () => {
expect(() => shouldDeploy(["steam/**"], [undefined] as any)).not.toThrow();
expect(shouldDeploy(["steam/**"], [undefined] as any)).toBe(false);
});

it("still matches valid modified paths when nullish entries are mixed in", () => {
expect(
shouldDeploy(["steam/**"], [undefined, "steam/src/app.ts"] as any),
).toBe(true);
});
});
25 changes: 9 additions & 16 deletions apps/dokploy/pages/api/deploy/[refreshToken].ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import {
type Bitbucket,
getBitbucketHeaders,
IS_CLOUD,
normalizeChangedFilesFromCommits,
shouldDeploy,
} from "@dokploy/server";
import { db } from "@dokploy/server/db";
Expand Down Expand Up @@ -110,8 +111,8 @@ export default async function handler(
}
// If webhook doesn't provide image info, we'll use the configured image (old behavior)
} else if (sourceType === "github") {
const normalizedCommits = req.body?.commits?.flatMap(
(commit: any) => commit.modified,
const normalizedCommits = normalizeChangedFilesFromCommits(
req.body?.commits,
);

const shouldDeployPaths = shouldDeploy(
Expand Down Expand Up @@ -141,21 +142,13 @@ export default async function handler(
let normalizedCommits: string[] = [];

if (provider === "github") {
normalizedCommits = req.body?.commits?.flatMap(
(commit: any) => commit.modified,
);
normalizedCommits = normalizeChangedFilesFromCommits(req.body?.commits);
} else if (provider === "gitlab") {
normalizedCommits = req.body?.commits?.flatMap(
(commit: any) => commit.modified,
);
normalizedCommits = normalizeChangedFilesFromCommits(req.body?.commits);
} else if (provider === "gitea") {
normalizedCommits = req.body?.commits?.flatMap(
(commit: any) => commit.modified,
);
normalizedCommits = normalizeChangedFilesFromCommits(req.body?.commits);
} else if (provider === "soft-serve") {
normalizedCommits = req.body?.commits?.flatMap(
(commit: any) => commit.modified,
);
normalizedCommits = normalizeChangedFilesFromCommits(req.body?.commits);
}

const shouldDeployPaths = shouldDeploy(
Expand All @@ -170,8 +163,8 @@ export default async function handler(
} else if (sourceType === "gitlab") {
const branchName = extractBranchName(req.headers, req.body);

const normalizedCommits = req.body?.commits?.flatMap(
(commit: any) => commit.modified,
const normalizedCommits = normalizeChangedFilesFromCommits(
req.body?.commits,
);

const shouldDeployPaths = shouldDeploy(
Expand Down
30 changes: 14 additions & 16 deletions apps/dokploy/pages/api/deploy/compose/[refreshToken].ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import { IS_CLOUD, shouldDeploy } from "@dokploy/server";
import {
IS_CLOUD,
normalizeChangedFilesFromCommits,
shouldDeploy,
} from "@dokploy/server";
import { db } from "@dokploy/server/db";
import { eq } from "drizzle-orm";
import type { NextApiRequest, NextApiResponse } from "next";
Expand Down Expand Up @@ -53,8 +57,8 @@ export default async function handler(

if (sourceType === "github") {
const branchName = extractBranchName(req.headers, req.body);
const normalizedCommits = req.body?.commits?.flatMap(
(commit: any) => commit.modified,
const normalizedCommits = normalizeChangedFilesFromCommits(
req.body?.commits,
);

const shouldDeployPaths = shouldDeploy(
Expand All @@ -73,8 +77,8 @@ export default async function handler(
}
} else if (sourceType === "gitlab") {
const branchName = extractBranchName(req.headers, req.body);
const normalizedCommits = req.body?.commits?.flatMap(
(commit: any) => commit.modified,
const normalizedCommits = normalizeChangedFilesFromCommits(
req.body?.commits,
);

const shouldDeployPaths = shouldDeploy(
Expand Down Expand Up @@ -124,17 +128,11 @@ export default async function handler(
let normalizedCommits: string[] = [];

if (provider === "github") {
normalizedCommits = req.body?.commits?.flatMap(
(commit: any) => commit.modified,
);
normalizedCommits = normalizeChangedFilesFromCommits(req.body?.commits);
} else if (provider === "gitlab") {
normalizedCommits = req.body?.commits?.flatMap(
(commit: any) => commit.modified,
);
normalizedCommits = normalizeChangedFilesFromCommits(req.body?.commits);
} else if (provider === "gitea") {
normalizedCommits = req.body?.commits?.flatMap(
(commit: any) => commit.modified,
);
normalizedCommits = normalizeChangedFilesFromCommits(req.body?.commits);
}

const shouldDeployPaths = shouldDeploy(
Expand All @@ -149,8 +147,8 @@ export default async function handler(
} else if (sourceType === "gitea") {
const branchName = extractBranchName(req.headers, req.body);

const normalizedCommits = req.body?.commits?.flatMap(
(commit: any) => commit.modified,
const normalizedCommits = normalizeChangedFilesFromCommits(
req.body?.commits,
);

const shouldDeployPaths = shouldDeploy(
Expand Down
5 changes: 3 additions & 2 deletions apps/dokploy/pages/api/deploy/github.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
findPreviewDeploymentByApplicationId,
findPreviewDeploymentsByPullRequestId,
IS_CLOUD,
normalizeChangedFilesFromCommits,
removePreviewDeployment,
shouldDeploy,
} from "@dokploy/server";
Expand Down Expand Up @@ -213,8 +214,8 @@ export default async function handler(
const deploymentTitle = extractCommitMessage(req.headers, req.body);
const deploymentHash = extractHash(req.headers, req.body);
const owner = githubBody?.repository?.owner?.name;
const normalizedCommits = githubBody?.commits?.flatMap(
(commit: any) => commit.modified,
const normalizedCommits = normalizeChangedFilesFromCommits(
githubBody?.commits,
);

const apps = await db.query.applications.findMany({
Expand Down
1 change: 1 addition & 0 deletions packages/server/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,4 +132,5 @@ export * from "./utils/traefik/types";
export * from "./utils/traefik/web-server";
export * from "./utils/volume-backups/index";
export * from "./utils/watch-paths/should-deploy";
export * from "./utils/watch-paths/normalize-changed-files";
export * from "./wss/utils";
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
interface CommitLike {
added?: Array<string | null | undefined> | null;
modified?: Array<string | null | undefined> | null;
removed?: Array<string | null | undefined> | null;
}

export const normalizeChangedFilesFromCommits = (
commits: Array<CommitLike | null | undefined> | null | undefined,
): string[] => {
return (commits || [])
.flatMap((commit) => {
if (!commit) {
return [];
}

return [commit.added, commit.modified, commit.removed].flatMap(
(paths) => paths || [],
);
})
.filter(
(path): path is string => typeof path === "string" && path.length > 0,
);
};
12 changes: 10 additions & 2 deletions packages/server/src/utils/watch-paths/should-deploy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@ import micromatch from "micromatch";

export const shouldDeploy = (
watchPaths: string[] | null,
modifiedFiles: string[],
modifiedFiles: Array<string | null | undefined> | null | undefined,
): boolean => {
if (!watchPaths || watchPaths?.length === 0) return true;
return micromatch.some(modifiedFiles, watchPaths);
const normalizedModifiedFiles = (modifiedFiles || []).filter(
(path): path is string => typeof path === "string" && path.length > 0,
);

if (normalizedModifiedFiles.length === 0) {
return false;
}

return micromatch.some(normalizedModifiedFiles, watchPaths);
};