All files / infrastructure/scripts/src/ship/adapters gh.ts

97.14% Statements 34/35
100% Branches 12/12
88.88% Functions 8/9
100% Lines 32/32

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          1x 2x 2x   1x         1x   1x 1x                   1x 1x     1x 1x     1x 2x     1x 2x 2x 4x           1x       3x 3x         2x   2x 1x 1x   1x       1x 3x     1x       3x 2x 1x 1x    
import { assertNotFlagLike } from "../../shared/adapters/exec.ts";
import { gh } from "../../shared/adapters/gh.ts";
import { isMissingChecksError } from "../domain/checks.ts";
 
/** A missing PR is the normal first-push case, so return `undefined` for `main.ts` to create one. */
export const prUrlForBranch = (branch: string): string | undefined => {
    try {
        return gh("pr", "view", assertNotFlagLike(branch, "branch"), "--json", "url", "-q", ".url");
    } catch {
        return undefined;
    }
};
 
/** `--fill` takes the title and body from the branch's one commit, or its commits' summary. */
export const createPr = (): string => gh("pr", "create", "--fill");
 
export const enableAutoMerge = (url: string, method: string): void => {
    gh("pr", "merge", "--auto", `--${method}`, assertNotFlagLike(url, "PR url"));
};
 
interface ChecksResult {
    passed: boolean;
    /** `gh`'s per-check table, shown when a check fails. */
    output: string;
}
 
// ~7 checks across 3 services, each registering on its own schedule after a push
const CHECK_REGISTRATION_BUDGET_MS = 60_000;
const CHECK_REGISTRATION_INTERVAL_MS = 5_000;
 
/** Auto-merge only queues at this point — GitHub takes a further beat to actually execute the merge. */
const MERGE_POLL_BUDGET_MS = 60_000;
const MERGE_POLL_INTERVAL_MS = 3_000;
 
/** Keep retry loops synchronous so the rest of this script stays synchronous. */
const wait = (ms: number): void => {
    Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, ms);
};
 
const errorText = (error: unknown): string => {
    const { stdout, stderr } = error as { stdout?: Buffer | string; stderr?: Buffer | string };
    return [stdout, stderr]
        .map((part) => part?.toString().trim() ?? "")
        .filter(Boolean)
        .join("\n");
};
 
/** Failed `gh` checks put their result table on the caught error's stdout. */
export const watchChecks = (
    url: string,
    deadline = Date.now() + CHECK_REGISTRATION_BUDGET_MS,
): ChecksResult => {
    try {
        return {
            passed: true,
            output: gh("pr", "checks", assertNotFlagLike(url, "PR url"), "--watch"),
        };
    } catch (error) {
        const output = errorText(error);
        // "no checks reported" also means registration is pending, so retry before treating it as failure.
        if (isMissingChecksError(output) && Date.now() < deadline) {
            wait(CHECK_REGISTRATION_INTERVAL_MS);
            return watchChecks(url, deadline);
        }
        return { passed: false, output };
    }
};
 
const prState = (url: string): string =>
    gh("pr", "view", assertNotFlagLike(url, "PR url"), "--json", "state", "-q", ".state");
 
/** Polling lets the same run return to master after the merge completes. */
export const waitForMerge = (
    url: string,
    deadline = Date.now() + MERGE_POLL_BUDGET_MS,
): boolean => {
    if (prState(url) === "MERGED") return true;
    if (Date.now() >= deadline) return false;
    wait(MERGE_POLL_INTERVAL_MS);
    return waitForMerge(url, deadline);
};