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