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

forgejoPullRequestJson.ts

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