All files / infrastructure/scripts/src/ship main.ts

100% Statements 30/30
100% Branches 14/14
100% Functions 2/2
100% Lines 26/26

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                          1x   1x 3x 3x   1x 1x     1x 4x 4x   3x   3x 3x 3x   3x   3x 3x 3x 3x 4x   3x 4x   4x 1x 1x     3x   3x    
import process from "node:process";
import { out } from "../shared/adapters/io.ts";
import { ExpectedError } from "../shared/errors.ts";
import {
    createPr,
    enableAutoMerge,
    prUrlForBranch,
    waitForMerge,
    watchChecks,
} from "./adapters/gh.ts";
import { checkoutMaster, currentBranch, pullMaster, push } from "./adapters/git.ts";
import { shipReport } from "./domain/report.ts";
 
const MERGE_METHOD = "merge";
 
const prUrl = (branch: string): string => {
    const existing = prUrlForBranch(branch);
    if (existing) return existing;
 
    out.info("No PR for this branch yet — creating one.");
    return createPr();
};
 
export const main = (): void => {
    const branch = currentBranch();
    if (branch === "master") throw new ExpectedError("On master — nothing to ship from here.");
 
    out.begin(`🚀 Shipping ${branch}`);
 
    push(branch);
    const url = prUrl(branch);
    out.note(url, "PR");
 
    enableAutoMerge(url, MERGE_METHOD);
 
    const loading = out.spinner();
    loading.start("Watching checks — this blocks until they conclude...");
    const { passed, output } = watchChecks(url);
    loading.stop(passed ? "Checks passed." : "A check failed.");
    if (!passed) out.note(output, "Checks");
 
    const merged = passed && waitForMerge(url);
    const report = shipReport({ checksPassed: passed, merged });
 
    if (report.syncMaster) {
        checkoutMaster();
        pullMaster();
    }
 
    out.end(report.message);
 
    if (report.exitCode !== undefined) process.exitCode = report.exitCode;
};