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 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 // gh < 2.47 exports headRepository as {id, name} only; nameWithOwner was
34 // added later. Both fields stay optional so a version-drifted gh CLI can
35 // never fail the decode and silently drop the PR from the list.
36 headRepository: Schema.optional(
37 Schema.NullOr(
38 Schema.Struct({
39 nameWithOwner: Schema.optional(Schema.NullOr(Schema.String)),
40 name: Schema.optional(Schema.NullOr(Schema.String)),
41 }),
42 ),
43 ),
44 headRepositoryOwner: Schema.optional(
45 Schema.NullOr(
46 Schema.Struct({
47 login: Schema.optional(Schema.NullOr(Schema.String)),
48 }),
49 ),
50 ),
51});
52
53function trimOptionalString(value: string | null | undefined): string | null {
54 const trimmed = value?.trim() ?? "";
55 return trimmed.length > 0 ? trimmed : null;
56}
57
58function normalizeGitHubPullRequestState(input: {
59 state?: string | null | undefined;
60 mergedAt?: string | null | undefined;
61}): "open" | "closed" | "merged" {
62 const normalizedState = input.state?.trim().toUpperCase();
63 if (
64 (typeof input.mergedAt === "string" && input.mergedAt.trim().length > 0) ||
65 normalizedState === "MERGED"
66 ) {
67 return "merged";
68 }
69 if (normalizedState === "CLOSED") {
70 return "closed";
71 }
72 return "open";
73}
74
75function normalizeGitHubPullRequestRecord(
76 raw: Schema.Schema.Type<typeof GitHubPullRequestSchema>,
77): NormalizedGitHubPullRequestRecord {
78 const explicitNameWithOwner = trimOptionalString(raw.headRepository?.nameWithOwner);
79 const headRepositoryName = trimOptionalString(raw.headRepository?.name);
80 const headRepositoryOwnerLogin =
81 trimOptionalString(raw.headRepositoryOwner?.login) ??
82 (explicitNameWithOwner?.includes("/") ? (explicitNameWithOwner.split("/")[0] ?? null) : null);
83 const headRepositoryNameWithOwner =
84 explicitNameWithOwner ??
85 (headRepositoryOwnerLogin && headRepositoryName
86 ? `${headRepositoryOwnerLogin}/${headRepositoryName}`
87 : null);
88
89 return {
90 number: raw.number,
91 title: raw.title,
92 url: raw.url,
93 baseRefName: raw.baseRefName,
94 headRefName: raw.headRefName,
95 state: normalizeGitHubPullRequestState(raw),
96 updatedAt: raw.updatedAt ?? Option.none(),
97 ...(typeof raw.isCrossRepository === "boolean"
98 ? { isCrossRepository: raw.isCrossRepository }
99 : {}),
100 ...(headRepositoryNameWithOwner ? { headRepositoryNameWithOwner } : {}),
101 ...(headRepositoryOwnerLogin ? { headRepositoryOwnerLogin } : {}),
102 };
103}
104
105const decodeGitHubPullRequestList = decodeJsonResult(Schema.Array(Schema.Unknown));
106const decodeGitHubPullRequest = decodeJsonResult(GitHubPullRequestSchema);
107const decodeGitHubPullRequestEntry = Schema.decodeUnknownExit(GitHubPullRequestSchema);
108
109export const formatGitHubJsonDecodeError = formatSchemaError;
110
111export function decodeGitHubPullRequestListJson(
112 raw: string,
113): Result.Result<
114 ReadonlyArray<NormalizedGitHubPullRequestRecord>,
115 Cause.Cause<Schema.SchemaError>
116> {
117 const result = decodeGitHubPullRequestList(raw);
118 if (Result.isSuccess(result)) {
119 const pullRequests: NormalizedGitHubPullRequestRecord[] = [];
120 for (const entry of result.success) {
121 const decodedEntry = decodeGitHubPullRequestEntry(entry);
122 if (Exit.isFailure(decodedEntry)) {
123 continue;
124 }
125 pullRequests.push(normalizeGitHubPullRequestRecord(decodedEntry.value));
126 }
127 return Result.succeed(pullRequests);
128 }
129 return Result.fail(result.failure);
130}
131
132export function decodeGitHubPullRequestJson(
133 raw: string,
134): Result.Result<NormalizedGitHubPullRequestRecord, Cause.Cause<Schema.SchemaError>> {
135 const result = decodeGitHubPullRequest(raw);
136 if (Result.isSuccess(result)) {
137 return Result.succeed(normalizeGitHubPullRequestRecord(result.success));
138 }
139 return Result.fail(result.failure);
140}