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

gitHubPullRequests.ts

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