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

100% Statements 22/22
87.5% Branches 7/8
100% Functions 6/6
100% Lines 20/20

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                                          1x   1x 3x 3x   1x         1x 4x 4x   4x     4x         1x 5x 5x 5x 5x   4x 4x 4x   5x                 3x                
import { out } from "../shared/adapters/io.ts";
import type { Args } from "../shared/cli.ts";
import { ExpectedError } from "../shared/errors.ts";
import { decisions, exists, projectDirs } from "./adapters/files.ts";
import { blockedBy, issueStatus, viewThread } from "./adapters/gh.ts";
import { currentBranch } from "./adapters/git.ts";
import { issueFromBranch } from "./domain/branch.ts";
import {
    blockersIn,
    decisionNumbersIn,
    docsIn,
    issueNumberFrom,
    projectsFor,
    referencedIssues,
    relevantDecisions,
    renderBrief,
    threadText,
    type IssueStatus,
} from "./domain/brief.ts";
 
const USAGE =
    "Name an issue — pnpm issue:brief <number> — or run it on a <type>/<board>/<number>-<slug> branch.";
 
const statusOf = (number: number): IssueStatus => {
    try {
        return issueStatus(number);
    } catch {
        return { number, title: "(couldn't read it)", state: "UNKNOWN" };
    }
};
 
/** GitHub's own blocked-by links first, then any older `**Blocked by:**` line in the body. */
const blockersOf = (number: number, body: string): IssueStatus[] => {
    const linked = blockedBy(number);
    const linkedNumbers = new Set(linked.map((blocker) => blocker.number));
 
    return [
        ...linked,
        ...blockersIn(body)
            .filter((blocker) => !linkedNumbers.has(blocker))
            .map(statusOf),
    ];
};
 
export const brief = ({ positionals }: Args): void => {
    const [requested] = positionals;
    const branch = issueFromBranch(currentBranch());
    const number = requested ? issueNumberFrom(requested) : branch?.number;
    if (!number) throw new ExpectedError(USAGE);
 
    const thread = viewThread(number);
    const text = threadText(thread);
    const branchArea = branch?.number === number ? branch.area : undefined;
 
    out.info(
        renderBrief({
            thread,
            blockers: blockersOf(number, thread.body),
            decisions: relevantDecisions(
                decisions(),
                [number, ...referencedIssues(thread)],
                decisionNumbersIn(text),
            ),
            projects: projectsFor(thread, branchArea, projectDirs()).map((dir) => ({
                dir,
                agents: exists(`${dir}/CLAUDE.md`) ? `${dir}/CLAUDE.md` : undefined,
            })),
            docs: docsIn(text, exists),
        }),
    );
};