@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 295 lines 9.86 KB
Raw
1 import { toIsoUtc } from "@t3tools/source-control-core/server/pullRequestUtils";
2 import * as Schema from "effect/Schema";
3 import type {
4 PullRequestActor,
5 PullRequestCheck,
6 PullRequestCheckStatus,
7 PullRequestComment,
8 PullRequestCommit,
9 PullRequestReaction,
10 PullRequestReactionContent,
11 PullRequestReviewThread,
12 } from "@t3tools/contracts";
13 import type { ProviderChangeRequest } from "@t3tools/source-control-core/server/PullRequestProvider";
14 import { dedupeChecks } from "@t3tools/source-control-core/server/pullRequestChecks";
15 import { forgejoHeadBranch } from "./forgejoPullRequests.ts";
16
17 export const ForgejoUser = Schema.Struct({
18 login: Schema.String,
19 full_name: Schema.optional(Schema.NullOr(Schema.String)),
20 avatar_url: Schema.optional(Schema.NullOr(Schema.String)),
21 });
22 export const ForgejoLabel = Schema.Struct({
23 id: Schema.Int,
24 name: Schema.String,
25 color: Schema.optional(Schema.NullOr(Schema.String)),
26 description: Schema.optional(Schema.NullOr(Schema.String)),
27 });
28 export const ForgejoRepository = Schema.Struct({
29 full_name: Schema.String,
30 permissions: Schema.optional(Schema.Struct({ push: Schema.Boolean, admin: Schema.Boolean })),
31 archived: Schema.optional(Schema.Boolean),
32 allow_merge_commits: Schema.optional(Schema.Boolean),
33 allow_squash_merge: Schema.optional(Schema.Boolean),
34 allow_rebase: Schema.optional(Schema.Boolean),
35 allow_fast_forward_only_merge: Schema.optional(Schema.Boolean),
36 allow_rebase_update: Schema.optional(Schema.Boolean),
37 default_delete_branch_after_merge: Schema.optional(Schema.Boolean),
38 });
39 const Branch = Schema.Struct({
40 ref: Schema.String,
41 label: Schema.optional(Schema.String),
42 sha: Schema.String,
43 repo: Schema.NullOr(ForgejoRepository),
44 });
45 export const ForgejoPullRequest = Schema.Struct({
46 number: Schema.Int,
47 title: Schema.String,
48 body: Schema.NullOr(Schema.String),
49 html_url: Schema.String,
50 user: Schema.NullOr(ForgejoUser),
51 state: Schema.String,
52 draft: Schema.optional(Schema.Boolean),
53 flow: Schema.optional(Schema.Int),
54 merged: Schema.Boolean,
55 mergeable: Schema.optional(Schema.Boolean),
56 is_locked: Schema.optional(Schema.Boolean),
57 head: Branch,
58 base: Branch,
59 merge_base: Schema.optional(Schema.String),
60 created_at: Schema.String,
61 updated_at: Schema.String,
62 closed_at: Schema.NullOr(Schema.String),
63 merged_at: Schema.NullOr(Schema.String),
64 merge_commit_sha: Schema.optional(Schema.NullOr(Schema.String)),
65 merged_by: Schema.optional(Schema.NullOr(ForgejoUser)),
66 additions: Schema.optional(Schema.NullOr(Schema.Int)),
67 deletions: Schema.optional(Schema.NullOr(Schema.Int)),
68 changed_files: Schema.optional(Schema.NullOr(Schema.Int)),
69 comments: Schema.optional(Schema.Int),
70 labels: Schema.NullOr(Schema.Array(ForgejoLabel)),
71 requested_reviewers: Schema.optional(Schema.NullOr(Schema.Array(ForgejoUser))),
72 });
73 export const ForgejoComment = Schema.Struct({
74 id: Schema.Int,
75 body: Schema.String,
76 user: Schema.NullOr(ForgejoUser),
77 created_at: Schema.String,
78 updated_at: Schema.optional(Schema.NullOr(Schema.String)),
79 html_url: Schema.optional(Schema.String),
80 });
81 export const ForgejoReview = Schema.Struct({
82 id: Schema.Int,
83 body: Schema.String,
84 user: Schema.NullOr(ForgejoUser),
85 state: Schema.String,
86 dismissed: Schema.optional(Schema.Boolean),
87 submitted_at: Schema.String,
88 html_url: Schema.optional(Schema.String),
89 comments_count: Schema.Int,
90 });
91 export const ForgejoReviewComment = Schema.Struct({
92 ...ForgejoComment.fields,
93 path: Schema.String,
94 position: Schema.Int,
95 original_position: Schema.Int,
96 commit_id: Schema.String,
97 original_commit_id: Schema.String,
98 resolver: Schema.NullOr(ForgejoUser),
99 });
100 export const ForgejoCommit = Schema.Struct({
101 sha: Schema.String,
102 author: Schema.NullOr(ForgejoUser),
103 commit: Schema.Struct({
104 message: Schema.String,
105 committer: Schema.Struct({ date: Schema.String }),
106 }),
107 parents: Schema.Array(Schema.Struct({ sha: Schema.String })),
108 stats: Schema.optional(
109 Schema.NullOr(Schema.Struct({ additions: Schema.Int, deletions: Schema.Int })),
110 ),
111 });
112 export const ForgejoStatus = Schema.Struct({
113 id: Schema.optional(Schema.Int),
114 context: Schema.String,
115 status: Schema.String,
116 description: Schema.NullOr(Schema.String),
117 target_url: Schema.NullOr(Schema.String),
118 updated_at: Schema.String,
119 });
120 export const ForgejoReaction = Schema.Struct({
121 content: Schema.String,
122 user: Schema.NullOr(ForgejoUser),
123 });
124
125 export function forgejoActor(
126 user: typeof ForgejoUser.Type | null | undefined,
127 ): PullRequestActor | null {
128 return user?.login
129 ? { login: user.login, name: user.full_name || null, avatarUrl: user.avatar_url || null }
130 : null;
131 }
132
133 export function forgejoChangeRequest(pr: typeof ForgejoPullRequest.Type) {
134 return {
135 number: pr.number,
136 title: pr.title,
137 url: pr.html_url,
138 author: forgejoActor(pr.user),
139 headBranch: forgejoHeadBranch(pr),
140 baseBranch: pr.base.ref,
141 headRepositoryNameWithOwner: pr.head.repo?.full_name ?? null,
142 state: pr.merged ? "merged" : pr.state === "closed" ? "closed" : "open",
143 isDraft: pr.draft ?? /^(?:\[WIP\]|WIP:)/i.test(pr.title),
144 // A false value also covers drafts and unfinished or failed conflict checks.
145 mergeability: pr.mergeable === true ? "mergeable" : "unknown",
146 additions: pr.additions ?? 0,
147 deletions: pr.deletions ?? 0,
148 createdAt: toIsoUtc(pr.created_at),
149 updatedAt: toIsoUtc(pr.updated_at),
150 closedAt: pr.closed_at === null ? null : toIsoUtc(pr.closed_at),
151 mergedAt: pr.merged_at === null ? null : toIsoUtc(pr.merged_at),
152 reviewRequestLogins: (pr.requested_reviewers ?? []).map((user) => user.login),
153 labels: (pr.labels ?? []).map((label) => ({
154 name: label.name,
155 color: label.color ?? null,
156 description: label.description || null,
157 })),
158 } satisfies ProviderChangeRequest;
159 }
160
161 export function forgejoComment(comment: typeof ForgejoComment.Type): PullRequestComment {
162 const createdAt = toIsoUtc(comment.created_at);
163 const editedAt = comment.updated_at == null ? undefined : toIsoUtc(comment.updated_at);
164 return {
165 id: String(comment.id),
166 kind: "issue-comment",
167 author: forgejoActor(comment.user),
168 body: comment.body,
169 createdAt,
170 ...(editedAt === undefined || editedAt === createdAt ? {} : { editedAt }),
171 url: comment.html_url || null,
172 path: null,
173 reviewState: null,
174 };
175 }
176
177 export function forgejoReview(review: typeof ForgejoReview.Type): PullRequestComment {
178 return {
179 id: `review:${review.id}`,
180 kind: "review",
181 author: forgejoActor(review.user),
182 body: review.body,
183 createdAt: toIsoUtc(review.submitted_at),
184 url: review.html_url || null,
185 path: null,
186 reviewState:
187 review.dismissed === true
188 ? "DISMISSED"
189 : review.state === "REQUEST_CHANGES"
190 ? "CHANGES_REQUESTED"
191 : review.state === "COMMENT"
192 ? "COMMENTED"
193 : review.state,
194 };
195 }
196
197 export function forgejoReviewThread(
198 comment: typeof ForgejoReviewComment.Type,
199 ): PullRequestReviewThread {
200 const oldSide = comment.position === 0 && comment.original_position > 0;
201 const line = oldSide ? comment.original_position : comment.position;
202 return {
203 id: String(comment.id),
204 path: comment.path,
205 line: line > 0 ? line : null,
206 side: oldSide ? "left" : "right",
207 isResolved: comment.resolver !== null,
208 isOutdated: false,
209 comments: [forgejoComment(comment)],
210 };
211 }
212
213 export function forgejoCommit(commit: typeof ForgejoCommit.Type): PullRequestCommit {
214 const author = forgejoActor(commit.author);
215 return {
216 oid: commit.sha,
217 messageHeadline: commit.commit.message.split("\n")[0] ?? "",
218 committedDate: toIsoUtc(commit.commit.committer.date),
219 authors: author ? [author] : [],
220 ...(commit.stats
221 ? { additions: commit.stats.additions, deletions: commit.stats.deletions }
222 : {}),
223 };
224 }
225
226 // Forgejo Actions reports a job that did not run as `skipped` and a cancelled one as `failure`,
227 // and `warning` is finished without being a pass. Only `pending` and states we do not know may
228 // hold a watch open.
229 function forgejoCheckStatus(state: string): PullRequestCheckStatus {
230 switch (state) {
231 case "success":
232 return "success";
233 case "failure":
234 case "error":
235 return "failure";
236 case "skipped":
237 return "skipped";
238 case "warning":
239 return "neutral";
240 default:
241 return "pending";
242 }
243 }
244
245 export function forgejoChecks(
246 statuses: ReadonlyArray<typeof ForgejoStatus.Type>,
247 pullRequestUrl: string,
248 ): ReadonlyArray<PullRequestCheck> {
249 return dedupeChecks(
250 statuses.map((status) => ({
251 workflowName: null,
252 at: status.updated_at,
253 ...(status.id === undefined ? {} : { sequence: status.id }),
254 check: {
255 name: status.context || "check",
256 description: status.description || null,
257 url: status.target_url
258 ? (URL.parse(status.target_url, pullRequestUrl)?.href ?? null)
259 : null,
260 status: forgejoCheckStatus(status.status),
261 },
262 })),
263 );
264 }
265
266 export const FORGEJO_REACTIONS: Record<PullRequestReactionContent, string> = {
267 "thumbs-up": "+1",
268 "thumbs-down": "-1",
269 laugh: "laugh",
270 hooray: "hooray",
271 confused: "confused",
272 heart: "heart",
273 rocket: "rocket",
274 eyes: "eyes",
275 };
276 export function forgejoReactions(
277 reactions: ReadonlyArray<typeof ForgejoReaction.Type>,
278 viewer: string,
279 ): ReadonlyArray<PullRequestReaction> {
280 return Object.entries(FORGEJO_REACTIONS).flatMap(([content, emoji]) => {
281 const matching = reactions.filter((reaction) => reaction.content === emoji);
282 return matching.length === 0
283 ? []
284 : [
285 {
286 content: content as PullRequestReactionContent,
287 count: matching.length,
288 actors: matching.flatMap((reaction) =>
289 reaction.user?.login && reaction.user.login !== viewer ? [reaction.user.login] : [],
290 ),
291 viewerHasReacted: matching.some((reaction) => reaction.user?.login === viewer),
292 },
293 ];
294 });
295 }