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