Open History to see the last commit
1import { Cause, DateTime, Exit, Option, Result, Schema } from "effect";
2import { PositiveInt, TrimmedNonEmptyString } from "@t3tools/contracts";
3import { decodeJsonResult, formatSchemaError } from "@t3tools/shared/schemaJson";
4
5export interface NormalizedGitHubPullRequestRecord {
6 readonly number: number;
7 readonly title: string;
8 readonly url: string;
9 readonly baseRefName: string;
10 readonly headRefName: string;
11 readonly state: "open" | "closed" | "merged";
12 readonly updatedAt: Option.Option<DateTime.Utc>;
13 readonly isCrossRepository?: boolean;
14 readonly headRepositoryNameWithOwner?: string | null;
15 readonly headRepositoryOwnerLogin?: string | null;
16}
17
18const GitHubPullRequestSchema = Schema.Struct({
19 number: PositiveInt,
20 title: TrimmedNonEmptyString,
21 url: TrimmedNonEmptyString,
22 baseRefName: TrimmedNonEmptyString,
23 headRefName: TrimmedNonEmptyString,
24 state: Schema.optional(Schema.NullOr(Schema.String)),
25 mergedAt: Schema.optional(Schema.NullOr(Schema.String)),
26 updatedAt: Schema.optional(Schema.OptionFromNullOr(Schema.DateTimeUtcFromString)),
27 isCrossRepository: Schema.optional(Schema.Boolean),
28 headRepository: Schema.optional(
29 Schema.NullOr(
30 Schema.Struct({
31 nameWithOwner: Schema.String,
32 }),
33 ),
34 ),
35 headRepositoryOwner: Schema.optional(
36 Schema.NullOr(
37 Schema.Struct({
38 login: Schema.String,
39 }),
40 ),
41 ),
42});
43
44function trimOptionalString(value: string | null | undefined): string | null {
45 const trimmed = value?.trim() ?? "";
46 return trimmed.length > 0 ? trimmed : null;
47}
48
49function normalizeGitHubPullRequestState(input: {
50 state?: string | null | undefined;
51 mergedAt?: string | null | undefined;
52}): "open" | "closed" | "merged" {
53 const normalizedState = input.state?.trim().toUpperCase();
54 if (
55 (typeof input.mergedAt === "string" && input.mergedAt.trim().length > 0) ||
56 normalizedState === "MERGED"
57 ) {
58 return "merged";
59 }
60 if (normalizedState === "CLOSED") {
61 return "closed";
62 }
63 return "open";
64}
65
66function normalizeGitHubPullRequestRecord(
67 raw: Schema.Schema.Type<typeof GitHubPullRequestSchema>,
68): NormalizedGitHubPullRequestRecord {
69 const headRepositoryNameWithOwner = trimOptionalString(raw.headRepository?.nameWithOwner);
70 const headRepositoryOwnerLogin =
71 trimOptionalString(raw.headRepositoryOwner?.login) ??
72 (typeof headRepositoryNameWithOwner === "string" && headRepositoryNameWithOwner.includes("/")
73 ? (headRepositoryNameWithOwner.split("/")[0] ?? null)
74 : null);
75
76 return {
77 number: raw.number,
78 title: raw.title,
79 url: raw.url,
80 baseRefName: raw.baseRefName,
81 headRefName: raw.headRefName,
82 state: normalizeGitHubPullRequestState(raw),
83 updatedAt: raw.updatedAt ?? Option.none(),
84 ...(typeof raw.isCrossRepository === "boolean"
85 ? { isCrossRepository: raw.isCrossRepository }
86 : {}),
87 ...(headRepositoryNameWithOwner ? { headRepositoryNameWithOwner } : {}),
88 ...(headRepositoryOwnerLogin ? { headRepositoryOwnerLogin } : {}),
89 };
90}
91
92const decodeGitHubPullRequestList = decodeJsonResult(Schema.Array(Schema.Unknown));
93const decodeGitHubPullRequest = decodeJsonResult(GitHubPullRequestSchema);
94const decodeGitHubPullRequestEntry = Schema.decodeUnknownExit(GitHubPullRequestSchema);
95
96export const formatGitHubJsonDecodeError = formatSchemaError;
97
98export function decodeGitHubPullRequestListJson(
99 raw: string,
100): Result.Result<
101 ReadonlyArray<NormalizedGitHubPullRequestRecord>,
102 Cause.Cause<Schema.SchemaError>
103> {
104 const result = decodeGitHubPullRequestList(raw);
105 if (Result.isSuccess(result)) {
106 const pullRequests: NormalizedGitHubPullRequestRecord[] = [];
107 for (const entry of result.success) {
108 const decodedEntry = decodeGitHubPullRequestEntry(entry);
109 if (Exit.isFailure(decodedEntry)) {
110 continue;
111 }
112 pullRequests.push(normalizeGitHubPullRequestRecord(decodedEntry.value));
113 }
114 return Result.succeed(pullRequests);
115 }
116 return Result.fail(result.failure);
117}
118
119export function decodeGitHubPullRequestJson(
120 raw: string,
121): Result.Result<NormalizedGitHubPullRequestRecord, Cause.Cause<Schema.SchemaError>> {
122 const result = decodeGitHubPullRequest(raw);
123 if (Result.isSuccess(result)) {
124 return Result.succeed(normalizeGitHubPullRequestRecord(result.success));
125 }
126 return Result.fail(result.failure);
127}