refactor(source-control): GitHub lives in @t3tools/source-control-github (#17607)
Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
8d1858d9d6
commit
507361bc73
51 files changed
+411
-195
No files matched your search
@@ -0,0 +1,145 @@
|
||||
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<DateTime.Utc>;
|
||||
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<typeof GitHubPullRequestSchema>,
|
||||
): 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<unknown>,
|
||||
): ReadonlyArray<NormalizedGitHubPullRequestRecord> {
|
||||
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<NormalizedGitHubPullRequestRecord>,
|
||||
Cause.Cause<Schema.SchemaError>
|
||||
> {
|
||||
return Result.map(decodeGitHubPullRequestList(raw), decodeGitHubPullRequestEntries);
|
||||
}
|
||||
Reference in new issue
Block a user