All files / infrastructure/scripts/src/issue/domain brief.ts

96.49% Statements 55/57
68.42% Branches 26/38
93.75% Functions 30/32
96.07% Lines 49/51

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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170                                                                  3x           31x   3x 22x     3x 5x     12x       3x 18x   3x 15x     3x 5x   1x       3x 15x   3x 5x 5x   5x                 3x         5x   3x 2x     3x         7x 3x 3x 3x   12x 7x   1x   7x     3x 7x 3x     3x 25x   3x 5x       1x     5x               1x               6x                       4x               3x 22x 22x 22x    
export interface IssueThread {
    number: number;
    title: string;
    state: string;
    url: string;
    body: string;
    labels: string[];
    assignees: string[];
    boards: { title: string; status: string | undefined }[];
    comments: { author: string; createdAt: string; body: string }[];
}
 
export interface IssueStatus {
    number: number;
    title: string;
    state: string;
}
 
export interface Decision {
    path: string;
    issue: number | undefined;
    status: string | undefined;
    decision: string | undefined;
}
 
export interface Brief {
    thread: IssueThread;
    blockers: IssueStatus[];
    decisions: Decision[];
    projects: { dir: string; agents: string | undefined }[];
    docs: string[];
}
 
const LABEL_ROOTS: Record<string, string> = {
    app: "apps",
    package: "packages",
    infra: "infrastructure",
};
 
const unique = <T>(values: T[]): T[] => [...new Set(values)];
 
const numbersIn = (text: string): number[] =>
    [...text.matchAll(/#(\d+)\b/g)].map((match) => Number(match[1]));
 
/** The `github-issue` task template writes `**Blocked by:** #244, #250`. */
export const blockersIn = (body: string): number[] =>
    unique(
        body
            .split("\n")
            .filter((line) => /blocked by/i.test(line))
            .flatMap(numbersIn),
    );
 
export const threadText = ({ body, comments }: IssueThread): string =>
    [body, ...comments.map(({ body: comment }) => comment)].join("\n");
 
export const referencedIssues = (thread: IssueThread): number[] =>
    unique(numbersIn(threadText(thread))).filter((number) => number !== thread.number);
 
/** `ADR 0028`, `decision 0028` or `0028-some-slug` in prose, as the four-digit file prefix. */
export const decisionNumbersIn = (text: string): string[] =>
    unique(
        [...text.matchAll(/\b(?:ADR|decision)\s+(\d{4})\b|\b(\d{4})-[a-z][\w-]*\.md\b/gi)].map(
            (match) => match[1] ?? match[2] ?? "",
        ),
    ).filter(Boolean);
 
const field = (frontmatter: string, name: string): string | undefined =>
    new RegExp(String.raw`^${name}:\s*(.+)$`, "m").exec(frontmatter)?.[1]?.trim();
 
export const parseDecision = (path: string, text: string): Decision => {
    const frontmatter = /^---\n([\s\S]*?)\n---/.exec(text)?.[1] ?? "";
    const issue = /^\d+$/.exec(field(frontmatter, "issue") ?? "")?.[0];
 
    return {
        path,
        issue: issue === undefined ? undefined : Number(issue),
        status: field(frontmatter, "status"),
        decision: field(frontmatter, "decision"),
    };
};
 
/** A decision's file number is its own, not the issue's, so the match is on frontmatter. */
export const relevantDecisions = (
    decisions: Decision[],
    issues: number[],
    mentioned: string[],
): Decision[] =>
    decisions.filter(
        ({ path, issue }) =>
            (issue !== undefined && issues.includes(issue)) ||
            mentioned.some((number) => path.includes(`/${number}-`)),
    );
 
export const projectsFor = (
    thread: IssueThread,
    branchArea: string | undefined,
    dirs: string[],
): string[] => {
    const fromLabels = thread.labels.flatMap((label) => {
        const [kind, name] = label.split(":");
        const root = kind ? LABEL_ROOTS[kind] : undefined;
        return root && name ? [`${root}/${name}`] : [];
    });
    const fromBranch = branchArea ? dirs.filter((dir) => dir.endsWith(`/${branchArea}`)) : [];
    const fromText = [
        ...threadText(thread).matchAll(/\b(?:apps|packages|infrastructure)\/[\w-]+/g),
    ].map(([path]) => path);
 
    return unique([...fromLabels, ...fromBranch, ...fromText]).filter((dir) => dirs.includes(dir));
};
 
export const docsIn = (text: string, exists: (path: string) => boolean): string[] =>
    unique(
        [...text.matchAll(/(?<![\w./-])(?:docs|\.claude)\/[\w./-]+\.md\b/g)].map(([path]) => path),
    ).filter(exists);
 
const section = (title: string, lines: string[]): string[] =>
    lines.length === 0 ? [] : ["", `## ${title}`, ...lines];
 
export const renderBrief = ({ thread, blockers, decisions, projects, docs }: Brief): string => {
    const meta = [
        thread.state,
        thread.labels.join(", "),
        thread.assignees.map((login) => `@${login}`).join(", ") || "unassigned",
        ...thread.boards.map(({ title, status }) => `${title}: ${status ?? "no status"}`),
    ].filter(Boolean);
 
    return [
        `#${thread.number} ${thread.title}`,
        meta.join(" · "),
        thread.url,
        "",
        thread.body.trim() || "(no body)",
        ...section(
            `Comments (${thread.comments.length})`,
            thread.comments.flatMap(({ author, createdAt, body }) => [
                `@${author} · ${createdAt.slice(0, 10)}`,
                body.trim(),
                "",
            ]),
        ),
        ...section(
            "Blockers",
            blockers.map(({ number, title, state }) => `#${number} ${title} — ${state}`),
        ),
        ...section(
            "Decisions",
            decisions.map(({ path, status, decision }) =>
                [path, status && `status: ${status}`, decision && `decision: ${decision}`]
                    .filter(Boolean)
                    .join(" — "),
            ),
        ),
        ...section(
            "Projects",
            projects.map(({ dir, agents }) => (agents ? `${dir} — read ${agents}` : dir)),
        ),
        ...section("Docs mentioned", docs),
    ]
        .join("\n")
        .trimEnd();
};
 
export const issueNumberFrom = (value: string): number | undefined => {
    const match = /^#?(\d+)$|\/issues\/(\d+)\/?$/.exec(value.trim());
    const digits = match?.[1] ?? match?.[2];
    return digits ? Number(digits) : undefined;
};