All files / infrastructure/scripts/src/invariants/domain workflows.ts

100% Statements 12/12
90% Branches 9/10
100% Functions 3/3
100% Lines 11/11

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                  2x   2x 5x   2x 6x 6x       2x 6x 6x 6x   3x                          
import { parse } from "yaml";
import type { Finding } from "./findings.ts";
 
export interface WorkflowSource {
    /** The basename, e.g. `deploy-portfolio.yml`. */
    file: string;
    text: string;
}
 
const SEPARATOR = " / ";
 
const fileFor = (name: string): string =>
    `${name.split(SEPARATOR).join("-").toLowerCase().replaceAll(/\s+/g, "-")}.yml`;
 
const nameOf = (text: string): string | undefined => {
    const { name } = (parse(text) ?? {}) as { name?: unknown };
    return typeof name === "string" ? name : undefined;
};
 
/** A workflow whose file isn't its `name` slugged, so `Deploy / Portfolio` sits in `deploy-portfolio.yml`. */
export const misnamedWorkflow = ({ file, text }: WorkflowSource): Finding[] => {
    const name = nameOf(text);
    const expected = name === undefined ? undefined : fileFor(name);
    if (expected === file) return [];
 
    return [
        {
            id: "misnamed-workflow",
            title: "A workflow's file and name disagree",
            detail:
                name === undefined
                    ? `.github/workflows/${file} has no top-level name. Name it "<Category>${SEPARATOR}<Subject>", e.g. "Deploy${SEPARATOR}Portfolio", or a single word like "CI".`
                    : `.github/workflows/${file} is named "${name}", so the file is ${expected}. Rename one so they match; the category before "${SEPARATOR}" is what groups the Actions sidebar.`,
            evidence: [`.github/workflows/${file}`],
            blocks: true,
        },
    ];
};