All files / infrastructure/scripts/src/ci-failures main.ts

100% Statements 29/29
83.33% Branches 10/12
100% Functions 4/4
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                                    1x 2x 2x   1x   1x             1x 5x 5x   3x 3x   2x 2x 2x 2x   5x   2x         1x 6x 6x   6x 6x 6x 6x   6x 4x    
import { out } from "../shared/adapters/io.ts";
import { fail, type Args } from "../shared/cli.ts";
import { excerpt } from "../shared/domain/logs.ts";
import { saveLog } from "./adapters/files.ts";
import { failedLog, prChecks, prTitle } from "./adapters/gh.ts";
import { openIssues } from "./adapters/sonar.ts";
import {
    externalCheck,
    group,
    headline,
    jobOf,
    sonarIssueLines,
    sonarPullRequestOf,
    stripLogPrefix,
    type Check,
    type SonarPullRequest,
} from "./domain/checks.ts";
 
const sonarReport = async (check: Check, sonar: SonarPullRequest): Promise<string[]> => {
    try {
        const issues = await openIssues(sonar);
 
        return [...externalCheck(check), ...sonarIssueLines(sonar, issues)];
    } catch (error) {
        return [
            ...externalCheck(check),
            `  Sonar's issues didn't load: ${(error as Error).message}`,
        ];
    }
};
 
const report = async (check: Check): Promise<string[]> => {
    const sonar = sonarPullRequestOf(check);
    if (sonar) return sonarReport(check, sonar);
 
    const job = jobOf(check);
    if (!job) return externalCheck(check);
 
    const log = stripLogPrefix(failedLog(job.run, job.job));
    const path = saveLog(job.job, log);
    const { lines, omitted } = excerpt(log);
    const name = check.workflow ? `${check.workflow} / ${check.name}` : check.name;
 
    return [
        `✖ ${name}`,
        ...lines.map((line) => `  ${line}`),
        omitted > 0 ? `  … ${omitted} more lines in ${path}` : `  full log: ${path}`,
    ];
};
 
export const main = async ({ positionals }: Args): Promise<void> => {
    const [pr] = positionals;
    const grouped = group(prChecks(pr));
 
    out.info(headline(prTitle(pr), grouped));
    for (const check of grouped.pending) out.info(`… ${check.name} (${check.state.toLowerCase()})`);
    const reports = await Promise.all(grouped.failed.map(report));
    for (const lines of reports) out.info(lines.join("\n"));
 
    if (grouped.failed.length > 0)
        fail(`${grouped.failed.length} failing check${grouped.failed.length === 1 ? "" : "s"}.`);
};