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

100% Statements 49/49
88.88% Branches 16/18
100% Functions 8/8
100% Lines 45/45

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                              1x   55x   1x 13x 1x 1x     12x   11x             11x       1x 12x           2x       12x     1x 6x   2x 2x   1x     1x 14x 14x   14x 14x 14x 14x 14x   14x 12x   11x     1x       11x     1x       10x   14x 1x 1x     7x 7x   7x 7x 7x 7x   1x 1x     6x    
import { autocomplete, confirm, select, text } from "@clack/prompts";
import { createIssue } from "../shared/adapters/gh.ts";
import { out, requireTerminal } from "../shared/adapters/io.ts";
import { orExit, type Cancellable } from "../shared/adapters/prompts.ts";
import { listLabels, listProjects, type Label, type Project } from "./adapters/gh.ts";
import { currentBranch } from "./adapters/git.ts";
import {
    labelOptions,
    NONE_OPTION,
    PROJECT_SCOPE_HINT,
    projectOptions,
} from "./adapters/prompts.ts";
import { labelOptionMatches, optionalSelection } from "./domain/pick.ts";
import { pick } from "./pick.ts";
 
const CANCELLED = "Cancelled — no issue created.";
 
const or = <T>(value: Cancellable<T>): T => orExit(value, CANCELLED);
 
const pickProject = async (projects: Project[]): Promise<string | undefined> => {
    if (projects.length === 0) {
        out.warn(PROJECT_SCOPE_HINT);
        return undefined;
    }
 
    if (projects.length === 1) return projects[0]?.title;
 
    const picked = or(
        await select({
            message: "Project",
            options: [...projectOptions(projects), NONE_OPTION],
        }),
    );
 
    return optionalSelection(picked);
};
 
/** Keep `None` visible only for an empty search; once typed, the picker is searching for a label. */
const pickLabel = async (labels: Label[]): Promise<string | undefined> => {
    const picked = or(
        await autocomplete({
            message: "Label",
            placeholder: "Type a label or what it means",
            maxItems: 12,
            options: [...labelOptions(labels), NONE_OPTION],
            filter: (search, option) => labelOptionMatches(NONE_OPTION.value, option, search),
        }),
    );
 
    return optionalSelection(picked);
};
 
const offerPick = async (): Promise<void> => {
    if (currentBranch() !== "master") return;
 
    const wantsPick = or(await confirm({ message: "You're on master — pick an issue now?" }));
    if (!wantsPick) return;
 
    await pick();
};
 
export const add = async (): Promise<void> => {
    requireTerminal("file it with `gh issue create --title … --body … --label … --project …`.");
    out.begin("📝 Add an issue");
 
    const loading = out.spinner();
    loading.start("Asking gh what's available...");
    const projects = listProjects();
    const labels = listLabels();
    loading.stop("Ready.");
 
    const project = await pickProject(projects);
    const label = await pickLabel(labels);
 
    const title = or(
        await text({
            message: "Title",
            validate: (value) => (value?.trim() ? undefined : "A title is required."),
        }),
    );
 
    const body = or(
        await text({
            message: "Description",
            validate: (value) => (value?.trim() ? undefined : "A description is required."),
        }),
    );
 
    out.note(`${body}\n\nlabel: ${label ?? "—"}\nproject: ${project ?? "—"}`, title);
 
    if (!or(await confirm({ message: "Create it?" }))) {
        out.cancelled(CANCELLED);
        return;
    }
 
    const creating = out.spinner();
    creating.start("Creating...");
 
    try {
        const url = createIssue({ title: title.trim(), body: body.trim(), label, project });
        creating.stop("Created.");
        out.end(url);
    } catch (error) {
        creating.stop("gh issue create failed.");
        throw error;
    }
 
    await offerPick();
};