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) => ({
151 name: label.name,
152 color: label.color ?? null,
153 description: label.description || null,
154 })),
155 } satisfies ProviderChangeRequest;
156}
157
158export function forgejoComment(comment: typeof ForgejoComment.Type): PullRequestComment {
159 const createdAt = toIsoUtc(comment.created_at);
160 const editedAt = comment.updated_at == null ? undefined : toIsoUtc(comment.updated_at);
161 return {
162 id: String(comment.id),
163 kind: "issue-comment",
164 author: forgejoActor(comment.user),
165 body: comment.body,
166 createdAt,
167 ...(editedAt === undefined || editedAt === createdAt ? {} : { editedAt }),
168 url: comment.html_url || null,
169 path: null,
170 reviewState: null,
171 };
172}
173
174export function forgejoReview(review: typeof ForgejoReview.Type): PullRequestComment {
175 return {
176 id: `review:${review.id}`,
177 kind: "review",
178 author: forgejoActor(review.user),
179 body: review.body,
180 createdAt: toIsoUtc(review.submitted_at),
181 url: review.html_url || null,
182 path: null,
183 reviewState:
184 review.dismissed === true
185 ? "DISMISSED"
186 : review.state === "REQUEST_CHANGES"
187 ? "CHANGES_REQUESTED"
188 : review.state === "COMMENT"
189 ? "COMMENTED"
190 : review.state,
191 };
192}
193
194export function forgejoReviewThread(
195 comment: typeof ForgejoReviewComment.Type,
196): PullRequestReviewThread {
197 const oldSide = comment.position === 0 && comment.original_position > 0;
198 const line = oldSide ? comment.original_position : comment.position;
199 return {
200 id: String(comment.id),
201 path: comment.path,
202 line: line > 0 ? line : null,
203 side: oldSide ? "left" : "right",
204 isResolved: comment.resolver !== null,
205 isOutdated: false,
206 comments: [forgejoComment(comment)],
207 };
208}
209
210export function forgejoCommit(commit: typeof ForgejoCommit.Type): PullRequestCommit {
211 const author = forgejoActor(commit.author);
212 return {
213 oid: commit.sha,
214 messageHeadline: commit.commit.message.split("\n")[0] ?? "",
215 committedDate: toIsoUtc(commit.commit.committer.date),
216 authors: author ? [author] : [],
217 ...(commit.stats
218 ? { additions: commit.stats.additions, deletions: commit.stats.deletions }
219 : {}),
220 };
221}
222
223// Forgejo Actions reports a job that did not run as `skipped` and a cancelled one as `failure`,
224// and `warning` is finished without being a pass. Only `pending` and states we do not know may
225// hold a watch open.
226function forgejoCheckStatus(state: string): PullRequestCheckStatus {
227 switch (state) {
228 case "success":
229 return "success";
230 case "failure":
231 case "error":
232 return "failure";
233 case "skipped":
234 return "skipped";
235 case "warning":
236 return "neutral";
237 default:
238 return "pending";
239 }
240}
241
242export function forgejoChecks(
243 statuses: ReadonlyArray<typeof ForgejoStatus.Type>,
244 pullRequestUrl: string,
245): ReadonlyArray<PullRequestCheck> {
246 return dedupeChecks(
247 statuses.map((status) => ({
248 workflowName: null,
249 at: status.updated_at,
250 ...(status.id === undefined ? {} : { sequence: status.id }),
251 check: {
252 name: status.context || "check",
253 description: status.description || null,
254 url: status.target_url
255 ? (URL.parse(status.target_url, pullRequestUrl)?.href ?? null)
256 : null,
257 status: forgejoCheckStatus(status.status),
258 },
259 })),
260 );
261}
262
263export const FORGEJO_REACTIONS: Record<PullRequestReactionContent, string> = {
264 "thumbs-up": "+1",
265 "thumbs-down": "-1",
266 laugh: "laugh",
267 hooray: "hooray",
268 confused: "confused",
269 heart: "heart",
270 rocket: "rocket",
271 eyes: "eyes",
272};
273export function forgejoReactions(
274 reactions: ReadonlyArray<typeof ForgejoReaction.Type>,
275 viewer: string,
276): ReadonlyArray<PullRequestReaction> {
277 return Object.entries(FORGEJO_REACTIONS).flatMap(([content, emoji]) => {
278 const matching = reactions.filter((reaction) => reaction.content === emoji);
279 return matching.length === 0
280 ? []
281 : [
282 {
283 content: content as PullRequestReactionContent,
284 count: matching.length,
285 actors: matching.flatMap((reaction) =>
286 reaction.user?.login && reaction.user.login !== viewer ? [reaction.user.login] : [],
287 ),
288 viewerHasReacted: matching.some((reaction) => reaction.user?.login === viewer),
289 },
290 ];
291 });
292}