Skip to content

Commit 0d5905e

Browse files
committed
Move the sanitize fn too
1 parent 24917fe commit 0d5905e

File tree

3 files changed

+17
-18
lines changed

3 files changed

+17
-18
lines changed

apps/webapp/app/services/upsertBranch.server.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import { type PrismaClient, type PrismaClientOrTransaction } from "@trigger.dev/
22
import slug from "slug";
33
import { prisma } from "~/db.server";
44
import { createApiKeyForEnv, createPkApiKeyForEnv } from "~/models/api-key.server";
5+
import { type CreateBranchOptions } from "~/routes/_app.orgs.$organizationSlug.projects.$projectParam.env.$envParam.branches/route";
6+
import { isValidGitBranchName, sanitizeBranchName } from "~/v3/gitBranch";
57
import { logger } from "./logger.server";
68
import { getLimit } from "./platform.v3.server";
7-
import { type CreateBranchOptions } from "~/routes/_app.orgs.$organizationSlug.projects.$projectParam.env.$envParam.branches/route";
8-
import { isValidGitBranchName } from "~/v3/validGitBranch";
99

1010
export class UpsertBranchService {
1111
#prismaClient: PrismaClient;
@@ -167,17 +167,3 @@ export async function checkBranchLimit(
167167
isAtLimit: used >= limit,
168168
};
169169
}
170-
171-
export function sanitizeBranchName(ref: string): string | null {
172-
if (!ref) return null;
173-
if (ref.startsWith("refs/heads/")) return ref.substring("refs/heads/".length);
174-
if (ref.startsWith("refs/remotes/")) return ref.substring("refs/remotes/".length);
175-
if (ref.startsWith("refs/tags/")) return ref.substring("refs/tags/".length);
176-
if (ref.startsWith("refs/pull/")) return ref.substring("refs/pull/".length);
177-
if (ref.startsWith("refs/merge/")) return ref.substring("refs/merge/".length);
178-
if (ref.startsWith("refs/release/")) return ref.substring("refs/release/".length);
179-
//unknown ref format, so reject
180-
if (ref.startsWith("refs/")) return null;
181-
182-
return ref;
183-
}

apps/webapp/app/v3/validGitBranch.ts renamed to apps/webapp/app/v3/gitBranch.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,17 @@ export function isValidGitBranchName(branch: string): boolean {
2828

2929
return true;
3030
}
31+
32+
export function sanitizeBranchName(ref: string): string | null {
33+
if (!ref) return null;
34+
if (ref.startsWith("refs/heads/")) return ref.substring("refs/heads/".length);
35+
if (ref.startsWith("refs/remotes/")) return ref.substring("refs/remotes/".length);
36+
if (ref.startsWith("refs/tags/")) return ref.substring("refs/tags/".length);
37+
if (ref.startsWith("refs/pull/")) return ref.substring("refs/pull/".length);
38+
if (ref.startsWith("refs/merge/")) return ref.substring("refs/merge/".length);
39+
if (ref.startsWith("refs/release/")) return ref.substring("refs/release/".length);
40+
//unknown ref format, so reject
41+
if (ref.startsWith("refs/")) return null;
42+
43+
return ref;
44+
}

apps/webapp/test/validateGitBranchName.test.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { describe, expect, it } from "vitest";
2-
import { sanitizeBranchName } from "../app/services/upsertBranch.server";
3-
import { isValidGitBranchName } from "~/v3/validGitBranch";
2+
import { isValidGitBranchName, sanitizeBranchName } from "~/v3/gitBranch";
43

54
describe("isValidGitBranchName", () => {
65
it("returns true for a valid branch name", async () => {

0 commit comments

Comments
 (0)