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

gitHubPullRequests.ts

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