All files / infrastructure/scripts/src/issue/domain branch.ts

100% Statements 24/24
100% Branches 10/10
100% Functions 8/8
100% Lines 21/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52  4x       4x 49x                 4x   4x     4x 20x 46x 29x   3x     4x 16x             4x 8x 8x     4x   4x 9x     4x 20x 12x    
/** The four prefixes `.husky/pre-push` accepts; anything else is unpushable. */
export const BRANCH_TYPES = ["feature", "fix", "hotfix", "release"] as const;
 
type BranchType = (typeof BRANCH_TYPES)[number];
 
export const slugify = (title: string): string =>
    title
        .normalize("NFD")
        .replace(/[\u0300-\u036f]/g, "")
        .toLowerCase()
        .replace(/[^a-z0-9]+/g, "-")
        .replace(/^-/, "")
        .replace(/-$/, "");
 
/** What an issue with no `app:`, `package:` or `infra:` label is about: the repo as a whole. */
const REPO_AREA = "repo";
 
const AREA_PREFIXES = ["app", "package", "infra"] as const;
 
/** The folder the first area label names, so `app:huella-legal` becomes `huella-legal`. */
export const areaFor = (labels: string[]): string => {
    for (const prefix of AREA_PREFIXES) {
        const label = labels.find((name) => name.startsWith(`${prefix}:`));
        if (label) return label.slice(prefix.length + 1);
    }
    return REPO_AREA;
};
 
export const branchName = (type: BranchType, area: string, issue: number, title: string): string =>
    `${type}/${slugify(area)}/${issue}-${slugify(title)}`;
 
interface BranchIssue {
    area: string;
    number: number;
}
 
export const issueFromBranch = (branch: string): BranchIssue | undefined => {
    const match = /^[^/]+\/([^/]+)\/(\d+)-/.exec(branch);
    return match?.[1] && match[2] ? { area: match[1], number: Number(match[2]) } : undefined;
};
 
const LABEL_TYPES: Record<string, BranchType> = { enhancement: "feature", bug: "fix" };
 
export const isBranchType = (value: string): value is BranchType =>
    (BRANCH_TYPES as readonly string[]).includes(value);
 
/** Undefined for a `spike` or an unmapped label: which branch that becomes is a judgement call. */
export const branchTypeFor = (labels: string[]): BranchType | undefined => {
    const types = new Set(labels.flatMap((label) => LABEL_TYPES[label] ?? []));
    return types.size === 1 ? [...types][0] : undefined;
};