Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

forgejoPullRequestJson.ts

285 lines9.4 KB

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 PullRequestCheckStatus,
8 PullRequestComment,
9 PullRequestCommit,
10 PullRequestReaction,
11 PullRequestReactionContent,
12 PullRequestReviewThread,
13} from "@t3tools/contracts";
14import type { ProviderChangeRequest } from "@t3tools/source-control-core/server/PullRequestProvider";
15import { dedupeChecks } from "@t3tools/source-control-core/server/pullRequestChecks";
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 sha: Schema.String,
41 repo: Schema.NullOr(ForgejoRepository),
42});
43export const ForgejoPullRequest = Schema.Struct({
44 number: Schema.Int,
45 title: Schema.String,
46 body: Schema.NullOr(Schema.String),
47 html_url: Schema.String,
48 user: Schema.NullOr(ForgejoUser),
49 state: Schema.String,
50 draft: Schema.optional(Schema.Boolean),
51 merged: Schema.Boolean,
52 mergeable: Schema.optional(Schema.Boolean),
53 is_locked: Schema.optional(Schema.Boolean),
54 head: Branch,
55 base: Branch,
56 merge_base: Schema.optional(Schema.String),
57 created_at: Schema.String,
58 updated_at: Schema.String,
59 closed_at: Schema.NullOr(Schema.String),
60 merged_at: Schema.NullOr(Schema.String),
61 additions: Schema.optional(Schema.NullOr(Schema.Int)),
62 deletions: Schema.optional(Schema.NullOr(Schema.Int)),
63 changed_files: Schema.optional(Schema.NullOr(Schema.Int)),
64 comments: Schema.optional(Schema.Int),
65 labels: Schema.NullOr(Schema.Array(ForgejoLabel)),
66 requested_reviewers: Schema.optional(Schema.NullOr(Schema.Array(ForgejoUser))),
67});
68export const ForgejoComment = Schema.Struct({
69 id: Schema.Int,
70 body: Schema.String,
71 user: Schema.NullOr(ForgejoUser),
72 created_at: Schema.String,
73 updated_at: Schema.optional(Schema.NullOr(Schema.String)),
74 html_url: Schema.optional(Schema.String),
75});
76export const ForgejoReview = Schema.Struct({
77 id: Schema.Int,
78 body: Schema.String,
79 user: Schema.NullOr(ForgejoUser),
80 state: Schema.String,
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 context: Schema.String,
108 status: Schema.String,
109 description: Schema.NullOr(Schema.String),
110 target_url: Schema.NullOr(Schema.String),
111 updated_at: Schema.String,
112});
113export const ForgejoReaction = Schema.Struct({
114 content: Schema.String,
115 user: Schema.NullOr(ForgejoUser),
116});
117​
118export function forgejoActor(
119 user: typeof ForgejoUser.Type | null | undefined,
120): PullRequestActor | null {
121 return user?.login
122 ? { login: user.login, name: user.full_name || null, avatarUrl: user.avatar_url || null }
123 : null;
125​
126function toIsoUtc(value: string): string {
127 return Option.match(DateTime.make(value), { onNone: () => value, onSome: DateTime.formatIso });
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: pr.head.ref,
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;
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 };
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.state === "REQUEST_CHANGES"
181 ? "CHANGES_REQUESTED"
182 : review.state === "COMMENT"
183 ? "COMMENTED"
184 : review.state,
185 };
187​
188export function forgejoReviewThread(
189 comment: typeof ForgejoReviewComment.Type,
190): PullRequestReviewThread {
191 const oldSide = comment.position === 0 && comment.original_position > 0;
192 const line = oldSide ? comment.original_position : comment.position;
193 return {
194 id: String(comment.id),
195 path: comment.path,
196 line: line > 0 ? line : null,
197 side: oldSide ? "left" : "right",
198 isResolved: comment.resolver !== null,
199 isOutdated: false,
200 comments: [forgejoComment(comment)],
201 };
203​
204export function forgejoCommit(commit: typeof ForgejoCommit.Type): PullRequestCommit {
205 const author = forgejoActor(commit.author);
206 return {
207 oid: commit.sha,
208 messageHeadline: commit.commit.message.split("\n")[0] ?? "",
209 committedDate: toIsoUtc(commit.commit.committer.date),
210 authors: author ? [author] : [],
211 ...(commit.stats
212 ? { additions: commit.stats.additions, deletions: commit.stats.deletions }
213 : {}),
214 };
216​
217// Forgejo Actions reports a job that did not run as `skipped` and a cancelled one as `failure`,
218// and `warning` is finished without being a pass. Only `pending` and states we do not know may
219// hold a watch open.
220function forgejoCheckStatus(state: string): PullRequestCheckStatus {
221 switch (state) {
222 case "success":
223 return "success";
224 case "failure":
225 case "error":
226 return "failure";
227 case "skipped":
228 return "skipped";
229 case "warning":
230 return "neutral";
231 default:
232 return "pending";
233 }
235​
236export function forgejoChecks(
237 statuses: ReadonlyArray<typeof ForgejoStatus.Type>,
238 pullRequestUrl: string,
239): ReadonlyArray<PullRequestCheck> {
240 return dedupeChecks(
241 statuses.map((status) => ({
242 workflowName: null,
243 at: status.updated_at,
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 );
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 });