The best way to control Claude Code, Codex and more from any computer or phone
| 1 | /** |
| 2 | * Forgejo's issue shapes, beside the pull request ones they share users, labels and comments |
| 3 | * with. Gitea answers the same API, so a Gitea server reads through this too. |
| 4 | * |
| 5 | * @module source-control-forgejo/server/forgejoIssueJson |
| 6 | */ |
| 7 | import * as DateTime from "effect/DateTime"; |
| 8 | import * as Option from "effect/Option"; |
| 9 | import * as Schema from "effect/Schema"; |
| 10 | import type { IssueComment, IssueEvent, IssueState, IssueTemplate } from "@t3tools/contracts"; |
| 11 | import type { TrackerIssueSummary } from "@t3tools/source-control-core/server/IssueTracker"; |
| 12 | |
| 13 | import { |
| 14 | ForgejoComment, |
| 15 | ForgejoLabel, |
| 16 | ForgejoRepository, |
| 17 | ForgejoUser, |
| 18 | forgejoActor, |
| 19 | } from "./forgejoPullRequestJson.ts"; |
| 20 | |
| 21 | export const ForgejoIssue = Schema.Struct({ |
| 22 | number: Schema.Int, |
| 23 | title: Schema.String, |
| 24 | body: Schema.optional(Schema.NullOr(Schema.String)), |
| 25 | html_url: Schema.String, |
| 26 | user: Schema.NullOr(ForgejoUser), |
| 27 | state: Schema.String, |
| 28 | created_at: Schema.String, |
| 29 | updated_at: Schema.String, |
| 30 | closed_at: Schema.optional(Schema.NullOr(Schema.String)), |
| 31 | assignees: Schema.optional(Schema.NullOr(Schema.Array(ForgejoUser))), |
| 32 | labels: Schema.optional(Schema.NullOr(Schema.Array(ForgejoLabel))), |
| 33 | comments: Schema.optional(Schema.Int), |
| 34 | is_locked: Schema.optional(Schema.Boolean), |
| 35 | /** Present on a row that is really a pull request, which the issue listing also returns. */ |
| 36 | pull_request: Schema.optional(Schema.NullOr(Schema.Struct({ merged: Schema.Boolean }))), |
| 37 | }); |
| 38 | |
| 39 | export const ForgejoIssueRepository = Schema.Struct({ |
| 40 | ...ForgejoRepository.fields, |
| 41 | has_issues: Schema.optional(Schema.Boolean), |
| 42 | }); |
| 43 | |
| 44 | /** One row of `/issues/{index}/timeline`, which is a comment with a type and a subject. */ |
| 45 | export const ForgejoTimelineEntry = Schema.Struct({ |
| 46 | id: Schema.Int, |
| 47 | type: Schema.String, |
| 48 | body: Schema.optional(Schema.NullOr(Schema.String)), |
| 49 | user: Schema.optional(Schema.NullOr(ForgejoUser)), |
| 50 | created_at: Schema.String, |
| 51 | html_url: Schema.optional(Schema.NullOr(Schema.String)), |
| 52 | assignee: Schema.optional(Schema.NullOr(ForgejoUser)), |
| 53 | removed_assignee: Schema.optional(Schema.Boolean), |
| 54 | new_title: Schema.optional(Schema.NullOr(Schema.String)), |
| 55 | label: Schema.optional(Schema.NullOr(ForgejoLabel)), |
| 56 | ref_issue: Schema.optional( |
| 57 | Schema.NullOr( |
| 58 | Schema.Struct({ |
| 59 | number: Schema.Int, |
| 60 | html_url: Schema.String, |
| 61 | /** Where the citing issue or pull request lives, which need not be this repository. */ |
| 62 | repository: Schema.optional(Schema.NullOr(Schema.Struct({ full_name: Schema.String }))), |
| 63 | pull_request: Schema.optional(Schema.NullOr(Schema.Struct({ merged: Schema.Boolean }))), |
| 64 | }), |
| 65 | ), |
| 66 | ), |
| 67 | }); |
| 68 | |
| 69 | export const ForgejoIssueTemplate = Schema.Struct({ |
| 70 | file_name: Schema.String, |
| 71 | name: Schema.optional(Schema.NullOr(Schema.String)), |
| 72 | about: Schema.optional(Schema.NullOr(Schema.String)), |
| 73 | title: Schema.optional(Schema.NullOr(Schema.String)), |
| 74 | content: Schema.optional(Schema.NullOr(Schema.String)), |
| 75 | labels: Schema.optional(Schema.NullOr(Schema.Array(Schema.String))), |
| 76 | assignees: Schema.optional(Schema.NullOr(Schema.Array(Schema.String))), |
| 77 | }); |
| 78 | |
| 79 | export const ForgejoIssueConfig = Schema.Struct({ |
| 80 | blank_issues_enabled: Schema.optional(Schema.Boolean), |
| 81 | }); |
| 82 | |
| 83 | function toIsoUtc(value: string): string { |
| 84 | return Option.match(DateTime.make(value), { onNone: () => value, onSome: DateTime.formatIso }); |
| 85 | } |
| 86 | |
| 87 | const OPEN: IssueState = { category: "open", name: "Open" }; |
| 88 | const CLOSED: IssueState = { category: "closed", name: "Closed" }; |
| 89 | |
| 90 | /** Forgejo has exactly the two states and records no reason for a close. */ |
| 91 | export const ISSUE_STATES: ReadonlyArray<IssueState> = [OPEN, CLOSED]; |
| 92 | |
| 93 | export function forgejoIssue(issue: typeof ForgejoIssue.Type): TrackerIssueSummary { |
| 94 | return { |
| 95 | key: String(issue.number), |
| 96 | displayKey: `#${issue.number}`, |
| 97 | title: issue.title, |
| 98 | url: issue.html_url, |
| 99 | author: forgejoActor(issue.user), |
| 100 | state: issue.state === "closed" ? CLOSED : OPEN, |
| 101 | createdAt: toIsoUtc(issue.created_at), |
| 102 | updatedAt: toIsoUtc(issue.updated_at), |
| 103 | closedAt: issue.closed_at ? toIsoUtc(issue.closed_at) : null, |
| 104 | assignees: (issue.assignees ?? []).flatMap((user) => forgejoActor(user) ?? []), |
| 105 | labels: (issue.labels ?? []).map((label) => ({ name: label.name, color: label.color ?? null })), |
| 106 | commentCount: Math.max(0, issue.comments ?? 0), |
| 107 | }; |
| 108 | } |
| 109 | |
| 110 | export function forgejoIssueComment(comment: typeof ForgejoComment.Type): IssueComment { |
| 111 | return { |
| 112 | id: String(comment.id), |
| 113 | author: forgejoActor(comment.user), |
| 114 | body: comment.body, |
| 115 | createdAt: toIsoUtc(comment.created_at), |
| 116 | url: comment.html_url || null, |
| 117 | }; |
| 118 | } |
| 119 | |
| 120 | /** Null for a timeline entry with no kind in the shared vocabulary, which is dropped. */ |
| 121 | export function forgejoIssueEvent(entry: typeof ForgejoTimelineEntry.Type): IssueEvent | null { |
| 122 | const event = (kind: IssueEvent["kind"], detail: string | null = null): IssueEvent => ({ |
| 123 | id: String(entry.id), |
| 124 | kind, |
| 125 | actor: forgejoActor(entry.user), |
| 126 | createdAt: toIsoUtc(entry.created_at), |
| 127 | detail, |
| 128 | }); |
| 129 | switch (entry.type) { |
| 130 | case "close": |
| 131 | return event("closed"); |
| 132 | case "reopen": |
| 133 | return event("reopened"); |
| 134 | case "assignees": |
| 135 | return event( |
| 136 | entry.removed_assignee ? "unassigned" : "assigned", |
| 137 | entry.assignee?.login || null, |
| 138 | ); |
| 139 | case "change_title": |
| 140 | return event("renamed", entry.new_title || null); |
| 141 | case "label": |
| 142 | // Forgejo writes `1` into the body of a labelling and nothing into a removal. |
| 143 | return event(entry.body === "1" ? "labeled" : "unlabeled", entry.label?.name || null); |
| 144 | case "issue_ref": |
| 145 | case "commit_ref": |
| 146 | case "comment_ref": |
| 147 | case "pull_ref": |
| 148 | return event("referenced", entry.ref_issue ? `#${entry.ref_issue.number}` : null); |
| 149 | default: |
| 150 | return null; |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | /** Only markdown templates carry a body; a YAML form is a questionnaire this cannot flatten. */ |
| 155 | export function forgejoIssueTemplate( |
| 156 | template: typeof ForgejoIssueTemplate.Type, |
| 157 | ): IssueTemplate | null { |
| 158 | const key = template.file_name.trim(); |
| 159 | if (key.length === 0 || !/\.md$/i.test(key)) return null; |
| 160 | return { |
| 161 | key, |
| 162 | name: template.name?.trim() || key, |
| 163 | about: template.about ?? "", |
| 164 | title: template.title ?? "", |
| 165 | body: template.content ?? "", |
| 166 | labels: template.labels ?? [], |
| 167 | assignees: template.assignees ?? [], |
| 168 | }; |
| 169 | } |