refactor(server): GitHub source control reads GitHubApi directly (#16982)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
1 parent
a21ac5fb15
commit
42d92ec597
21 files changed
+1759
-2304
No files matched your search
@@ -1,21 +1,42 @@
|
||||
import * as Schema from "effect/Schema";
|
||||
import * as DateTime from "effect/DateTime";
|
||||
import * as Context from "effect/Context";
|
||||
import * as Data from "effect/Data";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Exit from "effect/Exit";
|
||||
import * as FileSystem from "effect/FileSystem";
|
||||
import * as Option from "effect/Option";
|
||||
import * as Request from "effect/Request";
|
||||
import * as RequestResolver from "effect/RequestResolver";
|
||||
import * as Result from "effect/Result";
|
||||
import * as Schema from "effect/Schema";
|
||||
import {
|
||||
DEFAULT_SERVER_SETTINGS,
|
||||
SourceControlProviderError,
|
||||
TrimmedNonEmptyString,
|
||||
type ChangeRequest,
|
||||
type GitHubSettings,
|
||||
type SourceControlProviderDiscoveryItem,
|
||||
type SourceControlRepositoryCloneUrls,
|
||||
} from "@t3tools/contracts";
|
||||
|
||||
import { normalizeGitRemoteUrl } from "@t3tools/shared/git";
|
||||
import { HostProcessEnvironment } from "@t3tools/shared/hostProcess";
|
||||
import { decodeJsonResult } from "@t3tools/shared/schemaJson";
|
||||
import { isSshRemoteUrl } from "@t3tools/shared/sourceControl";
|
||||
|
||||
import * as ServerSettings from "../serverSettings.ts";
|
||||
import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
|
||||
import * as GitHubApi from "./GitHubApi.ts";
|
||||
import * as GitHubRepositoryApi from "./GitHubRepositoryApi.ts";
|
||||
import {
|
||||
decodeGitHubPullRequestEntries,
|
||||
type NormalizedGitHubPullRequestRecord,
|
||||
} from "./gitHubPullRequests.ts";
|
||||
import {
|
||||
parseFetchRemotes,
|
||||
parseGitHubRepositorySelector,
|
||||
parsePullRequestReference,
|
||||
resolveGitHubRepository,
|
||||
type GitHubRepositoryLocator,
|
||||
} from "./gitHubRepositoryResolution.ts";
|
||||
import * as SourceControlRateLimit from "./SourceControlRateLimit.ts";
|
||||
import {
|
||||
effectiveGitHubAccount,
|
||||
findAuthenticatedGitHubAccount,
|
||||
@@ -40,33 +61,8 @@ const decodeLinkSubject = Schema.decodeUnknownEffect(
|
||||
),
|
||||
);
|
||||
|
||||
function toChangeRequest(summary: GitHubRepositoryApi.GitHubPullRequestSummary): ChangeRequest {
|
||||
return {
|
||||
provider: "github",
|
||||
number: summary.number,
|
||||
title: summary.title,
|
||||
url: summary.url,
|
||||
baseRefName: summary.baseRefName,
|
||||
headRefName: summary.headRefName,
|
||||
...(summary.headSha !== undefined ? { headSha: summary.headSha } : {}),
|
||||
state: summary.state ?? "open",
|
||||
...(summary.isDraft === true ? { isDraft: true } : {}),
|
||||
closedAt: summary.closedAt ?? null,
|
||||
mergedAt: summary.mergedAt ?? null,
|
||||
updatedAt:
|
||||
summary.updatedAt === undefined
|
||||
? Option.none()
|
||||
: Option.some(DateTime.makeUnsafe(summary.updatedAt)),
|
||||
...(summary.isCrossRepository !== undefined
|
||||
? { isCrossRepository: summary.isCrossRepository }
|
||||
: {}),
|
||||
...(summary.headRepositoryNameWithOwner !== undefined
|
||||
? { headRepositoryNameWithOwner: summary.headRepositoryNameWithOwner }
|
||||
: {}),
|
||||
...(summary.headRepositoryOwnerLogin !== undefined
|
||||
? { headRepositoryOwnerLogin: summary.headRepositoryOwnerLogin }
|
||||
: {}),
|
||||
};
|
||||
function toChangeRequest(record: NormalizedGitHubPullRequestRecord): ChangeRequest {
|
||||
return { provider: "github", ...record };
|
||||
}
|
||||
|
||||
function authAccounts(accounts: ReadonlyArray<GitHubAuthStatusAccount>) {
|
||||
@@ -263,77 +259,601 @@ export const makeDiscovery = Effect.gen(function* () {
|
||||
} satisfies SourceControlManagedCliDiscoverySpec;
|
||||
});
|
||||
|
||||
/**
|
||||
* Why a GitHub read or write failed, in words a user can act on. The provider methods attach the
|
||||
* operation and request context; `cause` keeps the raw failure for logs, never for transport.
|
||||
*/
|
||||
class GitHubFailure extends Data.TaggedError("GitHubFailure")<{
|
||||
readonly detail: string;
|
||||
readonly cause: unknown;
|
||||
}> {}
|
||||
|
||||
const failure = (detail: string, cause?: unknown) => new GitHubFailure({ detail, cause });
|
||||
|
||||
const PULL_REQUEST_NOT_FOUND = "Pull request not found. Check the PR number or URL and try again.";
|
||||
const REPOSITORY_NOT_FOUND = "Repository not found. Check the owner and name and try again.";
|
||||
|
||||
/** `notFound` names what was missing, which only the operation knows. */
|
||||
function fromGitHubApiError(
|
||||
error: GitHubApi.GitHubApiError,
|
||||
notFound = PULL_REQUEST_NOT_FOUND,
|
||||
): GitHubFailure {
|
||||
switch (error._tag) {
|
||||
case "GitHubCliMissingError":
|
||||
return failure(
|
||||
"No GitHub credential on the server. Set GH_TOKEN, or install the GitHub CLI and run `gh auth login`.",
|
||||
error,
|
||||
);
|
||||
case "GitHubNotSignedInError":
|
||||
case "GitHubHostDisabledError":
|
||||
// A missing or turned-off credential already says what to do about it.
|
||||
return failure(error.message, error);
|
||||
case "GitHubApiAuthenticationError":
|
||||
return failure(
|
||||
"GitHub is not authenticated. Run `gh auth login` (or set GH_TOKEN) and retry.",
|
||||
error,
|
||||
);
|
||||
case "GitHubApiRateLimitError":
|
||||
case "SourceControlRateLimitPausedError":
|
||||
return failure(
|
||||
"GitHub API rate limit exceeded. Requests resume when the limit resets.",
|
||||
error,
|
||||
);
|
||||
case "GitHubApiNotFoundError":
|
||||
return failure(notFound, error);
|
||||
case "GitHubCliFailedError":
|
||||
case "GitHubApiResponseError":
|
||||
case "GitHubApiRequestError":
|
||||
// GitHub's own reason ("A pull request already exists…") when it gave one.
|
||||
return failure(error.message.trim() || "GitHub request failed.", error);
|
||||
}
|
||||
}
|
||||
|
||||
const notFound = (cause: string) => failure(PULL_REQUEST_NOT_FOUND, new Error(cause));
|
||||
|
||||
const RawRepositorySchema = Schema.Struct({
|
||||
full_name: TrimmedNonEmptyString,
|
||||
html_url: TrimmedNonEmptyString,
|
||||
ssh_url: TrimmedNonEmptyString,
|
||||
default_branch: Schema.optional(Schema.NullOr(Schema.String)),
|
||||
});
|
||||
const decodeRawRepository = decodeJsonResult(RawRepositorySchema);
|
||||
|
||||
function repositoryCloneUrls(
|
||||
raw: Schema.Schema.Type<typeof RawRepositorySchema>,
|
||||
): SourceControlRepositoryCloneUrls {
|
||||
return { nameWithOwner: raw.full_name, url: raw.html_url, sshUrl: raw.ssh_url };
|
||||
}
|
||||
|
||||
const decodeViewerLogin = decodeJsonResult(Schema.Struct({ login: TrimmedNonEmptyString }));
|
||||
|
||||
type PullRequestListState = "open" | "closed" | "merged" | "all";
|
||||
|
||||
/** The pull request fields every read selects, in GraphQL. */
|
||||
const PULL_REQUEST_NODE_SELECTION =
|
||||
"number title url baseRefName headRefName headRefOid state isDraft mergedAt closedAt updatedAt isCrossRepository headRepository { name nameWithOwner } headRepositoryOwner { login }";
|
||||
const GRAPHQL_STATES: Record<PullRequestListState, ReadonlyArray<string>> = {
|
||||
open: ["OPEN"],
|
||||
closed: ["CLOSED"],
|
||||
merged: ["MERGED"],
|
||||
all: ["OPEN", "CLOSED", "MERGED"],
|
||||
};
|
||||
/**
|
||||
* Head lookups per GraphQL document. A document of a hundred costs one point, the same as one
|
||||
* single lookup, but half that keeps each answer near half a second.
|
||||
*/
|
||||
const HEAD_LOOKUPS_PER_DOCUMENT = 50;
|
||||
/**
|
||||
* How long a head lookup waits for company. Branch discovery reaches GitHub only after each
|
||||
* branch's own git reads, so lookups started together arrive tens of milliseconds apart.
|
||||
* A background sweep's lookups spread over up to ~300ms, and every document costs a point no
|
||||
* matter how few heads it holds, so reads without reserve wait longer.
|
||||
*/
|
||||
const HEAD_LOOKUP_BATCH_WINDOW = "50 millis";
|
||||
const BACKGROUND_HEAD_LOOKUP_BATCH_WINDOW = "500 millis";
|
||||
/**
|
||||
* Background documents fill up under the longer window, and a failed document fails every head
|
||||
* in it. Fifty `main`-like heads of a hundred pull requests each took up to ~10s, GitHub's own
|
||||
* processing limit; twenty-five took ~7s.
|
||||
*/
|
||||
const BACKGROUND_HEAD_LOOKUPS_PER_DOCUMENT = 25;
|
||||
/** A full document is 5,000 rows of well under 2 KB each. */
|
||||
const HEAD_LOOKUP_MAX_RESPONSE_BYTES = 16_000_000;
|
||||
/**
|
||||
* Rows read for a fork's `owner:branch` head, which GitHub cannot filter by owner. A branch
|
||||
* named like a busy default (`main`) is the case this bounds; the owner's own row is near the
|
||||
* top because the newest come first.
|
||||
*/
|
||||
const OWNER_HEAD_SCAN_LIMIT = 100;
|
||||
|
||||
class PullRequestsByHeadRead extends Request.Class<
|
||||
{
|
||||
readonly host: string;
|
||||
readonly owner: string;
|
||||
readonly name: string;
|
||||
readonly headRefName: string;
|
||||
readonly state: PullRequestListState;
|
||||
readonly limit: number;
|
||||
readonly allowReserve: boolean;
|
||||
},
|
||||
ReadonlyArray<NormalizedGitHubPullRequestRecord>,
|
||||
GitHubFailure
|
||||
> {}
|
||||
|
||||
/** One aliased `pullRequests` connection per lookup, each head and state passed as a variable. */
|
||||
function buildPullRequestsByHeadQuery(
|
||||
lookups: ReadonlyArray<Pick<PullRequestsByHeadRead, "headRefName" | "state" | "limit">>,
|
||||
): { readonly document: string; readonly variables: Record<string, unknown> } {
|
||||
const variables: Record<string, unknown> = {};
|
||||
const declarations: string[] = ["$owner: String!", "$name: String!"];
|
||||
const selections: string[] = [];
|
||||
for (const [index, lookup] of lookups.entries()) {
|
||||
variables[`h${index}`] = lookup.headRefName;
|
||||
variables[`s${index}`] = GRAPHQL_STATES[lookup.state];
|
||||
declarations.push(`$h${index}: String!`, `$s${index}: [PullRequestState!]`);
|
||||
// `gh pr list` orders the same way, so a head with more matches than the limit keeps the
|
||||
// same rows.
|
||||
selections.push(
|
||||
` h${index}: pullRequests(headRefName: $h${index}, states: $s${index}, first: ${lookup.limit}, orderBy: { field: CREATED_AT, direction: DESC }) { nodes { ${PULL_REQUEST_NODE_SELECTION} } }`,
|
||||
);
|
||||
}
|
||||
return {
|
||||
document: `query PullRequestsByHead(${declarations.join(", ")}) {\n repository(owner: $owner, name: $name) {\n${selections.join("\n")}\n }\n}`,
|
||||
variables,
|
||||
};
|
||||
}
|
||||
|
||||
const PULL_REQUEST_BY_NUMBER_QUERY = `query PullRequestByNumber($owner: String!, $name: String!, $number: Int!) {
|
||||
repository(owner: $owner, name: $name) {
|
||||
pullRequest(number: $number) { ${PULL_REQUEST_NODE_SELECTION} }
|
||||
}
|
||||
}`;
|
||||
|
||||
const decodePullRequestsByHead = decodeJsonResult(
|
||||
Schema.Struct({
|
||||
data: Schema.Struct({
|
||||
repository: Schema.NullOr(
|
||||
Schema.Record(
|
||||
Schema.String,
|
||||
Schema.NullOr(Schema.Struct({ nodes: Schema.Array(Schema.Unknown) })),
|
||||
),
|
||||
),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
|
||||
const decodePullRequestByNumber = decodeJsonResult(
|
||||
Schema.Struct({
|
||||
data: Schema.Struct({
|
||||
repository: Schema.NullOr(Schema.Struct({ pullRequest: Schema.NullOr(Schema.Unknown) })),
|
||||
}),
|
||||
}),
|
||||
);
|
||||
|
||||
/** The local branch a pull request checks out into, the way `gh pr checkout` names it. */
|
||||
export function pullRequestCheckoutBranchName(input: {
|
||||
readonly headRefName: string;
|
||||
readonly headOwner: string | null;
|
||||
readonly isCrossRepository: boolean;
|
||||
readonly defaultBranch: string | null;
|
||||
}): string {
|
||||
// gh prefixes the owner only where the fork's branch would take over the default branch's
|
||||
// name, which is the one collision every fork pull request from `main` would hit.
|
||||
return input.isCrossRepository &&
|
||||
input.headOwner !== null &&
|
||||
input.defaultBranch !== null &&
|
||||
input.headRefName === input.defaultBranch
|
||||
? `${input.headOwner}/${input.headRefName}`
|
||||
: input.headRefName;
|
||||
}
|
||||
|
||||
/** The checkout's GitHub API host, from the remote the caller resolved the provider from. */
|
||||
const contextHost = (context: SourceControlProvider.SourceControlProviderContext | undefined) =>
|
||||
context === undefined ? undefined : new URL(context.provider.baseUrl).host;
|
||||
|
||||
export const make = Effect.gen(function* () {
|
||||
const github = yield* GitHubRepositoryApi.GitHubRepositoryApi;
|
||||
const api = yield* GitHubApi.GitHubApi;
|
||||
const process = yield* VcsProcess.VcsProcess;
|
||||
const environment = yield* HostProcessEnvironment;
|
||||
const git = yield* GitVcsDriver.GitVcsDriver;
|
||||
const fileSystem = yield* FileSystem.FileSystem;
|
||||
|
||||
const listChangeRequests: SourceControlProvider.SourceControlProvider["Service"]["listChangeRequests"] =
|
||||
(input) => {
|
||||
if (input.state === "open") {
|
||||
return github
|
||||
.listOpenPullRequests({
|
||||
cwd: input.cwd,
|
||||
headSelector: input.headSelector,
|
||||
...(input.context === undefined
|
||||
? {}
|
||||
: { rateLimitHost: new URL(input.context.provider.baseUrl).host }),
|
||||
...(input.limit !== undefined ? { limit: input.limit } : {}),
|
||||
})
|
||||
.pipe(
|
||||
Effect.map((items) => items.map(toChangeRequest)),
|
||||
Effect.mapError(
|
||||
(error) =>
|
||||
new SourceControlProviderError({
|
||||
provider: "github",
|
||||
operation: "listChangeRequests",
|
||||
cwd: input.cwd,
|
||||
reference: SourceControlProvider.transportSafeSourceControlErrorValue(
|
||||
input.headSelector,
|
||||
),
|
||||
detail: error.message,
|
||||
cause: error,
|
||||
}),
|
||||
),
|
||||
);
|
||||
const gitRead = (cwd: string, args: ReadonlyArray<string>) =>
|
||||
process.run({
|
||||
operation: "GitHubSourceControlProvider.resolveRepository",
|
||||
command: "git",
|
||||
args,
|
||||
cwd,
|
||||
allowNonZeroExit: true,
|
||||
timeoutMs: 5_000,
|
||||
});
|
||||
|
||||
/**
|
||||
* The repository `gh` would act on in `cwd`: GH_REPO, else the remote `gh` would pick, else
|
||||
* the remote the caller resolved the provider from, else the best-ranked GitHub remote.
|
||||
*/
|
||||
const resolveRepository = Effect.fn("GitHubSourceControlProvider.resolveRepository")(
|
||||
function* (input: { readonly cwd: string; readonly host?: string | undefined }) {
|
||||
const envRepository = environment.GH_REPO?.trim();
|
||||
const defaultHost = (input.host ?? environment.GH_HOST ?? "github.com").toLowerCase();
|
||||
if (envRepository) {
|
||||
const locator = parseGitHubRepositorySelector(envRepository, defaultHost);
|
||||
if (locator !== null) return locator;
|
||||
}
|
||||
const [remotes, resolved] = yield* Effect.all([
|
||||
gitRead(input.cwd, ["remote", "-v"]),
|
||||
gitRead(input.cwd, ["config", "--get-regexp", "^remote\\..*\\.gh-resolved$"]),
|
||||
]).pipe(Effect.orElseSucceed(() => [null, null] as const));
|
||||
const { host, locator } = resolveGitHubRepository({
|
||||
remotes: remotes?.exitCode === 0 ? remotes.stdout : "",
|
||||
resolved: resolved?.exitCode === 0 ? resolved.stdout : "",
|
||||
hostHint: input.host,
|
||||
defaultHost,
|
||||
});
|
||||
if (locator !== null) return locator;
|
||||
return yield* failure(
|
||||
`No GitHub repository on ${host} was found among this checkout's git remotes.`,
|
||||
);
|
||||
},
|
||||
);
|
||||
|
||||
return github
|
||||
.listPullRequestsByHead({
|
||||
const graphqlJson = <A>(
|
||||
input: GitHubApi.GitHubGraphQlInput,
|
||||
decode: (raw: string) => Result.Result<A, unknown>,
|
||||
decodeDetail: string,
|
||||
) =>
|
||||
api.graphql(input).pipe(
|
||||
Effect.mapError(fromGitHubApiError),
|
||||
Effect.flatMap((raw) => {
|
||||
const decoded = decode(raw);
|
||||
return Result.isSuccess(decoded)
|
||||
? Effect.succeed(decoded.success)
|
||||
: Effect.fail(failure(decodeDetail, decoded.failure));
|
||||
}),
|
||||
);
|
||||
|
||||
const rest = (input: GitHubApi.GitHubRestInput, notFoundDetail?: string) =>
|
||||
api.rest(input).pipe(Effect.mapError((error) => fromGitHubApiError(error, notFoundDetail)));
|
||||
|
||||
const headResolver = RequestResolver.makeGrouped<PullRequestsByHeadRead, string>({
|
||||
key: ({ request, context }) =>
|
||||
[
|
||||
request.host,
|
||||
request.owner,
|
||||
request.name,
|
||||
String(request.allowReserve),
|
||||
Context.getOrElse(context, GitHubApi.PinnedGitHubCredential, () => null)
|
||||
?.credentialFingerprint ?? "",
|
||||
Context.getOrElse(context, SourceControlRateLimit.CredentialScope, () => ""),
|
||||
].join("\0"),
|
||||
resolver: (entries) => {
|
||||
const [first] = entries;
|
||||
const { host, owner, name, allowReserve } = first.request;
|
||||
const query = buildPullRequestsByHeadQuery(entries.map((entry) => entry.request));
|
||||
return graphqlJson(
|
||||
{
|
||||
host,
|
||||
operation: "listPullRequestsByHead",
|
||||
query: query.document,
|
||||
variables: { owner, name, ...query.variables },
|
||||
allowReserve,
|
||||
// Up to 50 heads of 100 rows each. A default branch such as `main` can match a hundred
|
||||
// fork pull requests, so the usual cap would cut the answer short.
|
||||
maxResponseBytes: HEAD_LOOKUP_MAX_RESPONSE_BYTES,
|
||||
},
|
||||
decodePullRequestsByHead,
|
||||
"GitHub returned an invalid change request list.",
|
||||
).pipe(
|
||||
Effect.flatMap((decoded) =>
|
||||
decoded.data.repository === null
|
||||
? Effect.fail(notFound("The repository could not be read."))
|
||||
: Effect.succeed(decoded.data.repository),
|
||||
),
|
||||
Effect.map((aliases) => {
|
||||
for (const [index, entry] of entries.entries()) {
|
||||
const alias = aliases[`h${index}`];
|
||||
entry.completeUnsafe(
|
||||
Exit.succeed(alias == null ? [] : decodeGitHubPullRequestEntries(alias.nodes)),
|
||||
);
|
||||
}
|
||||
}),
|
||||
Effect.catchCause((cause) =>
|
||||
Effect.sync(() => {
|
||||
for (const entry of entries) entry.completeUnsafe(Exit.failCause(cause));
|
||||
}),
|
||||
),
|
||||
);
|
||||
},
|
||||
}).pipe(RequestResolver.batchN(HEAD_LOOKUPS_PER_DOCUMENT));
|
||||
const interactiveHeadResolver = headResolver.pipe(
|
||||
RequestResolver.setDelay(HEAD_LOOKUP_BATCH_WINDOW),
|
||||
);
|
||||
const backgroundHeadResolver = headResolver.pipe(
|
||||
RequestResolver.batchN(BACKGROUND_HEAD_LOOKUPS_PER_DOCUMENT),
|
||||
RequestResolver.setDelay(BACKGROUND_HEAD_LOOKUP_BATCH_WINDOW),
|
||||
);
|
||||
|
||||
/**
|
||||
* Pull requests whose head is `headSelector` (a branch, or `owner:branch` for a fork), in the
|
||||
* repository `gh pr list` would read in `cwd`. Lookups on one repository that arrive together
|
||||
* share one GraphQL document.
|
||||
*/
|
||||
const listByHead = Effect.fn("GitHubSourceControlProvider.listByHead")(function* (input: {
|
||||
readonly cwd: string;
|
||||
readonly headSelector: string;
|
||||
readonly state: PullRequestListState;
|
||||
readonly limit: number;
|
||||
readonly host?: string | undefined;
|
||||
readonly allowReserve: boolean;
|
||||
}) {
|
||||
const locator = yield* resolveRepository({ cwd: input.cwd, host: input.host });
|
||||
const limit = Math.min(Math.max(Math.trunc(input.limit), 1), 100);
|
||||
// `owner:branch` names a fork's branch. GitHub filters on the branch name only, so the
|
||||
// owner is matched on the rows it returns.
|
||||
const ownerMatch = /^([^:/\s]+):(.+)$/u.exec(input.headSelector.trim());
|
||||
const headRefName = ownerMatch?.[2] ?? input.headSelector.trim();
|
||||
const rows = yield* Effect.request(
|
||||
new PullRequestsByHeadRead({
|
||||
...locator,
|
||||
headRefName,
|
||||
state: input.state,
|
||||
limit: ownerMatch ? OWNER_HEAD_SCAN_LIMIT : limit,
|
||||
allowReserve: input.allowReserve,
|
||||
}),
|
||||
input.allowReserve ? interactiveHeadResolver : backgroundHeadResolver,
|
||||
);
|
||||
if (!ownerMatch) return rows;
|
||||
const headOwner = ownerMatch[1]!.toLowerCase();
|
||||
return rows
|
||||
.filter((row) => row.headRepositoryOwnerLogin?.toLowerCase() === headOwner)
|
||||
.slice(0, limit);
|
||||
});
|
||||
|
||||
const readPullRequest = Effect.fn("GitHubSourceControlProvider.readPullRequest")(
|
||||
function* (input: {
|
||||
readonly cwd: string;
|
||||
readonly reference: string;
|
||||
readonly host?: string | undefined;
|
||||
}) {
|
||||
const parsed = parsePullRequestReference(input.reference);
|
||||
if (parsed.kind === "branch") {
|
||||
// `gh pr view <branch>` prefers an open pull request, then the newest of any state.
|
||||
const lookup = (state: PullRequestListState) =>
|
||||
listByHead({
|
||||
cwd: input.cwd,
|
||||
headSelector: parsed.headSelector,
|
||||
state,
|
||||
limit: 1,
|
||||
host: input.host,
|
||||
allowReserve: true,
|
||||
});
|
||||
const [open] = yield* lookup("open");
|
||||
const found = open ?? (yield* lookup("all"))[0];
|
||||
if (found === undefined) return yield* notFound("No pull request has this head branch.");
|
||||
return found;
|
||||
}
|
||||
const locator =
|
||||
parsed.kind === "url"
|
||||
? parsed.locator
|
||||
: yield* resolveRepository({ cwd: input.cwd, host: input.host });
|
||||
const decodeDetail = "GitHub returned an invalid pull request.";
|
||||
const decoded = yield* graphqlJson(
|
||||
{
|
||||
host: locator.host,
|
||||
operation: "getPullRequest",
|
||||
query: PULL_REQUEST_BY_NUMBER_QUERY,
|
||||
variables: { owner: locator.owner, name: locator.name, number: parsed.number },
|
||||
allowReserve: true,
|
||||
},
|
||||
decodePullRequestByNumber,
|
||||
decodeDetail,
|
||||
);
|
||||
const node = decoded.data.repository?.pullRequest;
|
||||
if (node == null) return yield* notFound("The pull request does not exist.");
|
||||
const [record] = decodeGitHubPullRequestEntries([node]);
|
||||
if (record === undefined)
|
||||
return yield* failure(decodeDetail, new Error("Malformed pull request."));
|
||||
return record;
|
||||
},
|
||||
);
|
||||
|
||||
const readRepository = Effect.fn("GitHubSourceControlProvider.readRepository")(function* (
|
||||
locator: GitHubRepositoryLocator,
|
||||
) {
|
||||
const response = yield* rest(
|
||||
{
|
||||
host: locator.host,
|
||||
operation: "getRepository",
|
||||
path: `repos/${encodeURIComponent(locator.owner)}/${encodeURIComponent(locator.name)}`,
|
||||
allowReserve: true,
|
||||
},
|
||||
REPOSITORY_NOT_FOUND,
|
||||
);
|
||||
const decoded = decodeRawRepository(response.body);
|
||||
if (Result.isFailure(decoded)) {
|
||||
return yield* failure("GitHub returned an invalid repository.", decoded.failure);
|
||||
}
|
||||
return decoded.success;
|
||||
});
|
||||
|
||||
const readViewerLogin = Effect.fn("GitHubSourceControlProvider.readViewerLogin")(function* (
|
||||
host: string,
|
||||
) {
|
||||
const response = yield* rest({ host, operation: "getViewer", path: "user" });
|
||||
const decoded = decodeViewerLogin(response.body);
|
||||
if (Result.isFailure(decoded)) {
|
||||
return yield* failure("GitHub request failed.", decoded.failure);
|
||||
}
|
||||
return decoded.success.login;
|
||||
});
|
||||
|
||||
// Git and filesystem errors can carry arguments, paths and stderr, so the detail a client
|
||||
// sees is fixed and the raw error stays in `cause`.
|
||||
const gitFailure = (cause: unknown) =>
|
||||
failure("The pull request could not be checked out with git.", cause);
|
||||
|
||||
const runGit = (cwd: string, operation: string, args: ReadonlyArray<string>) =>
|
||||
git.execute({
|
||||
operation: `GitHubSourceControlProvider.checkoutPullRequest.${operation}`,
|
||||
cwd,
|
||||
args,
|
||||
});
|
||||
|
||||
/**
|
||||
* `gh pr checkout` in plain git: the head branch is fetched from the remote that holds it (a
|
||||
* fork gets a remote of its own), checked out under the name gh would give it, and set to
|
||||
* track the head. A head branch that is gone is read from the base's `refs/pull/<n>/head`.
|
||||
* An existing branch fast-forwards, or with `force` is reset to the pull request.
|
||||
*/
|
||||
const checkoutPullRequest = Effect.fn("GitHubSourceControlProvider.checkoutPullRequest")(
|
||||
function* (input: {
|
||||
readonly cwd: string;
|
||||
readonly reference: string;
|
||||
readonly force?: boolean;
|
||||
}) {
|
||||
const reference = parsePullRequestReference(input.reference);
|
||||
const pullRequest = yield* readPullRequest({ cwd: input.cwd, reference: input.reference });
|
||||
const base =
|
||||
reference.kind === "url" ? reference.locator : yield* resolveRepository({ cwd: input.cwd });
|
||||
const baseNameWithOwner = `${base.owner}/${base.name}`.toLowerCase();
|
||||
const headNameWithOwner = pullRequest.headRepositoryNameWithOwner ?? null;
|
||||
const isCrossRepository =
|
||||
pullRequest.isCrossRepository ??
|
||||
(headNameWithOwner !== null && headNameWithOwner.toLowerCase() !== baseNameWithOwner);
|
||||
const headOwner =
|
||||
pullRequest.headRepositoryOwnerLogin ?? headNameWithOwner?.split("/")[0] ?? null;
|
||||
|
||||
const remotes = yield* gitRead(input.cwd, ["remote", "-v"]).pipe(
|
||||
Effect.map((result) => (result.exitCode === 0 ? result.stdout : "")),
|
||||
Effect.mapError(gitFailure),
|
||||
);
|
||||
const remoteFor = (nameWithOwner: string) =>
|
||||
parseFetchRemotes(remotes).find(
|
||||
(remote) =>
|
||||
normalizeGitRemoteUrl(remote.url).split("/").slice(1).join("/") ===
|
||||
nameWithOwner.toLowerCase(),
|
||||
)?.name ?? null;
|
||||
const baseRemote = Effect.suspend(() => {
|
||||
const known = remoteFor(baseNameWithOwner);
|
||||
return known === null ? git.resolvePrimaryRemoteName(input.cwd) : Effect.succeed(known);
|
||||
});
|
||||
// A fork's branch named like the base's default branch is checked out under the owner's
|
||||
// prefix. Without the default branch that collision cannot be ruled out, and the checkout
|
||||
// would reset the local default branch to the fork's commit, so it fails instead.
|
||||
const defaultBranch = isCrossRepository
|
||||
? yield* readRepository(base).pipe(
|
||||
Effect.map((repository) => repository.default_branch ?? null),
|
||||
)
|
||||
: null;
|
||||
const localBranch = pullRequestCheckoutBranchName({
|
||||
headRefName: pullRequest.headRefName,
|
||||
headOwner,
|
||||
isCrossRepository,
|
||||
defaultBranch,
|
||||
});
|
||||
|
||||
/** The remote the head branch lives on; a fork the checkout does not know yet is added. */
|
||||
const headRemote = Effect.gen(function* () {
|
||||
if (!isCrossRepository) return yield* baseRemote;
|
||||
if (headNameWithOwner === null) return yield* failure("The fork is gone.");
|
||||
const known = remoteFor(headNameWithOwner);
|
||||
if (known !== null) return known;
|
||||
const [owner, name] = headNameWithOwner.split("/");
|
||||
const fork = yield* readRepository({ host: base.host, owner: owner!, name: name! });
|
||||
const originUrl = yield* git.readConfigValue(input.cwd, "remote.origin.url");
|
||||
return yield* git.ensureRemote({
|
||||
cwd: input.cwd,
|
||||
headSelector: input.headSelector,
|
||||
state: input.state,
|
||||
limit: input.limit ?? 20,
|
||||
...(input.context === undefined
|
||||
? {}
|
||||
: { rateLimitHost: new URL(input.context.provider.baseUrl).host }),
|
||||
})
|
||||
.pipe(
|
||||
Effect.map((items) =>
|
||||
items.map(({ updatedAt, ...summary }) => ({
|
||||
...toChangeRequest({
|
||||
...summary,
|
||||
...(Option.isSome(updatedAt)
|
||||
? { updatedAt: DateTime.formatIso(updatedAt.value) }
|
||||
: {}),
|
||||
}),
|
||||
updatedAt,
|
||||
})),
|
||||
),
|
||||
Effect.mapError(
|
||||
(error) =>
|
||||
new SourceControlProviderError({
|
||||
provider: "github",
|
||||
operation: "listChangeRequests",
|
||||
cwd: input.cwd,
|
||||
reference: SourceControlProvider.transportSafeSourceControlErrorValue(
|
||||
input.headSelector,
|
||||
),
|
||||
detail: error.message,
|
||||
cause: error,
|
||||
}),
|
||||
),
|
||||
);
|
||||
};
|
||||
preferredName: headOwner ?? "fork",
|
||||
url: originUrl !== null && isSshRemoteUrl(originUrl) ? fork.ssh_url : fork.html_url,
|
||||
});
|
||||
});
|
||||
|
||||
const exists = (yield* git
|
||||
.listLocalBranchNames(input.cwd)
|
||||
.pipe(Effect.mapError(gitFailure))).includes(localBranch);
|
||||
|
||||
// The commit to check out, fetched from the head branch where it still exists.
|
||||
const target = yield* Effect.gen(function* () {
|
||||
const remoteName = yield* headRemote;
|
||||
yield* git.fetchRemoteTrackingBranch({
|
||||
cwd: input.cwd,
|
||||
remoteName,
|
||||
remoteBranch: pullRequest.headRefName,
|
||||
});
|
||||
return {
|
||||
ref: `refs/remotes/${remoteName}/${pullRequest.headRefName}`,
|
||||
upstream: { remoteName, remoteBranch: pullRequest.headRefName },
|
||||
};
|
||||
}).pipe(
|
||||
Effect.catch(() =>
|
||||
Effect.gen(function* () {
|
||||
yield* runGit(input.cwd, "fetchPullRef", [
|
||||
"fetch",
|
||||
"--quiet",
|
||||
"--no-tags",
|
||||
yield* baseRemote,
|
||||
`refs/pull/${pullRequest.number}/head`,
|
||||
]);
|
||||
const { commitSha } = yield* git.resolveCommit({
|
||||
cwd: input.cwd,
|
||||
revision: "FETCH_HEAD",
|
||||
});
|
||||
return { ref: commitSha, upstream: null };
|
||||
}),
|
||||
),
|
||||
Effect.mapError(gitFailure),
|
||||
);
|
||||
|
||||
yield* Effect.gen(function* () {
|
||||
if (!exists) yield* runGit(input.cwd, "branch", ["branch", localBranch, target.ref]);
|
||||
yield* Effect.scoped(git.switchRef({ cwd: input.cwd, refName: localBranch }));
|
||||
if (exists) {
|
||||
yield* runGit(
|
||||
input.cwd,
|
||||
"sync",
|
||||
input.force === true
|
||||
? ["reset", "--hard", "--quiet", target.ref]
|
||||
: ["merge", "--ff-only", "--quiet", target.ref],
|
||||
);
|
||||
}
|
||||
// Tracking is set once the branch is the pull request's, so a sync that fails leaves an
|
||||
// existing branch's upstream as it was.
|
||||
if (target.upstream !== null) {
|
||||
yield* git.setBranchUpstream({ cwd: input.cwd, branch: localBranch, ...target.upstream });
|
||||
}
|
||||
}).pipe(Effect.mapError(gitFailure));
|
||||
},
|
||||
);
|
||||
|
||||
/** Reports a failure against the operation and request that made it, with transport-safe context. */
|
||||
const providerError =
|
||||
(
|
||||
operation: string,
|
||||
cwd: string,
|
||||
context?: { readonly reference?: string; readonly repository?: string },
|
||||
) =>
|
||||
(error: GitHubFailure) =>
|
||||
new SourceControlProviderError({
|
||||
provider: "github",
|
||||
operation,
|
||||
cwd,
|
||||
...(context?.reference === undefined
|
||||
? {}
|
||||
: {
|
||||
reference: SourceControlProvider.transportSafeSourceControlErrorValue(
|
||||
context.reference,
|
||||
),
|
||||
}),
|
||||
...(context?.repository === undefined
|
||||
? {}
|
||||
: {
|
||||
repository: SourceControlProvider.transportSafeSourceControlErrorValue(
|
||||
context.repository,
|
||||
),
|
||||
}),
|
||||
detail: error.detail,
|
||||
cause: error.cause,
|
||||
});
|
||||
|
||||
const readLinkSubject = Effect.fn("GitHubSourceControlProvider.readLinkSubject")(function* (
|
||||
input: { readonly cwd: string; readonly url: URL },
|
||||
@@ -385,121 +905,122 @@ export const make = Effect.gen(function* () {
|
||||
if (!match) return undefined;
|
||||
return readLinkSubject(input, `repos/${match[1]}/${match[2]}/issues/${match[3]}`);
|
||||
},
|
||||
listChangeRequests,
|
||||
getChangeRequest: (input) =>
|
||||
github
|
||||
.getPullRequest({
|
||||
...input,
|
||||
...(input.context === undefined
|
||||
? {}
|
||||
: { rateLimitHost: new URL(input.context.provider.baseUrl).host }),
|
||||
})
|
||||
.pipe(
|
||||
Effect.map(toChangeRequest),
|
||||
Effect.mapError(
|
||||
(error) =>
|
||||
new SourceControlProviderError({
|
||||
provider: "github",
|
||||
operation: "getChangeRequest",
|
||||
cwd: input.cwd,
|
||||
reference: SourceControlProvider.transportSafeSourceControlErrorValue(
|
||||
input.reference,
|
||||
),
|
||||
detail: error.message,
|
||||
cause: error,
|
||||
}),
|
||||
),
|
||||
listChangeRequests: (input) =>
|
||||
// An open lookup is a user waiting on a status; the rest may be a background sweep.
|
||||
(input.state === "open" ? Effect.succeed(true) : GitHubApi.AllowGitHubReserve).pipe(
|
||||
Effect.flatMap((allowReserve) =>
|
||||
listByHead({
|
||||
cwd: input.cwd,
|
||||
headSelector: input.headSelector,
|
||||
state: input.state,
|
||||
limit: input.limit ?? (input.state === "open" ? 1 : 20),
|
||||
host: contextHost(input.context),
|
||||
allowReserve,
|
||||
}),
|
||||
),
|
||||
createChangeRequest: (input) =>
|
||||
github
|
||||
.createPullRequest({
|
||||
cwd: input.cwd,
|
||||
baseBranch: input.baseRefName,
|
||||
headSelector: input.headSelector,
|
||||
title: input.title,
|
||||
bodyFile: input.bodyFile,
|
||||
})
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(error) =>
|
||||
new SourceControlProviderError({
|
||||
provider: "github",
|
||||
operation: "createChangeRequest",
|
||||
cwd: input.cwd,
|
||||
reference: SourceControlProvider.transportSafeSourceControlErrorValue(
|
||||
input.headSelector,
|
||||
),
|
||||
detail: error.message,
|
||||
cause: error,
|
||||
}),
|
||||
),
|
||||
),
|
||||
getRepositoryCloneUrls: (input) =>
|
||||
github.getRepositoryCloneUrls(input).pipe(
|
||||
Effect.map((records) => records.map(toChangeRequest)),
|
||||
Effect.mapError(
|
||||
(error) =>
|
||||
new SourceControlProviderError({
|
||||
provider: "github",
|
||||
operation: "getRepositoryCloneUrls",
|
||||
cwd: input.cwd,
|
||||
repository: SourceControlProvider.transportSafeSourceControlErrorValue(
|
||||
input.repository,
|
||||
),
|
||||
detail: error.message,
|
||||
cause: error,
|
||||
}),
|
||||
providerError("listChangeRequests", input.cwd, { reference: input.headSelector }),
|
||||
),
|
||||
),
|
||||
getChangeRequest: (input) =>
|
||||
readPullRequest({ ...input, host: contextHost(input.context) }).pipe(
|
||||
Effect.map(toChangeRequest),
|
||||
Effect.mapError(
|
||||
providerError("getChangeRequest", input.cwd, { reference: input.reference }),
|
||||
),
|
||||
),
|
||||
createChangeRequest: (input) =>
|
||||
Effect.gen(function* () {
|
||||
const locator = yield* resolveRepository({ cwd: input.cwd });
|
||||
const body = yield* fileSystem
|
||||
.readFileString(input.bodyFile)
|
||||
.pipe(
|
||||
Effect.mapError((cause) =>
|
||||
failure("The pull request description could not be read.", cause),
|
||||
),
|
||||
);
|
||||
yield* rest({
|
||||
host: locator.host,
|
||||
operation: "createPullRequest",
|
||||
method: "POST",
|
||||
path: `repos/${encodeURIComponent(locator.owner)}/${encodeURIComponent(locator.name)}/pulls`,
|
||||
// `owner:branch` is how the REST API takes a fork's head, the same as `gh --head`.
|
||||
// gh allows maintainer edits unless told otherwise; the API's default is not documented.
|
||||
body: {
|
||||
base: input.baseRefName,
|
||||
head: input.headSelector,
|
||||
title: input.title,
|
||||
body,
|
||||
maintainer_can_modify: true,
|
||||
},
|
||||
});
|
||||
}).pipe(
|
||||
Effect.mapError(
|
||||
providerError("createChangeRequest", input.cwd, { reference: input.headSelector }),
|
||||
),
|
||||
),
|
||||
getRepositoryCloneUrls: (input) =>
|
||||
Effect.gen(function* () {
|
||||
const fallbackHost = (yield* resolveRepository({ cwd: input.cwd }).pipe(
|
||||
Effect.map((locator) => locator.host),
|
||||
Effect.orElseSucceed(() => environment.GH_HOST ?? "github.com"),
|
||||
)).toLowerCase();
|
||||
const locator = parseGitHubRepositorySelector(input.repository, fallbackHost);
|
||||
if (locator === null) return yield* failure("Repositories are named owner/name.");
|
||||
return repositoryCloneUrls(yield* readRepository(locator));
|
||||
}).pipe(
|
||||
Effect.mapError(
|
||||
providerError("getRepositoryCloneUrls", input.cwd, { repository: input.repository }),
|
||||
),
|
||||
),
|
||||
createRepository: (input) =>
|
||||
github.createRepository(input).pipe(
|
||||
Effect.gen(function* () {
|
||||
const locator = parseGitHubRepositorySelector(
|
||||
input.repository,
|
||||
(environment.GH_HOST ?? "github.com").toLowerCase(),
|
||||
);
|
||||
const viewer = locator === null ? null : yield* readViewerLogin(locator.host);
|
||||
const owner = locator?.owner ?? viewer;
|
||||
const name = locator?.name ?? input.repository.trim();
|
||||
const host = locator?.host ?? (environment.GH_HOST?.trim().toLowerCase() || "github.com");
|
||||
const isViewer = viewer !== null && owner?.toLowerCase() === viewer.toLowerCase();
|
||||
const response = yield* rest(
|
||||
{
|
||||
host,
|
||||
operation: "createRepository",
|
||||
method: "POST",
|
||||
path:
|
||||
isViewer || owner === null ? "user/repos" : `orgs/${encodeURIComponent(owner)}/repos`,
|
||||
body: { name, private: input.visibility === "private" },
|
||||
},
|
||||
// GitHub answers 404 for an organization the account cannot create repositories in.
|
||||
`No organization named ${owner ?? "that"} that this account can create repositories in.`,
|
||||
);
|
||||
const decoded = decodeRawRepository(response.body);
|
||||
if (Result.isFailure(decoded)) {
|
||||
return yield* failure("GitHub returned an invalid repository.", decoded.failure);
|
||||
}
|
||||
return repositoryCloneUrls(decoded.success);
|
||||
}).pipe(
|
||||
Effect.mapError(
|
||||
(error) =>
|
||||
new SourceControlProviderError({
|
||||
provider: "github",
|
||||
operation: "createRepository",
|
||||
cwd: input.cwd,
|
||||
repository: SourceControlProvider.transportSafeSourceControlErrorValue(
|
||||
input.repository,
|
||||
),
|
||||
detail: error.message,
|
||||
cause: error,
|
||||
}),
|
||||
providerError("createRepository", input.cwd, { repository: input.repository }),
|
||||
),
|
||||
),
|
||||
getDefaultBranch: (input) =>
|
||||
github
|
||||
.getDefaultBranch({
|
||||
...input,
|
||||
...(input.context === undefined
|
||||
? {}
|
||||
: { rateLimitHost: new URL(input.context.provider.baseUrl).host }),
|
||||
})
|
||||
.pipe(
|
||||
Effect.mapError(
|
||||
(error) =>
|
||||
new SourceControlProviderError({
|
||||
provider: "github",
|
||||
operation: "getDefaultBranch",
|
||||
cwd: input.cwd,
|
||||
detail: error.message,
|
||||
cause: error,
|
||||
}),
|
||||
),
|
||||
),
|
||||
Effect.gen(function* () {
|
||||
const locator = yield* resolveRepository({
|
||||
cwd: input.cwd,
|
||||
host: contextHost(input.context),
|
||||
});
|
||||
const repository = yield* readRepository(locator);
|
||||
const branch = repository.default_branch?.trim() ?? "";
|
||||
return branch.length > 0 ? branch : null;
|
||||
}).pipe(Effect.mapError(providerError("getDefaultBranch", input.cwd))),
|
||||
checkoutChangeRequest: (input) =>
|
||||
github.checkoutPullRequest(input).pipe(
|
||||
checkoutPullRequest(input).pipe(
|
||||
Effect.mapError(
|
||||
(error) =>
|
||||
new SourceControlProviderError({
|
||||
provider: "github",
|
||||
operation: "checkoutChangeRequest",
|
||||
cwd: input.cwd,
|
||||
reference: SourceControlProvider.transportSafeSourceControlErrorValue(
|
||||
input.reference,
|
||||
),
|
||||
detail: error.message,
|
||||
cause: error,
|
||||
}),
|
||||
providerError("checkoutChangeRequest", input.cwd, { reference: input.reference }),
|
||||
),
|
||||
),
|
||||
});
|
||||
|
||||
Reference in new issue
Block a user