fix: limit worktree slug length to 50 characters

Truncates at word boundary (hyphen) when possible for cleaner names.

🤖 Generated with [Claude Code](https://claude.com/claude-code)
This commit is contained in:
Mohamed Boudra
2026-01-09 11:07:19 +07:00
parent 6f278cb6a6
commit 666a0f2fa6
2 changed files with 37 additions and 2 deletions

View File

@@ -1,5 +1,5 @@
import { describe, it, expect, beforeEach, afterEach } from "vitest";
import { createWorktree } from "./worktree";
import { createWorktree, slugify } from "./worktree";
import { execSync } from "child_process";
import { mkdtempSync, rmSync, existsSync, realpathSync, writeFileSync, readFileSync } from "fs";
import { join } from "path";
@@ -154,3 +154,24 @@ describe("createWorktree", () => {
expect(existsSync(expectedWorktreePath)).toBe(false);
});
});
describe("slugify", () => {
it("converts to lowercase kebab-case", () => {
expect(slugify("Hello World")).toBe("hello-world");
expect(slugify("FOO_BAR")).toBe("foo-bar");
});
it("truncates long strings at word boundary", () => {
const longInput = "https-stackoverflow-com-questions-68349031-only-run-actions-on-non-draft-pull-request";
const result = slugify(longInput);
expect(result.length).toBeLessThanOrEqual(50);
expect(result).toBe("https-stackoverflow-com-questions-68349031-only");
});
it("truncates without trailing hyphen when no word boundary", () => {
const longInput = "a".repeat(60);
const result = slugify(longInput);
expect(result.length).toBe(50);
expect(result.endsWith("-")).toBe(false);
});
});

View File

@@ -156,14 +156,28 @@ export function validateBranchSlug(slug: string): {
return { valid: true };
}
const MAX_SLUG_LENGTH = 50;
/**
* Convert string to kebab-case for branch names
*/
export function slugify(input: string): string {
return input
const slug = input
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-")
.replace(/^-+|-+$/g, "");
if (slug.length <= MAX_SLUG_LENGTH) {
return slug;
}
// Truncate at word boundary (hyphen) if possible
const truncated = slug.slice(0, MAX_SLUG_LENGTH);
const lastHyphen = truncated.lastIndexOf("-");
if (lastHyphen > MAX_SLUG_LENGTH / 2) {
return truncated.slice(0, lastHyphen);
}
return truncated.replace(/-+$/, "");
}
function sanitizeWorktreeSlug(input: string): string {