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

gitHubPullRequests.ts

134 lines4.8 KB

1import { trimOptionalString } from "@t3tools/source-control-core/server/pullRequestUtils";
2import * as Cause from "effect/Cause";
3import * as DateTime from "effect/DateTime";
4import * as Exit from "effect/Exit";
5import * as Option from "effect/Option";
6import * as Result from "effect/Result";
7import * as Schema from "effect/Schema";
8import { PositiveInt, TrimmedNonEmptyString } from "@t3tools/contracts";
9import { decodeJsonResult } from "@t3tools/shared/schemaJson";
10​
11export interface NormalizedGitHubPullRequestRecord {
12 readonly number: number;
13 readonly title: string;
14 readonly url: string;
15 readonly baseRefName: string;
16 readonly headRefName: string;
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 omits headRepository.nameWithOwner.
42 headRepository: Schema.optional(
43 Schema.NullOr(
44 Schema.Struct({
45 nameWithOwner: Schema.optional(Schema.NullOr(Schema.String)),
46 name: Schema.optional(Schema.NullOr(Schema.String)),
47 }),
48 ),
49 ),
50 headRepositoryOwner: Schema.optional(
51 Schema.NullOr(
52 Schema.Struct({
53 login: Schema.optional(Schema.NullOr(Schema.String)),
54 }),
55 ),
56 ),
57});
58​
59function normalizeGitHubPullRequestState(input: {
60 state?: string | null | undefined;
61 mergedAt?: string | null | undefined;
62}): "open" | "closed" | "merged" {
63 const normalizedState = input.state?.trim().toUpperCase();
64 if (
65 (typeof input.mergedAt === "string" && input.mergedAt.trim().length > 0) ||
66 normalizedState === "MERGED"
67 ) {
68 return "merged";
69 }
70 if (normalizedState === "CLOSED") {
71 return "closed";
72 }
73 return "open";
75​
76export function normalizeGitHubPullRequestRecord(
77 raw: Schema.Schema.Type<typeof GitHubPullRequestSchema>,
78): NormalizedGitHubPullRequestRecord {
79 const explicitNameWithOwner = trimOptionalString(raw.headRepository?.nameWithOwner);
80 const headRepositoryName = trimOptionalString(raw.headRepository?.name);
81 const headRepositoryOwnerLogin =
82 trimOptionalString(raw.headRepositoryOwner?.login) ??
83 (explicitNameWithOwner?.includes("/") ? (explicitNameWithOwner.split("/")[0] ?? null) : null);
84 const headRepositoryNameWithOwner =
85 explicitNameWithOwner ??
86 (headRepositoryOwnerLogin && headRepositoryName
87 ? `${headRepositoryOwnerLogin}/${headRepositoryName}`
88 : null);
89 const headSha = trimOptionalString(raw.headRefOid);
90​
91 return {
92 number: raw.number,
93 title: raw.title,
94 url: raw.url,
95 baseRefName: raw.baseRefName,
96 headRefName: raw.headRefName,
97 ...(headSha ? { headSha } : {}),
98 state: normalizeGitHubPullRequestState(raw),
99 ...(raw.isDraft === true ? { isDraft: true } : {}),
100 closedAt: raw.closedAt ?? null,
101 mergedAt: raw.mergedAt ?? null,
102 updatedAt: raw.updatedAt ?? Option.none(),
103 ...(typeof raw.isCrossRepository === "boolean"
104 ? { isCrossRepository: raw.isCrossRepository }
105 : {}),
106 ...(headRepositoryNameWithOwner ? { headRepositoryNameWithOwner } : {}),
107 ...(headRepositoryOwnerLogin ? { headRepositoryOwnerLogin } : {}),
108 };
110​
111const decodeGitHubPullRequestList = decodeJsonResult(Schema.Array(Schema.Unknown));
112const decodeGitHubPullRequestEntry = Schema.decodeUnknownExit(GitHubPullRequestSchema);
113​
114function decodeGitHubPullRequestEntries(
115 entries: ReadonlyArray<unknown>,
116): ReadonlyArray<NormalizedGitHubPullRequestRecord> {
117 const pullRequests: NormalizedGitHubPullRequestRecord[] = [];
118 for (const entry of entries) {
119 const decodedEntry = decodeGitHubPullRequestEntry(entry);
120 if (Exit.isSuccess(decodedEntry)) {
121 pullRequests.push(normalizeGitHubPullRequestRecord(decodedEntry.value));
122 }
123 }
124 return pullRequests;
126​
127export function decodeGitHubPullRequestListJson(
128 raw: string,
129): Result.Result<
130 ReadonlyArray<NormalizedGitHubPullRequestRecord>,
131 Cause.Cause<Schema.SchemaError>
132> {
133 return Result.map(decodeGitHubPullRequestList(raw), decodeGitHubPullRequestEntries);