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 NormalizedGitHubPullRequestRecord {
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 GitHubPullRequestSchema = Schema.Struct({
24 number: PositiveInt,
25 title: TrimmedNonEmptyString,
26 url: TrimmedNonEmptyString,
27 baseRefName: TrimmedNonEmptyString,
28 headRefName: TrimmedNonEmptyString,
29 state: Schema.optional(Schema.NullOr(Schema.String)),
30 mergedAt: Schema.optional(Schema.NullOr(Schema.String)),
31 updatedAt: Schema.optional(Schema.OptionFromNullOr(Schema.DateTimeUtcFromString)),
32 isCrossRepository: Schema.optional(Schema.Boolean),
33 headRepository: Schema.optional(
34 Schema.NullOr(
35 Schema.Struct({
36 nameWithOwner: Schema.String,
37 }),
38 ),
39 ),
40 headRepositoryOwner: Schema.optional(
41 Schema.NullOr(
42 Schema.Struct({
43 login: Schema.String,
44 }),
45 ),
46 ),
47});
48
49function trimOptionalString(value: string | null | undefined): string | null {
50 const trimmed = value?.trim() ?? "";
51 return trimmed.length > 0 ? trimmed : null;
52}
53
54function normalizeGitHubPullRequestState(input: {
55 state?: string | null | undefined;
56 mergedAt?: string | null | undefined;
57}): "open" | "closed" | "merged" {
58 const normalizedState = input.state?.trim().toUpperCase();
59 if (
60 (typeof input.mergedAt === "string" && input.mergedAt.trim().length > 0) ||
61 normalizedState === "MERGED"
62 ) {
63 return "merged";
64 }
65 if (normalizedState === "CLOSED") {
66 return "closed";
67 }
68 return "open";
69}
70
71function normalizeGitHubPullRequestRecord(
72 raw: Schema.Schema.Type<typeof GitHubPullRequestSchema>,
73): NormalizedGitHubPullRequestRecord {
74 const headRepositoryNameWithOwner = trimOptionalString(raw.headRepository?.nameWithOwner);
75 const headRepositoryOwnerLogin =
76 trimOptionalString(raw.headRepositoryOwner?.login) ??
77 (typeof headRepositoryNameWithOwner === "string" && headRepositoryNameWithOwner.includes("/")
78 ? (headRepositoryNameWithOwner.split("/")[0] ?? null)
79 : null);
80
81 return {
82 number: raw.number,
83 title: raw.title,
84 url: raw.url,
85 baseRefName: raw.baseRefName,
86 headRefName: raw.headRefName,
87 state: normalizeGitHubPullRequestState(raw),
88 updatedAt: raw.updatedAt ?? Option.none(),
89 ...(typeof raw.isCrossRepository === "boolean"
90 ? { isCrossRepository: raw.isCrossRepository }
91 : {}),
92 ...(headRepositoryNameWithOwner ? { headRepositoryNameWithOwner } : {}),
93 ...(headRepositoryOwnerLogin ? { headRepositoryOwnerLogin } : {}),
94 };
95}
96
97const decodeGitHubPullRequestList = decodeJsonResult(Schema.Array(Schema.Unknown));
98const decodeGitHubPullRequest = decodeJsonResult(GitHubPullRequestSchema);
99const decodeGitHubPullRequestEntry = Schema.decodeUnknownExit(GitHubPullRequestSchema);
100
101export const formatGitHubJsonDecodeError = formatSchemaError;
102
103export function decodeGitHubPullRequestListJson(
104 raw: string,
105): Result.Result<
106 ReadonlyArray<NormalizedGitHubPullRequestRecord>,
107 Cause.Cause<Schema.SchemaError>
108> {
109 const result = decodeGitHubPullRequestList(raw);
110 if (Result.isSuccess(result)) {
111 const pullRequests: NormalizedGitHubPullRequestRecord[] = [];
112 for (const entry of result.success) {
113 const decodedEntry = decodeGitHubPullRequestEntry(entry);
114 if (Exit.isFailure(decodedEntry)) {
115 continue;
116 }
117 pullRequests.push(normalizeGitHubPullRequestRecord(decodedEntry.value));
118 }
119 return Result.succeed(pullRequests);
120 }
121 return Result.fail(result.failure);
122}
123
124export function decodeGitHubPullRequestJson(
125 raw: string,
126): Result.Result<NormalizedGitHubPullRequestRecord, Cause.Cause<Schema.SchemaError>> {
127 const result = decodeGitHubPullRequest(raw);
128 if (Result.isSuccess(result)) {
129 return Result.succeed(normalizeGitHubPullRequestRecord(result.success));
130 }
131 return Result.fail(result.failure);
132}