All files / infrastructure/scripts/src/ci-failures/domain checks.ts

100% Statements 28/28
91.3% Branches 21/23
100% Functions 13/13
100% Lines 25/25

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 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87                                          7x 13x 13x 13x     2x 5x 5x       2x 3x   5x 5x     2x 7x   4x                                         2x 8x   7x 7x 7x   7x           2x 2x 2x     2x   2x    
export interface Check {
    name: string;
    bucket: string;
    state: string;
    link: string;
    workflow: string;
    description: string;
}
 
interface Job {
    run: string;
    job: string;
}
 
interface Grouped {
    failed: Check[];
    pending: Check[];
    passed: number;
}
 
/** `gh`'s buckets: pass, fail, pending, skipping and cancel. */
export const group = (checks: Check[]): Grouped => ({
    failed: checks.filter(({ bucket }) => bucket === "fail" || bucket === "cancel"),
    pending: checks.filter(({ bucket }) => bucket === "pending"),
    passed: checks.filter(({ bucket }) => bucket === "pass" || bucket === "skipping").length,
});
 
export const jobOf = ({ link }: Check): Job | undefined => {
    const match = /\/actions\/runs\/(\d+)\/job\/(\d+)/.exec(link);
    return match?.[1] && match[2] ? { run: match[1], job: match[2] } : undefined;
};
 
/** `--log-failed` prefixes every line with the job and the step, and most with a timestamp. */
export const stripLogPrefix = (log: string): string =>
    log
        .split("\n")
        .map((line) => line.replace(/^[^\t]*\t[^\t]*\t\ufeff?(?:\d{4}-\d\d-\d\dT[\d:.]+Z ?)?/, ""))
        .filter((line) => !/^##\[(?:group|endgroup)\]/.test(line))
        .join("\n");
 
export const headline = (pr: string, { failed, pending, passed }: Grouped): string =>
    `${pr}: ${failed.length} failed, ${pending.length} pending, ${passed} passed or skipped`;
 
export const externalCheck = ({ name, state, description, link }: Check): string[] => [
    `✖ ${name} (${state.toLowerCase()})`,
    ...(description ? [`  ${description}`] : []),
    `  ${link || "no link"}`,
];
 
export interface SonarPullRequest {
    origin: string;
    project: string;
    pullRequest: string;
}
 
export interface SonarIssue {
    severity: string;
    rule: string;
    component: string;
    line?: number;
    message: string;
}
 
/** The "SonarCloud Code Analysis" status links to `/dashboard?id=<project>&pullRequest=<n>`. */
export const sonarPullRequestOf = ({ link }: Check): SonarPullRequest | undefined => {
    if (!URL.canParse(link)) return undefined;
 
    const url = new URL(link);
    const project = url.searchParams.get("id");
    const pullRequest = url.searchParams.get("pullRequest");
 
    return url.pathname === "/dashboard" && project && pullRequest
        ? { origin: url.origin, project, pullRequest }
        : undefined;
};
 
/** Sonar's component is `<project>:<path>`; the path alone is what an agent opens. */
export const sonarIssueLines = ({ project }: SonarPullRequest, issues: SonarIssue[]): string[] =>
    issues.map(({ severity, rule, component, line, message }) => {
        const path = component.startsWith(`${project}:`)
            ? component.slice(project.length + 1)
            : component;
        const at = line === undefined ? path : `${path}:${line}`;
 
        return `  ${severity} ${rule} ${at} — ${message}`;
    });