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

gitHubPullRequests.ts

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