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