Skip to content

Commit 24917fe

Browse files
committed
Move isValidGitBranchName to a separate file
1 parent c14e029 commit 24917fe

File tree

3 files changed

+33
-32
lines changed

3 files changed

+33
-32
lines changed

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

Lines changed: 1 addition & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { createApiKeyForEnv, createPkApiKeyForEnv } from "~/models/api-key.serve
55
import { logger } from "./logger.server";
66
import { getLimit } from "./platform.v3.server";
77
import { type CreateBranchOptions } from "~/routes/_app.orgs.$organizationSlug.projects.$projectParam.env.$envParam.branches/route";
8+
import { isValidGitBranchName } from "~/v3/validGitBranch";
89

910
export class UpsertBranchService {
1011
#prismaClient: PrismaClient;
@@ -167,37 +168,6 @@ export async function checkBranchLimit(
167168
};
168169
}
169170

170-
export function isValidGitBranchName(branch: string): boolean {
171-
// Must not be empty
172-
if (!branch) return false;
173-
174-
// Disallowed characters: space, ~, ^, :, ?, *, [, \
175-
if (/[ \~\^:\?\*\[\\]/.test(branch)) return false;
176-
177-
// Disallow ASCII control characters (0-31) and DEL (127)
178-
for (let i = 0; i < branch.length; i++) {
179-
const code = branch.charCodeAt(i);
180-
if ((code >= 0 && code <= 31) || code === 127) return false;
181-
}
182-
183-
// Cannot start or end with a slash
184-
if (branch.startsWith("/") || branch.endsWith("/")) return false;
185-
186-
// Cannot have consecutive slashes
187-
if (branch.includes("//")) return false;
188-
189-
// Cannot contain '..'
190-
if (branch.includes("..")) return false;
191-
192-
// Cannot contain '@{'
193-
if (branch.includes("@{")) return false;
194-
195-
// Cannot end with '.lock'
196-
if (branch.endsWith(".lock")) return false;
197-
198-
return true;
199-
}
200-
201171
export function sanitizeBranchName(ref: string): string | null {
202172
if (!ref) return null;
203173
if (ref.startsWith("refs/heads/")) return ref.substring("refs/heads/".length);

apps/webapp/app/v3/validGitBranch.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
export function isValidGitBranchName(branch: string): boolean {
2+
// Must not be empty
3+
if (!branch) return false;
4+
5+
// Disallowed characters: space, ~, ^, :, ?, *, [, \
6+
if (/[ \~\^:\?\*\[\\]/.test(branch)) return false;
7+
8+
// Disallow ASCII control characters (0-31) and DEL (127)
9+
for (let i = 0; i < branch.length; i++) {
10+
const code = branch.charCodeAt(i);
11+
if ((code >= 0 && code <= 31) || code === 127) return false;
12+
}
13+
14+
// Cannot start or end with a slash
15+
if (branch.startsWith("/") || branch.endsWith("/")) return false;
16+
17+
// Cannot have consecutive slashes
18+
if (branch.includes("//")) return false;
19+
20+
// Cannot contain '..'
21+
if (branch.includes("..")) return false;
22+
23+
// Cannot contain '@{'
24+
if (branch.includes("@{")) return false;
25+
26+
// Cannot end with '.lock'
27+
if (branch.endsWith(".lock")) return false;
28+
29+
return true;
30+
}

apps/webapp/test/validateGitBranchName.test.ts

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

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

0 commit comments

Comments
 (0)