mirror of
https://github.com/anthropics/claude-code-action.git
synced 2026-08-22 03:18:54 +08:00
fix: sanitize {{label}} in branch name templates (#1492)
A scoped label like area:permissions was substituted into the branch
name verbatim, producing a ":" that validateBranchName rejects. Because
the branch setup block catches that error and calls process.exit(1), the
whole run died. {{description}} was already sanitized via extractDescription;
{{label}} was the only free-text variable that skipped it.
Add a sanitizeLabel helper (replaces invalid-char runs with a hyphen so
scoped labels stay readable) and apply it before substitution, falling back
to entityType when a label sanitizes to empty. Adds regression tests that
also assert the result passes validateBranchName.
This commit is contained in:
@@ -5,6 +5,7 @@ import {
|
||||
applyBranchTemplate,
|
||||
generateBranchName,
|
||||
} from "../src/utils/branch-template";
|
||||
import { validateBranchName } from "../src/github/operations/branch";
|
||||
|
||||
describe("branch template utilities", () => {
|
||||
describe("applyBranchTemplate", () => {
|
||||
@@ -144,6 +145,53 @@ describe("branch template utilities", () => {
|
||||
expect(result).toBe("dev/enhancement-issue_789");
|
||||
});
|
||||
|
||||
it("should sanitize scoped labels that contain invalid git characters", () => {
|
||||
const template = "{{prefix}}{{label}}/{{entityNumber}}";
|
||||
const result = generateBranchName(
|
||||
template,
|
||||
"claude/",
|
||||
"issue",
|
||||
123,
|
||||
undefined,
|
||||
"area:permissions",
|
||||
);
|
||||
|
||||
expect(result).toBe("claude/area-permissions/123");
|
||||
// Regression: an unsanitized ":" here previously failed validateBranchName
|
||||
// and crashed the run via process.exit(1).
|
||||
expect(() => validateBranchName(result)).not.toThrow();
|
||||
});
|
||||
|
||||
it("should replace spaces in labels with hyphens", () => {
|
||||
const template = "{{prefix}}{{label}}-{{entityNumber}}";
|
||||
const result = generateBranchName(
|
||||
template,
|
||||
"fix/",
|
||||
"issue",
|
||||
456,
|
||||
undefined,
|
||||
"needs review",
|
||||
);
|
||||
|
||||
expect(result).toBe("fix/needs-review-456");
|
||||
expect(() => validateBranchName(result)).not.toThrow();
|
||||
});
|
||||
|
||||
it("should fall back to entityType when a label sanitizes to empty", () => {
|
||||
const template = "{{prefix}}{{label}}-{{entityNumber}}";
|
||||
const result = generateBranchName(
|
||||
template,
|
||||
"fix/",
|
||||
"pr",
|
||||
789,
|
||||
undefined,
|
||||
"🎉",
|
||||
);
|
||||
|
||||
expect(result).toBe("fix/pr-789");
|
||||
expect(() => validateBranchName(result)).not.toThrow();
|
||||
});
|
||||
|
||||
it("should use description in template when provided", () => {
|
||||
const template = "{{prefix}}{{description}}/{{entityNumber}}";
|
||||
const result = generateBranchName(
|
||||
|
||||
Reference in New Issue
Block a user