feat(server): pull requests talk to GitHub's API instead of the gh CLI (#16320)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
1 parent
1564aaa5d2
commit
adc3c9327a
15 files changed
+2871
-2597
No files matched your search
@@ -1,32 +1,63 @@
|
||||
import * as Clock from "effect/Clock";
|
||||
import * as Cache from "effect/Cache";
|
||||
import * as Context from "effect/Context";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Option from "effect/Option";
|
||||
import * as Schema from "effect/Schema";
|
||||
import * as Semaphore from "effect/Semaphore";
|
||||
import { PositiveInt, type PullRequestChecks } from "@t3tools/contracts";
|
||||
|
||||
import * as GitHubCli from "../sourceControl/GitHubCli.ts";
|
||||
import * as GitHubApi from "../sourceControl/GitHubApi.ts";
|
||||
import type { GitHubPullRequestDetail } from "./gitHubPullRequestJson.ts";
|
||||
import type { GitHubPullRequestCliError } from "./GitHubPullRequestCli.ts";
|
||||
import type { ProviderRepositoryRef } from "./PullRequestProvider.ts";
|
||||
|
||||
const decodeHeadSchema = Schema.Struct({
|
||||
head: Schema.Struct({ sha: Schema.String.check(Schema.isPattern(/^[a-f0-9]{40,64}$/)) }),
|
||||
const HeadSchema = Schema.Struct({
|
||||
head: Schema.Struct({
|
||||
sha: Schema.String.check(Schema.isPattern(/^[a-f0-9]{40,64}$/)),
|
||||
// Null once the fork a pull request came from has been deleted.
|
||||
repo: Schema.NullOr(Schema.Struct({ id: PositiveInt })),
|
||||
}),
|
||||
base: Schema.Struct({ repo: Schema.Struct({ id: PositiveInt }) }),
|
||||
headRepositoryId: Schema.NullOr(PositiveInt),
|
||||
});
|
||||
const decodeHead = Schema.decodeUnknownEffect(Schema.fromJsonString(decodeHeadSchema));
|
||||
const decodeHead = Schema.decodeUnknownEffect(Schema.fromJsonString(HeadSchema));
|
||||
|
||||
type Validator = { etag: string | undefined; next: boolean };
|
||||
const WorkflowRunSchema = Schema.Struct({
|
||||
id: Schema.Int,
|
||||
name: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
html_url: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
status: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
conclusion: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
head_branch: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
});
|
||||
const decodeWorkflowRuns = Schema.decodeUnknownOption(
|
||||
Schema.fromJsonString(Schema.Struct({ workflow_runs: Schema.Array(WorkflowRunSchema) })),
|
||||
);
|
||||
|
||||
export type KnownWorkflowRun = typeof WorkflowRunSchema.Type;
|
||||
|
||||
/**
|
||||
* Every `pull_request` workflow run of one head, as the revalidator just confirmed them against
|
||||
* GitHub. A read running under it filters these rather than asking for the runs again.
|
||||
*/
|
||||
export const KnownWorkflowRuns = Context.Reference<{
|
||||
readonly headSha: string;
|
||||
readonly runs: ReadonlyArray<KnownWorkflowRun>;
|
||||
} | null>("t3/pullRequest/KnownWorkflowRuns", { defaultValue: () => null });
|
||||
|
||||
type Validator = { etag: string | undefined; next: boolean; body: string };
|
||||
|
||||
/** A host that answers these with one of them has no conditional REST for it at all. */
|
||||
const UNSUPPORTED_STATUSES = new Set([404, 405, 501]);
|
||||
|
||||
export const makeChecksRevalidator = Effect.gen(function* () {
|
||||
const github = yield* GitHubCli.GitHubCli;
|
||||
const api = yield* GitHubApi.GitHubApi;
|
||||
const entries = yield* Cache.makeWith(
|
||||
(_key: string) =>
|
||||
Effect.sync(() => ({
|
||||
gate: Semaphore.makeUnsafe(1),
|
||||
validators: new Map<string, Validator>(),
|
||||
head: null as Schema.Schema.Type<typeof decodeHeadSchema> | null,
|
||||
head: null as typeof HeadSchema.Type | null,
|
||||
value: null as PullRequestChecks | null,
|
||||
readAt: 0,
|
||||
supported: true,
|
||||
@@ -43,71 +74,64 @@ export const makeChecksRevalidator = Effect.gen(function* () {
|
||||
>,
|
||||
) =>
|
||||
Effect.gen(function* () {
|
||||
const credential = yield* GitHubCli.PinnedGitHubCredential;
|
||||
const credential = yield* GitHubApi.PinnedGitHubCredential;
|
||||
if (credential === null) return yield* read;
|
||||
const key = `${credential.credentialFingerprint}\0${input.host}\0${input.repository}\0${input.number}`;
|
||||
const entry = yield* Cache.get(entries, key);
|
||||
return yield* entry.gate.withPermit(
|
||||
Effect.gen(function* () {
|
||||
if (!entry.supported) return yield* read;
|
||||
const fail = () =>
|
||||
new GitHubCli.GitHubCliCommandError({
|
||||
command: "gh",
|
||||
cwd: input.cwd,
|
||||
cause: new Error("GitHub returned an invalid conditional checks response."),
|
||||
const fail = (status: number) =>
|
||||
new GitHubApi.GitHubApiResponseError({
|
||||
host: input.host,
|
||||
operation: "revalidateChecks",
|
||||
status,
|
||||
});
|
||||
/** Whether the endpoint has a next page, or null where the host cannot say. */
|
||||
const get = (endpoint: string, head = false) =>
|
||||
Effect.gen(function* () {
|
||||
const previous = entry.validators.get(endpoint);
|
||||
const result = yield* github
|
||||
.execute({
|
||||
cwd: input.cwd,
|
||||
args: [
|
||||
"api",
|
||||
endpoint,
|
||||
"--hostname",
|
||||
input.host,
|
||||
"--include",
|
||||
...(head
|
||||
? [
|
||||
"--jq",
|
||||
"{head:{sha:.head.sha},base:{repo:{id:.base.repo.id}},headRepositoryId:.head.repo.id}",
|
||||
]
|
||||
: ["--silent"]),
|
||||
...(previous?.etag ? ["-H", `If-None-Match: ${previous.etag}`] : []),
|
||||
],
|
||||
acceptNotModified: true,
|
||||
const response = yield* api
|
||||
.rest({
|
||||
host: input.host,
|
||||
operation: "revalidateChecks",
|
||||
path: endpoint,
|
||||
...(previous?.etag ? { ifNoneMatch: previous.etag } : {}),
|
||||
})
|
||||
.pipe(
|
||||
Effect.catchTags({
|
||||
GitHubCliCommandError: (error) =>
|
||||
GitHubApiNotFoundError: () =>
|
||||
Effect.sync(() => {
|
||||
entry.supported = ![404, 405, 501].includes(error.httpStatus ?? 0);
|
||||
entry.supported = false;
|
||||
return null;
|
||||
}),
|
||||
GitHubApiResponseError: (error) =>
|
||||
Effect.sync(() => {
|
||||
entry.supported = !UNSUPPORTED_STATUSES.has(error.status);
|
||||
return null;
|
||||
}),
|
||||
}),
|
||||
);
|
||||
if (result === null) {
|
||||
if (response === null) {
|
||||
entry.value = null;
|
||||
return null;
|
||||
}
|
||||
if (result.stdoutTruncated || result.stdoutInvalidUtf8) return yield* fail();
|
||||
const split = result.stdout.search(/\r?\n\r?\n/);
|
||||
const headers = split < 0 ? result.stdout : result.stdout.slice(0, split);
|
||||
const status = /^HTTP\/\S+ (\d+)/.exec(headers)?.[1];
|
||||
if (status === "304" && previous) return previous.next;
|
||||
if (status !== "200") return yield* fail();
|
||||
if (response.status === 304 && previous) return previous.next;
|
||||
if (response.status !== 200 || response.truncated) {
|
||||
return yield* fail(response.status);
|
||||
}
|
||||
entry.value = null;
|
||||
if (head) {
|
||||
const decoded = yield* decodeHead(result.stdout.slice(split).trim()).pipe(
|
||||
Effect.mapError(fail),
|
||||
const decoded = yield* decodeHead(response.body).pipe(
|
||||
Effect.mapError(() => fail(response.status)),
|
||||
);
|
||||
if (entry.head?.head.sha !== decoded.head.sha) entry.validators.clear();
|
||||
entry.head = decoded;
|
||||
}
|
||||
const validator = {
|
||||
etag: /^etag:\s*(.+)$/im.exec(headers)?.[1]?.trim(),
|
||||
next: /^link:.*rel="next"/im.test(headers),
|
||||
etag: response.headers["etag"]?.trim(),
|
||||
next: /rel="next"/.test(response.headers["link"] ?? ""),
|
||||
body: response.body,
|
||||
};
|
||||
entry.validators.set(endpoint, validator);
|
||||
return validator.next;
|
||||
@@ -115,27 +139,44 @@ export const makeChecksRevalidator = Effect.gen(function* () {
|
||||
const root = `repos/${input.repository}`;
|
||||
if ((yield* get(`${root}/pulls/${input.number}`, true)) === null) return yield* read;
|
||||
const head = entry.head;
|
||||
if (head === null) return yield* fail();
|
||||
if (head === null) return yield* fail(200);
|
||||
const runsEndpoint =
|
||||
head.head.repo?.id !== head.base.repo.id
|
||||
? `${root}/actions/runs?head_sha=${head.head.sha}&event=pull_request`
|
||||
: null;
|
||||
const endpoints = [
|
||||
`${root}/commits/${head.head.sha}/check-runs?filter=all`,
|
||||
`${root}/commits/${head.head.sha}/status`,
|
||||
...(head.headRepositoryId !== head.base.repo.id
|
||||
? [`${root}/actions/runs?head_sha=${head.head.sha}&event=pull_request`]
|
||||
: []),
|
||||
...(runsEndpoint === null ? [] : [runsEndpoint]),
|
||||
];
|
||||
const runPages: Array<string> = [];
|
||||
for (const endpoint of endpoints) {
|
||||
for (let page = 1; ; page++) {
|
||||
if (page > 100) return yield* fail();
|
||||
const next = yield* get(
|
||||
`${endpoint}${endpoint.includes("?") ? "&" : "?"}per_page=100&page=${page}`,
|
||||
);
|
||||
if (page > 100) return yield* fail(200);
|
||||
const paged = `${endpoint}${endpoint.includes("?") ? "&" : "?"}per_page=100&page=${page}`;
|
||||
const next = yield* get(paged);
|
||||
if (next === null) return yield* read;
|
||||
if (endpoint === runsEndpoint) runPages.push(paged);
|
||||
if (!next) break;
|
||||
}
|
||||
}
|
||||
const now = yield* Clock.currentTimeMillis;
|
||||
if (entry.value !== null && now - entry.readAt < 5 * 60_000) return entry.value;
|
||||
const fresh = yield* read;
|
||||
// Every page of the head's runs was just confirmed current, so the read filters them
|
||||
// rather than listing the runs a second time.
|
||||
const pages = runPages.map((endpoint) =>
|
||||
decodeWorkflowRuns(entry.validators.get(endpoint)?.body ?? ""),
|
||||
);
|
||||
const known =
|
||||
runsEndpoint !== null && pages.every(Option.isSome)
|
||||
? {
|
||||
headSha: head.head.sha,
|
||||
runs: pages.flatMap((page) =>
|
||||
Option.isSome(page) ? page.value.workflow_runs : [],
|
||||
),
|
||||
}
|
||||
: null;
|
||||
const fresh = yield* read.pipe(Effect.provideService(KnownWorkflowRuns, known));
|
||||
const value = {
|
||||
state: fresh.state,
|
||||
checks: fresh.checks,
|
||||
|
||||
Reference in new issue
Block a user