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