Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

gitHubPullRequests.ts

151 lines5.4 KB

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