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

0% Statements 0/23
0% Branches 0/16
0% Functions 0/3
0% Lines 0/19

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                                                                                                                       
import { out } from "../shared/adapters/io.ts";
import { flag, type Args } from "../shared/cli.ts";
import { ExpectedError } from "../shared/errors.ts";
import { closeAsNotPlanned, closeMilestone, milestoneIssues } from "./adapters/gh.ts";
import { commitLog } from "./adapters/git.ts";
import { oneProject, readProject } from "./adapters/files.ts";
import { dropPlan, ticketStatus } from "./domain/queue.ts";
import { activityByIssue, syncTargetOf, toSnapshot } from "./domain/snapshot.ts";
 
/**
 * Closes the milestone tickets nobody started as not planned, and the milestone once none is left;
 * a dry run unless `--yes`.
 */
export const drop = ({ positionals }: Args): void => {
    const id = oneProject(positionals);
    const read = syncTargetOf(readProject(id));
 
    if ("problem" in read) throw new ExpectedError(`${id}.yaml can't be read: ${read.problem}.`);
 
    const { issues } = toSnapshot(
        read.milestone,
        milestoneIssues(read.milestone),
        [],
        activityByIssue(commitLog()),
        "",
    );
    const { close, keep } = dropPlan(issues);
    const write = flag("yes");
 
    for (const issue of close) {
        if (write) closeAsNotPlanned(issue.number);
        out.info(`${write ? "Closed" : "Closes"} #${issue.number} ${issue.title}`);
    }
 
    if (keep.length > 0) {
        out.note(
            keep
                .map((issue) => `#${issue.number} ${issue.title} — ${ticketStatus(issue)}`)
                .join("\n"),
            `Kept (${keep.length})`,
        );
    }
 
    const openKept = keep.some(({ state }) => state === "open");
 
    if (!write) {
        out.note(
            `Nothing was closed. Run it again with --yes to close ${close.length} tickets` +
                (openKept ? "." : ` and the ${read.milestone} milestone.`),
        );
        return;
    }
 
    if (!openKept) closeMilestone(read.milestone);
    out.success(
        `${id}: closed ${close.length} tickets` +
            (openKept ? `; ${read.milestone} stays open for the rest.` : ` and ${read.milestone}.`),
    );
};