All files / infrastructure/scripts/src/workflow sync.ts

0% Statements 0/35
0% Branches 0/24
0% Functions 0/4
0% Lines 0/29

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 88 89 90 91 92 93                                                                                                                                                                                         
import { out } from "../shared/adapters/io.ts";
import type { Args } from "../shared/cli.ts";
import { ExpectedError } from "../shared/errors.ts";
import { areaIssuesWithoutMilestone, milestoneIssues } from "./adapters/gh.ts";
import { commitLog } from "./adapters/git.ts";
import {
    projectIds,
    readProject,
    readSnapshot,
    snapshotPath,
    writeSnapshot,
} from "./adapters/files.ts";
import {
    activityByIssue,
    describeSnapshot,
    sameIssues,
    settleCloses,
    syncTargetOf,
    toSnapshot,
} from "./domain/snapshot.ts";
import { issueFromBranch } from "../issue/domain/branch.ts";
import { currentBranch } from "../shared/adapters/usage.ts";
 
/** False when the milestone isn't there yet, so one missing milestone doesn't stop the others. */
const syncOne = (
    id: string,
    activity: ReturnType<typeof activityByIssue>,
    closing: number | null,
): boolean => {
    const read = syncTargetOf(readProject(id));
 
    if ("problem" in read) throw new ExpectedError(`${id}.yaml can't be synced: ${read.problem}.`);
 
    let issues: ReturnType<typeof milestoneIssues>;
    try {
        issues = milestoneIssues(read.milestone);
    } catch (error) {
        if (!(error instanceof ExpectedError)) throw error;
        out.warn(`${id}: ${error.message}`);
        return false;
    }
 
    const previous = readSnapshot(id);
    const snapshot = settleCloses(
        toSnapshot(
            read.milestone,
            issues,
            read.area ? areaIssuesWithoutMilestone(read.area) : [],
            activity,
            new Date().toISOString(),
        ),
        previous,
        closing,
    );
 
    if (sameIssues(previous, snapshot)) {
        out.success(`${id}: unchanged — ${describeSnapshot(snapshot)}.`);
        return true;
    }
 
    writeSnapshot(id, snapshot);
    out.success(`${id}: wrote ${snapshotPath(id)} — ${describeSnapshot(snapshot)}.`);
    return true;
};
 
/**
 * Snapshots each project's milestone and commit activity beside its project file. `--close`
 * records the current branch's ticket as closed, for the PR that closes it.
 */
export const sync = ({ positionals, flags }: Args): void => {
    const known = projectIds();
    const ids = positionals.length > 0 ? positionals : known;
 
    if (ids.length === 0) throw new ExpectedError("No project under data/workflows/projects/ yet.");
 
    const unknown = ids.filter((id) => !known.includes(id));
    if (unknown.length > 0) {
        throw new ExpectedError(
            `No project file for ${unknown.join(", ")}. Known: ${known.join(", ")}.`,
        );
    }
 
    const closing = flags.has("close") ? (issueFromBranch(currentBranch())?.number ?? null) : null;
    if (flags.has("close") && closing === null) {
        throw new ExpectedError("--close needs a ticket branch such as feature/<area>/<n>-<slug>.");
    }
 
    const activity = activityByIssue(commitLog());
    const failed = ids.filter((id) => !syncOne(id, activity, closing));
 
    if (failed.length > 0) throw new ExpectedError(`Not synced: ${failed.join(", ")}.`);
};