The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import * as Effect from "effect/Effect"; |
| 2 | import * as Result from "effect/Result"; |
| 3 | import * as Schema from "effect/Schema"; |
| 4 | import { parse as parseYaml } from "yaml"; |
| 5 | import { IssueTrackerKind, TrimmedNonEmptyString, type IssueTemplate } from "@t3tools/contracts"; |
| 6 | import { IssueTrackerError } from "@t3tools/source-control-core/server/IssueTracker"; |
| 7 | |
| 8 | import type * as Issues from "./issues.graphql.ts"; |
| 9 | import { toIssueTemplates } from "./gitHubIssueJson.ts"; |
| 10 | |
| 11 | const Names = Schema.Union([Schema.String, Schema.Array(Schema.String)]); |
| 12 | const FormOption = Schema.Union([ |
| 13 | Schema.String, |
| 14 | Schema.Struct({ label: Schema.String, required: Schema.optional(Schema.Boolean) }), |
| 15 | ]); |
| 16 | const FormField = Schema.Struct({ |
| 17 | type: Schema.String, |
| 18 | id: Schema.optional(Schema.String), |
| 19 | attributes: Schema.optional( |
| 20 | Schema.Struct({ |
| 21 | label: Schema.optional(Schema.String), |
| 22 | description: Schema.optional(Schema.String), |
| 23 | value: Schema.optional(Schema.String), |
| 24 | placeholder: Schema.optional(Schema.String), |
| 25 | render: Schema.optional(Schema.String), |
| 26 | multiple: Schema.optional(Schema.Boolean), |
| 27 | default: Schema.optional(Schema.Union([Schema.Int, Schema.Array(Schema.Int)])), |
| 28 | options: Schema.optional(Schema.Array(FormOption)), |
| 29 | }), |
| 30 | ), |
| 31 | validations: Schema.optional( |
| 32 | Schema.Struct({ |
| 33 | required: Schema.optional(Schema.Boolean), |
| 34 | accept: Schema.optional(Schema.String), |
| 35 | }), |
| 36 | ), |
| 37 | }); |
| 38 | const decodeForm = Schema.decodeUnknownSync( |
| 39 | Schema.Struct({ |
| 40 | name: TrimmedNonEmptyString, |
| 41 | description: Schema.String, |
| 42 | title: Schema.optional(Schema.String), |
| 43 | labels: Schema.optional(Names), |
| 44 | assignees: Schema.optional(Names), |
| 45 | body: Schema.Array(FormField), |
| 46 | }), |
| 47 | ); |
| 48 | const decodeConfig = Schema.decodeUnknownSync( |
| 49 | Schema.Struct({ blank_issues_enabled: Schema.optional(Schema.Boolean) }), |
| 50 | ); |
| 51 | |
| 52 | function names(value: string | ReadonlyArray<string> | undefined): ReadonlyArray<string> { |
| 53 | return (typeof value === "string" ? value.split(",") : (value ?? [])) |
| 54 | .map((name) => name.trim()) |
| 55 | .filter((name) => name.length > 0); |
| 56 | } |
| 57 | |
| 58 | function fieldMarkdown(field: typeof FormField.Type): string { |
| 59 | const attributes = field.attributes; |
| 60 | if (field.type === "markdown") return attributes?.value ?? ""; |
| 61 | |
| 62 | const sections = [`### ${attributes?.label ?? field.id ?? field.type}`]; |
| 63 | if (attributes?.description) sections.push(attributes.description); |
| 64 | if (field.validations?.required) sections.push("Required."); |
| 65 | if (attributes?.placeholder) |
| 66 | sections.push(`Example:\n> ${attributes.placeholder.trim().replaceAll("\n", "\n> ")}`); |
| 67 | if (attributes?.render) sections.push(`Format: ${attributes.render}`); |
| 68 | if (field.validations?.accept) sections.push(`Accepted files: ${field.validations.accept}`); |
| 69 | |
| 70 | const options = attributes?.options ?? []; |
| 71 | if (field.type === "checkboxes") { |
| 72 | sections.push( |
| 73 | options |
| 74 | .map((option) => { |
| 75 | const label = typeof option === "string" ? option : option.label; |
| 76 | const required = typeof option !== "string" && option.required; |
| 77 | return `- [ ] ${label}${required ? " (required)" : ""}`; |
| 78 | }) |
| 79 | .join("\n"), |
| 80 | ); |
| 81 | } else if (field.type === "dropdown") { |
| 82 | const defaults = attributes?.default; |
| 83 | const defaultIndices = typeof defaults === "number" ? [defaults] : (defaults ?? []); |
| 84 | sections.push(attributes?.multiple ? "Choose one or more:" : "Choose one:"); |
| 85 | sections.push( |
| 86 | options |
| 87 | .map((option, index) => { |
| 88 | const label = typeof option === "string" ? option : option.label; |
| 89 | return `- ${label}${defaultIndices.includes(index) ? " (default)" : ""}`; |
| 90 | }) |
| 91 | .join("\n"), |
| 92 | ); |
| 93 | } |
| 94 | if (attributes?.value) sections.push(attributes.value); |
| 95 | return sections.filter(Boolean).join("\n\n"); |
| 96 | } |
| 97 | |
| 98 | export const readIssueTemplates = Effect.fnUntraced(function* ( |
| 99 | repository: NonNullable<Issues.IssueTemplates.Result["repository"]>, |
| 100 | ) { |
| 101 | const templates = new Map<string, IssueTemplate>( |
| 102 | toIssueTemplates(repository).map((template) => [ |
| 103 | template.key.slice(template.key.lastIndexOf("/") + 1), |
| 104 | template, |
| 105 | ]), |
| 106 | ); |
| 107 | let blankIssuesEnabled = true; |
| 108 | const directory = repository.templateDirectory; |
| 109 | for (const entry of directory?.__typename === "Tree" ? (directory.entries ?? []) : []) { |
| 110 | if (!/\.ya?ml$/i.test(entry.name)) continue; |
| 111 | const blob = entry.object; |
| 112 | if (blob?.__typename !== "Blob" || blob.isTruncated || blob.text === null) { |
| 113 | return yield* new IssueTrackerError({ |
| 114 | tracker: IssueTrackerKind.make("github"), |
| 115 | operation: "listTemplates", |
| 116 | reason: "failed", |
| 117 | detail: "GitHub returned incomplete issue template contents.", |
| 118 | }); |
| 119 | } |
| 120 | const text = blob.text; |
| 121 | const isConfig = /^config\.ya?ml$/i.test(entry.name); |
| 122 | const parsed = yield* Effect.try({ |
| 123 | try: () => { |
| 124 | const document: unknown = parseYaml(text); |
| 125 | if (isConfig) { |
| 126 | return { config: decodeConfig(document ?? {}) }; |
| 127 | } |
| 128 | return { form: decodeForm(document) }; |
| 129 | }, |
| 130 | catch: (cause) => |
| 131 | new IssueTrackerError({ |
| 132 | tracker: IssueTrackerKind.make("github"), |
| 133 | operation: "listTemplates", |
| 134 | reason: "failed", |
| 135 | detail: "The repository has an unreadable issue template or configuration.", |
| 136 | cause, |
| 137 | }), |
| 138 | }).pipe(Effect.result); |
| 139 | if (Result.isFailure(parsed)) { |
| 140 | if (isConfig) return yield* parsed.failure; |
| 141 | continue; |
| 142 | } |
| 143 | if (parsed.success.config !== undefined) { |
| 144 | blankIssuesEnabled = parsed.success.config.blank_issues_enabled ?? true; |
| 145 | } else if (parsed.success.form !== undefined) { |
| 146 | const form = parsed.success.form; |
| 147 | templates.set(entry.name, { |
| 148 | key: entry.name, |
| 149 | name: form.name, |
| 150 | about: form.description, |
| 151 | title: form.title ?? "", |
| 152 | labels: names(form.labels), |
| 153 | assignees: names(form.assignees), |
| 154 | body: form.body.map(fieldMarkdown).filter(Boolean).join("\n\n"), |
| 155 | }); |
| 156 | } |
| 157 | } |
| 158 | return { templates: [...templates.values()], blankIssuesEnabled }; |
| 159 | }); |