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

85% Statements 17/20
77.41% Branches 24/31
100% Functions 5/5
89.47% Lines 17/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 61 62 63 64 65 66 67 68 69 70 71 72        42x 31x 1x 1x     30x   30x         30x                     1x                                             42x 18x 18x   18x   18x 36x   31x   18x             18x    
import { asText, isRecord, Reader } from "./reader.ts";
import type { Snapshot } from "./types/Snapshot.ts";
import type { SnapshotIssue } from "./types/SnapshotIssue.ts";
 
const readIssue = (reader: Reader, raw: unknown, where: string): SnapshotIssue | null => {
    if (!isRecord(raw) || typeof raw.number !== "number") {
        reader.problems.push(`${where} holds an issue without a number`);
        return null;
    }
 
    const at = `${where}: issue #${raw.number}`;
 
    Iif (raw.state !== "open" && raw.state !== "closed") {
        reader.problems.push(`${at} has no \`state\` of open or closed`);
        return null;
    }
 
    return {
        number: raw.number,
        title: asText(raw.title) ?? "",
        url: asText(raw.url) ?? "",
        state: raw.state,
        reason: raw.reason === "completed" || raw.reason === "not_planned" ? raw.reason : null,
        caps: reader.texts(raw, "caps", at),
        createdAt: asText(raw.createdAt),
        closedAt: asText(raw.closedAt),
        activity: reader.texts(raw, "activity", at),
        blockedBy: reader.list(raw, "blockedBy", at).flatMap((blocker) =>
            isRecord(blocker) && typeof blocker.number === "number"
                ? [
                      {
                          number: blocker.number,
                          open: blocker.open === true,
                          closedAt: asText(blocker.closedAt),
                      },
                  ]
                : [],
        ),
        branch: raw.branch === true,
        pr: raw.pr === true,
        owner: raw.owner === true,
        complexity:
            typeof raw.complexity === "number" &&
            Number.isFinite(raw.complexity) &&
            raw.complexity > 0
                ? raw.complexity
                : null,
    };
};
 
/** A snapshot read from parsed JSON; a malformed issue is dropped and reported, never thrown on. */
export const readSnapshot = (raw: unknown): { snapshot: Snapshot; problems: string[] } => {
    const reader = new Reader();
    const source = isRecord(raw) ? raw : {};
 
    Iif (!isRecord(raw)) reader.problems.push("the file isn't an object");
 
    const issuesOf = (key: string) =>
        reader
            .list(source, key, "snapshot")
            .flatMap((issue) => readIssue(reader, issue, `\`${key}\``) ?? []);
 
    const snapshot: Snapshot = {
        syncedAt: reader.text(source, "syncedAt", "snapshot"),
        milestone: reader.text(source, "milestone", "snapshot"),
        issues: issuesOf("issues"),
        noMilestone: issuesOf("noMilestone"),
    };
 
    return { snapshot, problems: reader.problems };
};