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

gitLabMergeRequests.ts

165 lines6.1 KB

1import * as Cause from "effect/Cause";
2import * as DateTime from "effect/DateTime";
3import * as Exit from "effect/Exit";
4import * as Option from "effect/Option";
5import * as Result from "effect/Result";
6import * as Schema from "effect/Schema";
7import { PositiveInt, TrimmedNonEmptyString } from "@t3tools/contracts";
8import { decodeJsonResult } from "@t3tools/shared/schemaJson";
9​
10export interface NormalizedGitLabMergeRequestRecord {
11 readonly number: number;
12 readonly title: string;
13 readonly url: string;
14 readonly baseRefName: string;
15 readonly headRefName: string;
16 readonly headSha?: string;
17 readonly state: "open" | "closed" | "merged";
18 readonly isDraft?: boolean;
19 readonly closedAt?: string | null;
20 readonly mergedAt?: string | null;
21 readonly updatedAt: Option.Option<DateTime.Utc>;
22 readonly isCrossRepository?: boolean;
23 readonly headRepositoryNameWithOwner?: string | null;
24 readonly headRepositoryOwnerLogin?: string | null;
26​
27const GitLabProjectReferenceSchema = Schema.Struct({
28 path_with_namespace: Schema.optional(Schema.String),
29 pathWithNamespace: Schema.optional(Schema.String),
30 namespace: Schema.optional(
31 Schema.NullOr(
32 Schema.Struct({
33 path: Schema.optional(Schema.String),
34 full_path: Schema.optional(Schema.String),
35 fullPath: Schema.optional(Schema.String),
36 }),
37 ),
38 ),
39});
40​
41const GitLabMergeRequestSchema = Schema.Struct({
42 iid: PositiveInt,
43 title: TrimmedNonEmptyString,
44 web_url: TrimmedNonEmptyString,
45 source_branch: TrimmedNonEmptyString,
46 target_branch: TrimmedNonEmptyString,
47 sha: Schema.optional(Schema.NullOr(Schema.String)),
48 state: Schema.optional(Schema.NullOr(Schema.String)),
49 draft: Schema.optional(Schema.Boolean),
50 work_in_progress: Schema.optional(Schema.Boolean),
51 closed_at: Schema.optional(Schema.NullOr(Schema.String)),
52 merged_at: Schema.optional(Schema.NullOr(Schema.String)),
53 updated_at: Schema.optional(Schema.OptionFromNullOr(Schema.DateTimeUtcFromString)),
54 source_project_id: Schema.optional(Schema.NullOr(Schema.Number)),
55 target_project_id: Schema.optional(Schema.NullOr(Schema.Number)),
56 source_project: Schema.optional(Schema.NullOr(GitLabProjectReferenceSchema)),
57 target_project: Schema.optional(Schema.NullOr(GitLabProjectReferenceSchema)),
58});
59​
60function trimOptionalString(value: string | null | undefined): string | null {
61 const trimmed = value?.trim() ?? "";
62 return trimmed.length > 0 ? trimmed : null;
64​
65function normalizeGitLabMergeRequestState(
66 state: string | null | undefined,
67): "open" | "closed" | "merged" {
68 const normalized = state?.trim().toLowerCase();
69 if (normalized === "merged") {
70 return "merged";
71 }
72 if (normalized === "closed") {
73 return "closed";
74 }
75 return "open";
77​
78function projectPathWithNamespace(
79 project: Schema.Schema.Type<typeof GitLabProjectReferenceSchema> | null | undefined,
80): string | null {
81 const explicit =
82 trimOptionalString(project?.path_with_namespace) ??
83 trimOptionalString(project?.pathWithNamespace);
84 if (explicit) {
85 return explicit;
86 }
87​
88 const namespacePath =
89 trimOptionalString(project?.namespace?.full_path) ??
90 trimOptionalString(project?.namespace?.fullPath) ??
91 trimOptionalString(project?.namespace?.path);
92 return namespacePath;
94​
95function ownerLoginFromPathWithNamespace(pathWithNamespace: string | null): string | null {
96 const [owner] = pathWithNamespace?.split("/") ?? [];
97 return trimOptionalString(owner);
99​
100function normalizeGitLabMergeRequestRecord(
101 raw: Schema.Schema.Type<typeof GitLabMergeRequestSchema>,
102): NormalizedGitLabMergeRequestRecord {
103 const sourceProjectPath = projectPathWithNamespace(raw.source_project);
104 const targetProjectPath = projectPathWithNamespace(raw.target_project);
105 const isCrossRepository =
106 typeof raw.source_project_id === "number" && typeof raw.target_project_id === "number"
107 ? raw.source_project_id !== raw.target_project_id
108 : sourceProjectPath !== null && targetProjectPath !== null
109 ? sourceProjectPath.toLowerCase() !== targetProjectPath.toLowerCase()
110 : undefined;
111 const headRepositoryOwnerLogin = ownerLoginFromPathWithNamespace(sourceProjectPath);
112 const headSha = trimOptionalString(raw.sha);
113​
114 return {
115 number: raw.iid,
116 title: raw.title,
117 url: raw.web_url,
118 baseRefName: raw.target_branch,
119 headRefName: raw.source_branch,
120 ...(headSha ? { headSha } : {}),
121 state: normalizeGitLabMergeRequestState(raw.state),
122 ...(raw.draft === true || raw.work_in_progress === true ? { isDraft: true } : {}),
123 closedAt: raw.closed_at ?? null,
124 mergedAt: raw.merged_at ?? null,
125 updatedAt: raw.updated_at ?? Option.none(),
126 ...(typeof isCrossRepository === "boolean" ? { isCrossRepository } : {}),
127 ...(sourceProjectPath ? { headRepositoryNameWithOwner: sourceProjectPath } : {}),
128 ...(headRepositoryOwnerLogin ? { headRepositoryOwnerLogin } : {}),
129 };
131​
132const decodeGitLabMergeRequestList = decodeJsonResult(Schema.Array(Schema.Unknown));
133const decodeGitLabMergeRequest = decodeJsonResult(GitLabMergeRequestSchema);
134const decodeGitLabMergeRequestEntry = Schema.decodeUnknownExit(GitLabMergeRequestSchema);
135​
136export function decodeGitLabMergeRequestListJson(
137 raw: string,
138): Result.Result<
139 ReadonlyArray<NormalizedGitLabMergeRequestRecord>,
140 Cause.Cause<Schema.SchemaError>
141> {
142 const result = decodeGitLabMergeRequestList(raw);
143 if (Result.isSuccess(result)) {
144 const mergeRequests: NormalizedGitLabMergeRequestRecord[] = [];
145 for (const entry of result.success) {
146 const decodedEntry = decodeGitLabMergeRequestEntry(entry);
147 if (Exit.isFailure(decodedEntry)) {
148 continue;
149 }
150 mergeRequests.push(normalizeGitLabMergeRequestRecord(decodedEntry.value));
151 }
152 return Result.succeed(mergeRequests);
153 }
154 return Result.fail(result.failure);
156​
157export function decodeGitLabMergeRequestJson(
158 raw: string,
159): Result.Result<NormalizedGitLabMergeRequestRecord, Cause.Cause<Schema.SchemaError>> {
160 const result = decodeGitLabMergeRequest(raw);
161 if (Result.isSuccess(result)) {
162 return Result.succeed(normalizeGitLabMergeRequestRecord(result.success));
163 }
164 return Result.fail(result.failure);