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