All files / infrastructure/scripts/src/issue/adapters gh.ts

75.51% Statements 37/49
90% Branches 9/10
52.17% Functions 12/23
72.72% Lines 32/44

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 171 172 173 174 175 176 177                                              2x     1x 2x 2x 2x   1x         1x 4x   2x 2x 2x           2x   1x         1x 1x   1x       1x 2x   2x   1x                                     1x 2x 1x       1x     1x   1x       1x         1x         1x                                                       1x                                                       1x       1x            
import { cached, readCache, writeCache } from "../../shared/adapters/cache.ts";
import { gh } from "../../shared/adapters/gh.ts";
import { slugify } from "../domain/branch.ts";
import type { IssueStatus, IssueThread } from "../domain/brief.ts";
 
export interface Project {
    title: string;
    number: number;
    url: string;
}
 
export interface Label {
    name: string;
    description: string;
}
 
export interface Issue {
    number: number;
    title: string;
    url: string;
    labels: string[];
}
 
const repoOwner = (): string => gh("repo", "view", "--json", "owner", "--jq", ".owner.login");
 
/** Whether `gh` is reachable, distinct from `listProjects` swallowing a missing `project` scope the same way. */
export const isOnline = (): boolean => {
    try {
        gh("api", "rate_limit");
        return true;
    } catch {
        return false;
    }
};
 
/** The owner's open projects; empty on a `gh` failure online, the file cache directly when offline. */
export const listProjects = (offline = false): Project[] => {
    if (offline) return readCache<Project[]>("projects") ?? [];
 
    return cached("projects", () => {
        try {
            const { projects } = JSON.parse(
                gh("project", "list", "--owner", repoOwner(), "--format", "json"),
            ) as {
                projects: (Project & { closed: boolean })[];
            };
 
            return projects.filter(({ closed }) => !closed);
        } catch {
            return [];
        }
    });
};
 
export const listLabels = (): Label[] =>
    cached(
        "labels",
        () => JSON.parse(gh("label", "list", "--json", "name,description")) as Label[],
    );
 
/** The repo's open issues on the given project; uncached online, the file cache when offline. */
export const listIssues = (project: string, offline = false): Issue[] => {
    const key = `issues-${slugify(project)}`;
 
    if (offline) return readCache<Issue[]>(key) ?? [];
 
    const issues = JSON.parse(
        gh(
            "issue",
            "list",
            "--state",
            "open",
            "--limit",
            "100",
            "--json",
            "number,title,url,labels,projectItems",
        ),
    ) as {
        number: number;
        title: string;
        url: string;
        labels: { name: string }[];
        projectItems: { title: string }[];
    }[];
 
    const forProject = issues
        .filter(({ projectItems }) => projectItems.some(({ title }) => title === project))
        .map(({ number, title, url, labels }) => ({
            number,
            title,
            url,
            labels: labels.map(({ name }) => name),
        }));
 
    writeCache(key, forProject);
 
    return forProject;
};
 
/** Assigns `@me`; re-adding an existing assignee is a no-op, so this never checks first. */
export const assignToMe = (issue: number): void => {
    gh("issue", "edit", String(issue), "--add-assignee", "@me");
};
 
/** `gh issue develop` over `git checkout -b`: the branch it creates is linked on the issue's Development panel, which a branch name alone never is. */
export const developBranch = (issue: number, branch: string): void => {
    gh("issue", "develop", String(issue), "--name", branch, "--checkout");
};
 
/** Addressed by name rather than node ID — `item-edit` resolves the field and option server-side instead of a separate `field-list` lookup. */
export const moveToInProgress = (project: Project, issue: Issue): void => {
    gh(
        "project",
        "item-edit",
        String(project.number),
        "--owner",
        repoOwner(),
        "--url",
        issue.url,
        "--field",
        "Status",
        "--value",
        "In Progress",
    );
};
 
interface RawThread {
    number: number;
    title: string;
    state: string;
    url: string;
    body: string;
    labels: { name: string }[];
    assignees: { login: string }[];
    projectItems: { title: string; status?: { name: string } | null }[];
    comments: { author: { login: string }; createdAt: string; body: string }[];
}
 
export const viewThread = (issue: number): IssueThread => {
    const raw = JSON.parse(
        gh(
            "issue",
            "view",
            String(issue),
            "--json",
            "number,title,state,url,body,labels,assignees,projectItems,comments",
        ),
    ) as RawThread;
 
    return {
        number: raw.number,
        title: raw.title,
        state: raw.state,
        url: raw.url,
        body: raw.body,
        labels: raw.labels.map(({ name }) => name),
        assignees: raw.assignees.map(({ login }) => login),
        boards: raw.projectItems.map(({ title, status }) => ({ title, status: status?.name })),
        comments: raw.comments.map(({ author, createdAt, body }) => ({
            author: author.login,
            createdAt,
            body,
        })),
    };
};
 
export const issueStatus = (issue: number): IssueStatus =>
    JSON.parse(gh("issue", "view", String(issue), "--json", "number,title,state")) as IssueStatus;
 
/** Every blocked-by link on the issue, read live so a pick never trusts a stale snapshot. */
export const blockedBy = (issue: number): IssueStatus[] =>
    (
        JSON.parse(gh("issue", "view", String(issue), "--json", "blockedBy")) as {
            blockedBy: { nodes: IssueStatus[] };
        }
    ).blockedBy.nodes.map(({ number, title, state }) => ({ number, title, state }));