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 "@v12/contracts";
8import { decodeJsonResult, formatSchemaError } from "@v12/shared/schemaJson";
9
10export interface NormalizedAzureDevOpsPullRequestRecord {
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}
19
20const AzureDevOpsPullRequestSchema = Schema.Struct({
21 pullRequestId: PositiveInt,
22 title: TrimmedNonEmptyString,
23 url: Schema.optional(Schema.String),
24 repository: Schema.optional(
25 Schema.Struct({
26 name: Schema.optional(Schema.String),
27 webUrl: Schema.optional(Schema.String),
28 project: Schema.optional(
29 Schema.Struct({
30 name: Schema.optional(Schema.String),
31 }),
32 ),
33 }),
34 ),
35 sourceRefName: TrimmedNonEmptyString,
36 targetRefName: TrimmedNonEmptyString,
37 status: Schema.String,
38 creationDate: Schema.optional(Schema.OptionFromNullOr(Schema.DateTimeUtcFromString)),
39 closedDate: Schema.optional(Schema.OptionFromNullOr(Schema.DateTimeUtcFromString)),
40 _links: Schema.optional(
41 Schema.Struct({
42 web: Schema.optional(
43 Schema.Struct({
44 href: Schema.String,
45 }),
46 ),
47 }),
48 ),
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 normalizeRefName(refName: string): string {
57 return refName.trim().replace(/^refs\/heads\//, "");
58}
59
60function normalizeAzureDevOpsPullRequestState(status: string): "open" | "closed" | "merged" {
61 switch (status.trim().toLowerCase()) {
62 case "completed":
63 return "merged";
64 case "abandoned":
65 return "closed";
66 default:
67 return "open";
68 }
69}
70
71function encodeAzureDevOpsPathSegment(segment: string): string {
72 return encodeURIComponent(segment);
73}
74
75function azureDevOpsOrganizationBaseFromRestApiUrl(
76 value: string | null | undefined,
77): string | null {
78 const rawUrl = trimOptionalString(value);
79 if (!rawUrl) {
80 return null;
81 }
82
83 try {
84 const url = new URL(rawUrl);
85 const hostname = url.hostname.toLowerCase();
86 const pathSegments = url.pathname.split("/").filter((segment) => segment.length > 0);
87 const isAzureRestUrl = pathSegments.some((segment) => segment.toLowerCase() === "_apis");
88 if (!isAzureRestUrl) {
89 return null;
90 }
91
92 if (hostname === "dev.azure.com") {
93 const organization = pathSegments[0];
94 return organization ? `${url.origin}/${organization}` : null;
95 }
96
97 if (hostname.endsWith(".visualstudio.com")) {
98 return url.origin;
99 }
100
101 return null;
102 } catch {
103 return null;
104 }
105}
106
107function normalizeAzureDevOpsPullRequestUrl(
108 raw: Schema.Schema.Type<typeof AzureDevOpsPullRequestSchema>,
109): string {
110 const webLink = trimOptionalString(raw._links?.web?.href);
111 if (webLink) {
112 return webLink;
113 }
114
115 const repositoryWebUrl = trimOptionalString(raw.repository?.webUrl);
116 if (repositoryWebUrl) {
117 return `${repositoryWebUrl.replace(/\/+$/, "")}/pullrequest/${raw.pullRequestId}`;
118 }
119
120 const organizationBase = azureDevOpsOrganizationBaseFromRestApiUrl(raw.url);
121 const projectName = trimOptionalString(raw.repository?.project?.name);
122 const repositoryName = trimOptionalString(raw.repository?.name);
123 if (organizationBase && projectName && repositoryName) {
124 const encodedProjectName = encodeAzureDevOpsPathSegment(projectName);
125 const encodedRepositoryName = encodeAzureDevOpsPathSegment(repositoryName);
126 return `${organizationBase}/${encodedProjectName}/_git/${encodedRepositoryName}/pullrequest/${raw.pullRequestId}`;
127 }
128
129 return trimOptionalString(raw.url) ?? "";
130}
131
132function normalizeAzureDevOpsPullRequestRecord(
133 raw: Schema.Schema.Type<typeof AzureDevOpsPullRequestSchema>,
134): NormalizedAzureDevOpsPullRequestRecord {
135 return {
136 number: raw.pullRequestId,
137 title: raw.title,
138 url: normalizeAzureDevOpsPullRequestUrl(raw),
139 baseRefName: normalizeRefName(raw.targetRefName),
140 headRefName: normalizeRefName(raw.sourceRefName),
141 state: normalizeAzureDevOpsPullRequestState(raw.status),
142 updatedAt: (raw.closedDate ?? Option.none()).pipe(
143 Option.orElse(() => raw.creationDate ?? Option.none()),
144 ),
145 };
146}
147
148const decodeAzureDevOpsPullRequestList = decodeJsonResult(Schema.Array(Schema.Unknown));
149const decodeAzureDevOpsPullRequest = decodeJsonResult(AzureDevOpsPullRequestSchema);
150const decodeAzureDevOpsPullRequestEntry = Schema.decodeUnknownExit(AzureDevOpsPullRequestSchema);
151
152export const formatAzureDevOpsJsonDecodeError = formatSchemaError;
153
154export function decodeAzureDevOpsPullRequestListJson(
155 raw: string,
156): Result.Result<
157 ReadonlyArray<NormalizedAzureDevOpsPullRequestRecord>,
158 Cause.Cause<Schema.SchemaError>
159> {
160 const result = decodeAzureDevOpsPullRequestList(raw);
161 if (Result.isSuccess(result)) {
162 const pullRequests: NormalizedAzureDevOpsPullRequestRecord[] = [];
163 for (const entry of result.success) {
164 const decodedEntry = decodeAzureDevOpsPullRequestEntry(entry);
165 if (Exit.isFailure(decodedEntry)) {
166 continue;
167 }
168 pullRequests.push(normalizeAzureDevOpsPullRequestRecord(decodedEntry.value));
169 }
170 return Result.succeed(pullRequests);
171 }
172 return Result.fail(result.failure);
173}
174
175export function decodeAzureDevOpsPullRequestJson(
176 raw: string,
177): Result.Result<NormalizedAzureDevOpsPullRequestRecord, Cause.Cause<Schema.SchemaError>> {
178 const result = decodeAzureDevOpsPullRequest(raw);
179 if (Result.isSuccess(result)) {
180 return Result.succeed(normalizeAzureDevOpsPullRequestRecord(result.success));
181 }
182 return Result.fail(result.failure);
183}