feat(server): source control, media and discovery use GitHub's API instead of gh (#16321)
Co-authored-by: Claude Opus 5.5 (1M context) <[email protected]>
This commit is contained in:
1 parent
adc3c9327a
commit
649418f01f
13 files changed
+1403
-1777
No files matched your search
@@ -2,22 +2,33 @@ import * as Schema from "effect/Schema";
|
||||
import * as DateTime from "effect/DateTime";
|
||||
import * as Effect from "effect/Effect";
|
||||
import * as Option from "effect/Option";
|
||||
import { SourceControlProviderError, type ChangeRequest } from "@t3tools/contracts";
|
||||
import * as Result from "effect/Result";
|
||||
import {
|
||||
SourceControlProviderError,
|
||||
type ChangeRequest,
|
||||
type SourceControlProviderDiscoveryItem,
|
||||
} from "@t3tools/contracts";
|
||||
|
||||
import { HostProcessEnvironment } from "@t3tools/shared/hostProcess";
|
||||
|
||||
import * as GitHubApi from "./GitHubApi.ts";
|
||||
import * as GitHubCli from "./GitHubCli.ts";
|
||||
import { findAuthenticatedGitHubAccount, parseGitHubAuthStatus } from "./gitHubAuthStatus.ts";
|
||||
import * as SourceControlProvider from "./SourceControlProvider.ts";
|
||||
import {
|
||||
combinedAuthOutput,
|
||||
firstSafeAuthLine,
|
||||
probeSourceControlProvider,
|
||||
providerAuth,
|
||||
type SourceControlAuthProbeInput,
|
||||
type SourceControlCliDiscoverySpec,
|
||||
type SourceControlManagedCliDiscoverySpec,
|
||||
} from "./SourceControlProviderDiscovery.ts";
|
||||
import * as VcsProcess from "../vcs/VcsProcess.ts";
|
||||
|
||||
const decodeLinkSubject = Schema.decodeUnknownEffect(
|
||||
Schema.fromJsonString(
|
||||
Schema.Struct({ title: Schema.String, body: Schema.NullOr(Schema.String) }),
|
||||
Schema.Struct({ title: Schema.String, body: Schema.optional(Schema.NullOr(Schema.String)) }),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -111,8 +122,70 @@ export const discovery = {
|
||||
"Install the GitHub command-line tool (`gh`) via https://cli.github.com/ or your package manager (for example `brew install gh`).",
|
||||
} satisfies SourceControlCliDiscoverySpec;
|
||||
|
||||
const decodeViewer = Schema.decodeUnknownOption(
|
||||
Schema.fromJsonString(Schema.Struct({ login: Schema.String })),
|
||||
);
|
||||
|
||||
/** The environment variable gh would take a github.com token from, if one is set. */
|
||||
function environmentTokenVariable(environment: NodeJS.ProcessEnv): string | null {
|
||||
return ["GH_TOKEN", "GITHUB_TOKEN"].find((name) => environment[name]?.trim()) ?? null;
|
||||
}
|
||||
|
||||
/**
|
||||
* GitHub is usable with a token from the environment or with `gh` to hand one over. An
|
||||
* environment token is checked against the API, since `gh auth status` may not know it.
|
||||
*/
|
||||
export const makeDiscovery = Effect.gen(function* () {
|
||||
const api = yield* GitHubApi.GitHubApi;
|
||||
const process = yield* VcsProcess.VcsProcess;
|
||||
const environment = yield* HostProcessEnvironment;
|
||||
return {
|
||||
type: "managed-cli",
|
||||
kind: discovery.kind,
|
||||
label: discovery.label,
|
||||
installHint: discovery.installHint,
|
||||
probe: Effect.fn("GitHubSourceControlProvider.discovery")(function* (cwd: string) {
|
||||
const cli = yield* probeSourceControlProvider({ cwd, process, spec: discovery });
|
||||
const variable = environmentTokenVariable(environment);
|
||||
if (variable === null) return cli;
|
||||
const viewer = yield* api
|
||||
.rest({ host: "github.com", operation: "discovery", path: "user" })
|
||||
.pipe(Effect.result);
|
||||
const login = Result.isSuccess(viewer)
|
||||
? Option.getOrUndefined(decodeViewer(viewer.success.body))?.login
|
||||
: undefined;
|
||||
return {
|
||||
...cli,
|
||||
status: "available" as const,
|
||||
auth:
|
||||
login !== undefined
|
||||
? providerAuth({
|
||||
status: "authenticated",
|
||||
account: login,
|
||||
host: "github.com",
|
||||
detail: `Using the token in ${variable} from the server environment.`,
|
||||
})
|
||||
: Result.isFailure(viewer) && viewer.failure._tag !== "GitHubApiAuthenticationError"
|
||||
? // Only a refusal says the token is bad; a network error or a pause says nothing.
|
||||
providerAuth({
|
||||
status: "unknown",
|
||||
host: "github.com",
|
||||
detail: `Could not check the token in ${variable}: ${viewer.failure.message}`,
|
||||
})
|
||||
: providerAuth({
|
||||
status: "unauthenticated",
|
||||
host: "github.com",
|
||||
detail: `GitHub refused the token in ${variable}. Replace it, or unset it to use \`gh auth login\`.`,
|
||||
}),
|
||||
} satisfies SourceControlProviderDiscoveryItem;
|
||||
}),
|
||||
refineUnknownRemote: () => Effect.succeed(null),
|
||||
} satisfies SourceControlManagedCliDiscoverySpec;
|
||||
});
|
||||
|
||||
export const make = Effect.gen(function* () {
|
||||
const github = yield* GitHubCli.GitHubCli;
|
||||
const api = yield* GitHubApi.GitHubApi;
|
||||
|
||||
const listChangeRequests: SourceControlProvider.SourceControlProvider["Service"]["listChangeRequests"] =
|
||||
(input) => {
|
||||
@@ -138,7 +211,7 @@ export const make = Effect.gen(function* () {
|
||||
reference: SourceControlProvider.transportSafeSourceControlErrorValue(
|
||||
input.headSelector,
|
||||
),
|
||||
detail: error.detail,
|
||||
detail: error.message,
|
||||
cause: error,
|
||||
}),
|
||||
),
|
||||
@@ -177,7 +250,7 @@ export const make = Effect.gen(function* () {
|
||||
reference: SourceControlProvider.transportSafeSourceControlErrorValue(
|
||||
input.headSelector,
|
||||
),
|
||||
detail: error.detail,
|
||||
detail: error.message,
|
||||
cause: error,
|
||||
}),
|
||||
),
|
||||
@@ -188,15 +261,15 @@ export const make = Effect.gen(function* () {
|
||||
input: { readonly cwd: string; readonly url: URL },
|
||||
endpoint: string,
|
||||
) {
|
||||
const result = yield* github
|
||||
.execute({
|
||||
cwd: input.cwd,
|
||||
args: ["api", "--hostname", input.url.host, endpoint, "--jq", "{title, body}"],
|
||||
env: { GH_PROMPT_DISABLED: "1" },
|
||||
timeoutMs: 3_000,
|
||||
maxOutputBytes: 32_000,
|
||||
const result = yield* api
|
||||
.rest({
|
||||
host: input.url.host,
|
||||
operation: "resolveLink",
|
||||
path: endpoint,
|
||||
maxResponseBytes: 1_000_000,
|
||||
})
|
||||
.pipe(
|
||||
Effect.timeout("3 seconds"),
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new SourceControlProviderError({
|
||||
@@ -208,7 +281,7 @@ export const make = Effect.gen(function* () {
|
||||
}),
|
||||
),
|
||||
);
|
||||
const subject = yield* decodeLinkSubject(result.stdout).pipe(
|
||||
const subject = yield* decodeLinkSubject(result.body).pipe(
|
||||
Effect.mapError(
|
||||
(cause) =>
|
||||
new SourceControlProviderError({
|
||||
@@ -220,7 +293,7 @@ export const make = Effect.gen(function* () {
|
||||
}),
|
||||
),
|
||||
);
|
||||
return { title: subject.title, body: subject.body };
|
||||
return { title: subject.title, body: subject.body ?? null };
|
||||
});
|
||||
|
||||
return SourceControlProvider.SourceControlProvider.of({
|
||||
@@ -255,7 +328,7 @@ export const make = Effect.gen(function* () {
|
||||
reference: SourceControlProvider.transportSafeSourceControlErrorValue(
|
||||
input.reference,
|
||||
),
|
||||
detail: error.detail,
|
||||
detail: error.message,
|
||||
cause: error,
|
||||
}),
|
||||
),
|
||||
@@ -280,7 +353,7 @@ export const make = Effect.gen(function* () {
|
||||
reference: SourceControlProvider.transportSafeSourceControlErrorValue(
|
||||
input.headSelector,
|
||||
),
|
||||
detail: error.detail,
|
||||
detail: error.message,
|
||||
cause: error,
|
||||
}),
|
||||
),
|
||||
@@ -297,7 +370,7 @@ export const make = Effect.gen(function* () {
|
||||
repository: SourceControlProvider.transportSafeSourceControlErrorValue(
|
||||
input.repository,
|
||||
),
|
||||
detail: error.detail,
|
||||
detail: error.message,
|
||||
cause: error,
|
||||
}),
|
||||
),
|
||||
@@ -314,7 +387,7 @@ export const make = Effect.gen(function* () {
|
||||
repository: SourceControlProvider.transportSafeSourceControlErrorValue(
|
||||
input.repository,
|
||||
),
|
||||
detail: error.detail,
|
||||
detail: error.message,
|
||||
cause: error,
|
||||
}),
|
||||
),
|
||||
@@ -335,7 +408,7 @@ export const make = Effect.gen(function* () {
|
||||
operation: "getDefaultBranch",
|
||||
command: error.command,
|
||||
cwd: input.cwd,
|
||||
detail: error.detail,
|
||||
detail: error.message,
|
||||
cause: error,
|
||||
}),
|
||||
),
|
||||
@@ -352,7 +425,7 @@ export const make = Effect.gen(function* () {
|
||||
reference: SourceControlProvider.transportSafeSourceControlErrorValue(
|
||||
input.reference,
|
||||
),
|
||||
detail: error.detail,
|
||||
detail: error.message,
|
||||
cause: error,
|
||||
}),
|
||||
),
|
||||
|
||||
Reference in new issue
Block a user