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

gitHubPullRequests.ts

145 lines5.3 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 headSha?: string;
17 readonly state: "open" | "closed" | "merged";
18 readonly isDraft?: boolean;
19 readonly closedAt?: string | null;
20 readonly mergedAt?: string | null;
21 readonly updatedAt: Option.Option<DateTime.Utc>;
22 readonly isCrossRepository?: boolean;
23 readonly headRepositoryNameWithOwner?: string | null;
24 readonly headRepositoryOwnerLogin?: 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​
82/** A pull request row, in `gh --json` or GraphQL node shape, as every read reports it. */
83export function normalizeGitHubPullRequestRecord(
84 raw: Schema.Schema.Type<typeof GitHubPullRequestSchema>,
85): NormalizedGitHubPullRequestRecord {
86 const explicitNameWithOwner = trimOptionalString(raw.headRepository?.nameWithOwner);
87 const headRepositoryName = trimOptionalString(raw.headRepository?.name);
88 const headRepositoryOwnerLogin =
89 trimOptionalString(raw.headRepositoryOwner?.login) ??
90 (explicitNameWithOwner?.includes("/") ? (explicitNameWithOwner.split("/")[0] ?? null) : null);
91 const headRepositoryNameWithOwner =
92 explicitNameWithOwner ??
93 (headRepositoryOwnerLogin && headRepositoryName
94 ? `${headRepositoryOwnerLogin}/${headRepositoryName}`
95 : null);
96 const headSha = trimOptionalString(raw.headRefOid);
97​
98 return {
99 number: raw.number,
100 title: raw.title,
101 url: raw.url,
102 baseRefName: raw.baseRefName,
103 headRefName: raw.headRefName,
104 ...(headSha ? { headSha } : {}),
105 state: normalizeGitHubPullRequestState(raw),
106 ...(raw.isDraft === true ? { isDraft: true } : {}),
107 closedAt: raw.closedAt ?? null,
108 mergedAt: raw.mergedAt ?? null,
109 updatedAt: raw.updatedAt ?? Option.none(),
110 ...(typeof raw.isCrossRepository === "boolean"
111 ? { isCrossRepository: raw.isCrossRepository }
112 : {}),
113 ...(headRepositoryNameWithOwner ? { headRepositoryNameWithOwner } : {}),
114 ...(headRepositoryOwnerLogin ? { headRepositoryOwnerLogin } : {}),
115 };
117​
118const decodeGitHubPullRequestList = decodeJsonResult(Schema.Array(Schema.Unknown));
119const decodeGitHubPullRequestEntry = Schema.decodeUnknownExit(GitHubPullRequestSchema);
120​
121/**
122 * Pull request rows in `gh --json` or GraphQL node shape. A row that does not decode is
123 * skipped, so one malformed pull request cannot hide the rest.
124 */
125function decodeGitHubPullRequestEntries(
126 entries: ReadonlyArray<unknown>,
127): ReadonlyArray<NormalizedGitHubPullRequestRecord> {
128 const pullRequests: NormalizedGitHubPullRequestRecord[] = [];
129 for (const entry of entries) {
130 const decodedEntry = decodeGitHubPullRequestEntry(entry);
131 if (Exit.isSuccess(decodedEntry)) {
132 pullRequests.push(normalizeGitHubPullRequestRecord(decodedEntry.value));
133 }
134 }
135 return pullRequests;
137​
138export function decodeGitHubPullRequestListJson(
139 raw: string,
140): Result.Result<
141 ReadonlyArray<NormalizedGitHubPullRequestRecord>,
142 Cause.Cause<Schema.SchemaError>
143> {
144 return Result.map(decodeGitHubPullRequestList(raw), decodeGitHubPullRequestEntries);