All files / apps/developer-portal/layers/workflows/shared ticket.ts

100% Statements 32/32
92.3% Branches 36/39
100% Functions 12/12
100% Lines 24/24

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        89x           45x         119x 17x   14x   119x       16x 1x                   45x 11x 13x 1x 11x   2x     45x 184x 174x 149x 141x     45x     45x   6x 17x 17x    
import type { Snapshot } from "./types/Snapshot.ts";
import type { SnapshotIssue } from "./types/SnapshotIssue.ts";
import type { TicketStatus } from "./types/TicketStatus.ts";
 
export const day = (timestamp: string): string => timestamp.slice(0, 10);
 
/**
 * An issue as it stood at the end of `asOf`, or null if it wasn't filed yet. A linked branch is
 * only known for the day of the sync, so an earlier day goes by commits alone.
 */
export const issueAsOf = (
    issue: SnapshotIssue,
    asOf: string | null,
    syncedOn: string | null,
): SnapshotIssue | null => {
    if (asOf === null) return issue;
    if (issue.createdAt && day(issue.createdAt) > asOf) return null;
 
    const closed = issue.closedAt !== null && day(issue.closedAt) <= asOf;
 
    return {
        ...issue,
        state: closed ? "closed" : "open",
        reason: closed ? issue.reason : null,
        activity: issue.activity.filter((commit) => commit <= asOf),
        blockedBy: issue.blockedBy.map((blocker) => ({
            ...blocker,
            open: blocker.closedAt ? day(blocker.closedAt) > asOf : blocker.open,
        })),
        branch: issue.branch && syncedOn !== null && asOf >= syncedOn,
        pr: issue.pr && syncedOn !== null && asOf >= syncedOn,
    };
};
 
/** The first day a commit names one of the project's tickets, and the day of its last sync. */
export const replayRange = (snapshot: Snapshot | null): { first: string; last: string } | null => {
    const days = (snapshot?.issues ?? [])
        .flatMap((issue) => issue.activity)
        .sort((a, b) => a.localeCompare(b));
    if (!snapshot?.syncedAt || days.length === 0) return null;
 
    return { first: days[0]!, last: day(snapshot.syncedAt) };
};
 
export const ticketStatus = (issue: SnapshotIssue): TicketStatus => {
    if (issue.state === "closed") return issue.reason === "not_planned" ? "dropped" : "done";
    if (issue.branch || issue.pr || issue.activity.length > 0) return "in-flight";
    if (issue.blockedBy.some(({ open }) => open)) return "blocked";
    return issue.owner ? "waiting-on-you" : "ready";
};
 
const UNTOUCHED: ReadonlySet<TicketStatus> = new Set(["ready", "blocked", "waiting-on-you"]);
 
/** The tickets `pnpm workflow:drop` closes, and the ones it keeps because work started or ended. */
export const dropPlan = (
    issues: SnapshotIssue[],
): { close: SnapshotIssue[]; keep: SnapshotIssue[] } => ({
    close: issues.filter((issue) => UNTOUCHED.has(ticketStatus(issue))),
    keep: issues.filter((issue) => !UNTOUCHED.has(ticketStatus(issue))),
});