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