import * as Cause from "effect/Cause"; import * as DateTime from "effect/DateTime"; import * as Exit from "effect/Exit"; import * as Option from "effect/Option"; import * as Result from "effect/Result"; import * as Schema from "effect/Schema"; import { PositiveInt, TrimmedNonEmptyString } from "@t3tools/contracts"; import { decodeJsonResult } from "@t3tools/shared/schemaJson"; export interface NormalizedGitHubPullRequestRecord { readonly number: number; readonly title: string; readonly url: string; readonly baseRefName: string; readonly headRefName: string; /** The head commit, when the read asked for `headRefOid`. */ readonly headSha?: string; readonly state: "open" | "closed" | "merged"; readonly isDraft?: boolean; readonly closedAt?: string | null; readonly mergedAt?: string | null; readonly updatedAt: Option.Option; readonly isCrossRepository?: boolean; readonly headRepositoryNameWithOwner?: string | null; readonly headRepositoryOwnerLogin?: string | null; } const GitHubPullRequestSchema = Schema.Struct({ number: PositiveInt, title: TrimmedNonEmptyString, url: TrimmedNonEmptyString, baseRefName: TrimmedNonEmptyString, headRefName: TrimmedNonEmptyString, headRefOid: Schema.optional(Schema.NullOr(Schema.String)), state: Schema.optional(Schema.NullOr(Schema.String)), isDraft: Schema.optional(Schema.Boolean), closedAt: Schema.optional(Schema.NullOr(Schema.String)), mergedAt: Schema.optional(Schema.NullOr(Schema.String)), updatedAt: Schema.optional(Schema.OptionFromNullOr(Schema.DateTimeUtcFromString)), isCrossRepository: Schema.optional(Schema.Boolean), // gh < 2.47 exports headRepository as {id, name} only; nameWithOwner was // added later. Both fields stay optional so a version-drifted gh CLI can // never fail the decode and silently drop the PR from the list. headRepository: Schema.optional( Schema.NullOr( Schema.Struct({ nameWithOwner: Schema.optional(Schema.NullOr(Schema.String)), name: Schema.optional(Schema.NullOr(Schema.String)), }), ), ), headRepositoryOwner: Schema.optional( Schema.NullOr( Schema.Struct({ login: Schema.optional(Schema.NullOr(Schema.String)), }), ), ), }); function trimOptionalString(value: string | null | undefined): string | null { const trimmed = value?.trim() ?? ""; return trimmed.length > 0 ? trimmed : null; } function normalizeGitHubPullRequestState(input: { state?: string | null | undefined; mergedAt?: string | null | undefined; }): "open" | "closed" | "merged" { const normalizedState = input.state?.trim().toUpperCase(); if ( (typeof input.mergedAt === "string" && input.mergedAt.trim().length > 0) || normalizedState === "MERGED" ) { return "merged"; } if (normalizedState === "CLOSED") { return "closed"; } return "open"; } function normalizeGitHubPullRequestRecord( raw: Schema.Schema.Type, ): NormalizedGitHubPullRequestRecord { const explicitNameWithOwner = trimOptionalString(raw.headRepository?.nameWithOwner); const headRepositoryName = trimOptionalString(raw.headRepository?.name); const headRepositoryOwnerLogin = trimOptionalString(raw.headRepositoryOwner?.login) ?? (explicitNameWithOwner?.includes("/") ? (explicitNameWithOwner.split("/")[0] ?? null) : null); const headRepositoryNameWithOwner = explicitNameWithOwner ?? (headRepositoryOwnerLogin && headRepositoryName ? `${headRepositoryOwnerLogin}/${headRepositoryName}` : null); const headSha = trimOptionalString(raw.headRefOid); return { number: raw.number, title: raw.title, url: raw.url, baseRefName: raw.baseRefName, headRefName: raw.headRefName, ...(headSha ? { headSha } : {}), state: normalizeGitHubPullRequestState(raw), ...(raw.isDraft === true ? { isDraft: true } : {}), closedAt: raw.closedAt ?? null, mergedAt: raw.mergedAt ?? null, updatedAt: raw.updatedAt ?? Option.none(), ...(typeof raw.isCrossRepository === "boolean" ? { isCrossRepository: raw.isCrossRepository } : {}), ...(headRepositoryNameWithOwner ? { headRepositoryNameWithOwner } : {}), ...(headRepositoryOwnerLogin ? { headRepositoryOwnerLogin } : {}), }; } const decodeGitHubPullRequestList = decodeJsonResult(Schema.Array(Schema.Unknown)); const decodeGitHubPullRequestEntry = Schema.decodeUnknownExit(GitHubPullRequestSchema); /** * Pull request rows in `gh --json` or GraphQL node shape. A row that does not decode is * skipped, so one malformed pull request cannot hide the rest. */ export function decodeGitHubPullRequestEntries( entries: ReadonlyArray, ): ReadonlyArray { const pullRequests: NormalizedGitHubPullRequestRecord[] = []; for (const entry of entries) { const decodedEntry = decodeGitHubPullRequestEntry(entry); if (Exit.isSuccess(decodedEntry)) { pullRequests.push(normalizeGitHubPullRequestRecord(decodedEntry.value)); } } return pullRequests; } export function decodeGitHubPullRequestListJson( raw: string, ): Result.Result< ReadonlyArray, Cause.Cause > { return Result.map(decodeGitHubPullRequestList(raw), decodeGitHubPullRequestEntries); }