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

forgejoPullRequestJson.ts

280 lines9.3 KB

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