mirror of
https://github.com/getpaseo/paseo.git
synced 2026-12-03 21:56:41 +00:00
Compare commits
3
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cb25c6c2ba | ||
|
|
277324c3b8 | ||
|
|
aa9f5704ff |
@@ -15,6 +15,7 @@ import pino from "pino";
|
||||
import { afterEach, expect, test, vi } from "vitest";
|
||||
import {
|
||||
getCheckoutStatus as readCheckoutStatus,
|
||||
getPullRequestStatus as readPullRequestStatus,
|
||||
type CheckoutSnapshotFacts,
|
||||
type CheckoutStatusGit,
|
||||
} from "../utils/checkout-git.js";
|
||||
@@ -1386,16 +1387,19 @@ test("a parent lookup that finishes during a refresh keeps its answer", async ()
|
||||
logger: pino({ enabled: false }),
|
||||
paseoHome: path.join(path.dirname(repo), "home"),
|
||||
deps: {
|
||||
getPullRequestStatus: async (...args) => {
|
||||
if (holdNextStatusLoad) {
|
||||
holdNextStatusLoad = false;
|
||||
statusLoadStarted.resolve();
|
||||
await releaseStatusLoad.promise;
|
||||
}
|
||||
return readPullRequestStatus(...args);
|
||||
},
|
||||
forgeOverrides: {
|
||||
github: createGitHubService({
|
||||
resolveGhPath: async () => "gh",
|
||||
resolveRepoHost: async () => null,
|
||||
runner: async (args) => {
|
||||
if (args[0] === "auth" && args[1] === "status" && holdNextStatusLoad) {
|
||||
holdNextStatusLoad = false;
|
||||
statusLoadStarted.resolve();
|
||||
await releaseStatusLoad.promise;
|
||||
}
|
||||
if (args[0] === "repo" && args[1] === "view" && args[2] === "fork-owner/repo") {
|
||||
repositoryViews += 1;
|
||||
if (repositoryViews === 1) {
|
||||
@@ -1417,7 +1421,11 @@ test("a parent lookup that finishes during a refresh keeps its answer", async ()
|
||||
},
|
||||
},
|
||||
});
|
||||
cleanup.push(() => service.dispose());
|
||||
cleanup.push(async () => {
|
||||
releaseLookup.resolve();
|
||||
releaseStatusLoad.resolve();
|
||||
await service.dispose();
|
||||
});
|
||||
|
||||
await service.getSnapshot(repo, { force: true, reason: "forge-web-test" });
|
||||
await lookupStarted.promise;
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
import { execFile } from "node:child_process";
|
||||
import { mkdir, mkdtemp, rm, writeFile } from "node:fs/promises";
|
||||
import { createServer } from "node:http";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
import { promisify } from "node:util";
|
||||
import { expect, it, vi } from "vitest";
|
||||
import { findExecutable } from "../executable-resolution/executable-resolution.js";
|
||||
import {
|
||||
createGitHubService,
|
||||
GitHubAuthenticationError,
|
||||
probeGitHubHost,
|
||||
} from "./github-service.js";
|
||||
|
||||
const exec = promisify(execFile);
|
||||
const gh = await findExecutable("gh");
|
||||
|
||||
it.skipIf(!gh)("authenticates an Enterprise workspace with an expired inactive login", async () => {
|
||||
if (!gh) throw new Error("Missing GitHub CLI");
|
||||
const root = await mkdtemp(join(tmpdir(), "paseo-enterprise-auth-"));
|
||||
const host = "github.localhost";
|
||||
let activeLoginValid = true;
|
||||
const api = createServer((request, response) => {
|
||||
response.setHeader("Content-Type", "application/json");
|
||||
if (!activeLoginValid || request.headers.authorization !== "token active-test-token") {
|
||||
response.writeHead(401).end(JSON.stringify({ message: "Bad credentials" }));
|
||||
return;
|
||||
}
|
||||
if (request.method === "POST") {
|
||||
const emptyPage = { nodes: [], totalCount: 0, pageInfo: { hasNextPage: false } };
|
||||
request.resume();
|
||||
response.end(
|
||||
JSON.stringify({
|
||||
data: {
|
||||
repository: {
|
||||
hasIssuesEnabled: true,
|
||||
issues: emptyPage,
|
||||
pullRequests: emptyPage,
|
||||
},
|
||||
},
|
||||
}),
|
||||
);
|
||||
return;
|
||||
}
|
||||
response.end(JSON.stringify({ login: "active" }));
|
||||
});
|
||||
try {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
api.once("error", reject);
|
||||
api.listen(0, "127.0.0.1", resolve);
|
||||
});
|
||||
const address = api.address();
|
||||
if (!address || typeof address === "string") {
|
||||
throw new Error("Missing test API port");
|
||||
}
|
||||
const config = join(root, "gh");
|
||||
const cwd = join(root, "repo");
|
||||
await mkdir(config);
|
||||
await mkdir(cwd);
|
||||
await writeFile(
|
||||
join(config, "hosts.yml"),
|
||||
`${host}:
|
||||
user: active
|
||||
oauth_token: active-test-token
|
||||
users:
|
||||
active:
|
||||
oauth_token: active-test-token
|
||||
expired:
|
||||
oauth_token: expired-test-token
|
||||
`,
|
||||
);
|
||||
vi.stubEnv("GH_CONFIG_DIR", config);
|
||||
vi.stubEnv("HTTP_PROXY", `http://127.0.0.1:${address.port}`);
|
||||
vi.stubEnv("NO_PROXY", "");
|
||||
vi.stubEnv("GH_HOST", undefined);
|
||||
for (const token of [
|
||||
"GH_TOKEN",
|
||||
"GITHUB_TOKEN",
|
||||
"GH_ENTERPRISE_TOKEN",
|
||||
"GITHUB_ENTERPRISE_TOKEN",
|
||||
]) {
|
||||
vi.stubEnv(token, undefined);
|
||||
}
|
||||
|
||||
await exec("git", ["init", "--quiet"], { cwd });
|
||||
await exec("git", ["remote", "add", "origin", `https://${host}/org/repo.git`], { cwd });
|
||||
const activeUser = await exec(gh, ["api", "--hostname", host, "user"], { cwd });
|
||||
expect(JSON.parse(activeUser.stdout)).toEqual({ login: "active" });
|
||||
|
||||
const service = createGitHubService();
|
||||
await expect(service.isAuthenticated({ cwd })).resolves.toBe(true);
|
||||
await expect(probeGitHubHost(host)).resolves.toBe(true);
|
||||
await expect(service.listPullRequests({ cwd })).resolves.toEqual([]);
|
||||
await expect(service.listIssues({ cwd })).resolves.toEqual([]);
|
||||
|
||||
activeLoginValid = false;
|
||||
await expect(createGitHubService().isAuthenticated({ cwd })).rejects.toBeInstanceOf(
|
||||
GitHubAuthenticationError,
|
||||
);
|
||||
await expect(probeGitHubHost(host)).resolves.toBe(false);
|
||||
} finally {
|
||||
vi.unstubAllEnvs();
|
||||
await new Promise<void>((resolve) => api.close(() => resolve()));
|
||||
await rm(root, { force: true, recursive: true });
|
||||
}
|
||||
});
|
||||
@@ -144,7 +144,7 @@ if [ "$1" = "--version" ]; then
|
||||
echo "gh version test"
|
||||
exit 0
|
||||
fi
|
||||
if [ "$1" = "auth" ] && [ "$2" = "status" ] && [ "$3" = "--hostname" ]; then
|
||||
if [ "$1" = "api" ] && [ "$2" = "--hostname" ] && [ "$5" = "user" ]; then
|
||||
case "$AUTH_STATUS_MODE" in
|
||||
success)
|
||||
exit 0
|
||||
@@ -5072,7 +5072,7 @@ describe("ForgeService", () => {
|
||||
|
||||
expect(readFakeGitHubCliLog(fixture.logPath)).toEqual(
|
||||
expect.arrayContaining([
|
||||
"auth status --hostname github.acme.internal|",
|
||||
"api --hostname github.acme.internal --silent user|",
|
||||
"repo set-default --view|github.acme.internal",
|
||||
"api -X POST repos/acme/repo/pulls -f title=Add thing -f head=feature -f base=main|github.acme.internal",
|
||||
]),
|
||||
@@ -5114,7 +5114,7 @@ describe("ForgeService", () => {
|
||||
|
||||
const log = readFakeGitHubCliLog(fixture.logPath);
|
||||
expect(
|
||||
log.filter((line) => line === "auth status --hostname github.acme.internal|"),
|
||||
log.filter((line) => line === "api --hostname github.acme.internal --silent user|"),
|
||||
).toHaveLength(2);
|
||||
expect(log).toEqual(
|
||||
expect.arrayContaining([
|
||||
@@ -5151,11 +5151,10 @@ describe("ForgeService", () => {
|
||||
).rejects.toThrow(/authentication failed/i);
|
||||
|
||||
const log = readFakeGitHubCliLog(fixture.logPath);
|
||||
expect(log).toContain("auth status --hostname github.acme.internal|");
|
||||
// It threw at host resolution, before running any api/graphql call.
|
||||
expect(log.some((line) => line.startsWith("api ") || line.startsWith("graphql"))).toBe(
|
||||
false,
|
||||
);
|
||||
// Only the host probe ran. No repository command reached GitHub.
|
||||
expect(log.filter((line) => !line.startsWith("--version|"))).toEqual([
|
||||
"api --hostname github.acme.internal --silent user|",
|
||||
]);
|
||||
} finally {
|
||||
fixture.dispose();
|
||||
}
|
||||
@@ -5181,13 +5180,29 @@ describe("ForgeService", () => {
|
||||
).resolves.toEqual({ url: "https://github.acme.internal/acme/repo/pull/7", number: 7 });
|
||||
|
||||
const log = readFakeGitHubCliLog(fixture.logPath);
|
||||
expect(log.some((line) => line.startsWith("auth status --hostname "))).toBe(false);
|
||||
expect(log.some((line) => line.startsWith("api --hostname "))).toBe(false);
|
||||
expect(log.every((line) => line.endsWith("|"))).toBe(true);
|
||||
} finally {
|
||||
fixture.dispose();
|
||||
}
|
||||
});
|
||||
|
||||
it.skipIf(isPlatform("win32"))("checks only the active login for github.com", async () => {
|
||||
vi.useRealTimers();
|
||||
const fixture = createFakeGitHubCliFixture({
|
||||
remoteUrl: "https://github.com/acme/repo.git",
|
||||
authStatusSucceeds: true,
|
||||
});
|
||||
try {
|
||||
await expect(createGitHubService().isAuthenticated({ cwd: fixture.cwd })).resolves.toBe(true);
|
||||
expect(readFakeGitHubCliLog(fixture.logPath)).toContain(
|
||||
"api --hostname github.com --silent user|",
|
||||
);
|
||||
} finally {
|
||||
fixture.dispose();
|
||||
}
|
||||
});
|
||||
|
||||
it("caches definitive null repo host resolutions", async () => {
|
||||
const runner = createRunner(["[]", "[]"]);
|
||||
let hostResolutions = 0;
|
||||
|
||||
@@ -29,7 +29,6 @@ import {
|
||||
ForgeAuthenticationError,
|
||||
ForgeCliMissingError,
|
||||
parseCliJsonOutput,
|
||||
probeHostViaCliAuthStatus,
|
||||
runWithResolvedCliPath,
|
||||
ForgeCommandError,
|
||||
type ForgeCommandFailureParams,
|
||||
@@ -1612,7 +1611,7 @@ export function createGitHubService(options: CreateGitHubServiceOptions = {}): G
|
||||
const authenticating = urls.map((url) => [url, watchedRequests.get(url)] as const);
|
||||
try {
|
||||
await run(
|
||||
["auth", "status", "--hostname", host],
|
||||
githubHostAuthArgs(host),
|
||||
{ cwd: process.cwd(), envOverlay: watchAuthProbeEnv },
|
||||
host,
|
||||
);
|
||||
@@ -2876,7 +2875,8 @@ export function createGitHubService(options: CreateGitHubServiceOptions = {}): G
|
||||
readOptions: input,
|
||||
load: async () => {
|
||||
try {
|
||||
await run(["auth", "status"], { cwd: input.cwd });
|
||||
const host = (await resolveRepoHostCached(input.cwd)) ?? "github.com";
|
||||
await run(githubHostAuthArgs(host), { cwd: input.cwd });
|
||||
return true;
|
||||
} catch (error) {
|
||||
if (isGitHubAuthenticationError(error)) {
|
||||
@@ -3143,28 +3143,30 @@ async function resolveGhPath(): Promise<string | null> {
|
||||
* Cloud github.com short-circuits via the registry's host match before this runs.
|
||||
*/
|
||||
export async function probeGitHubHost(host: string): Promise<boolean> {
|
||||
return probeGitHubAuthStatus(host);
|
||||
try {
|
||||
return await probeGitHubHostAuthentication(host);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async function probeGitHubAuthStatus(host: string): Promise<boolean> {
|
||||
return probeHostViaCliAuthStatus({
|
||||
cli: "gh",
|
||||
host,
|
||||
envOverlay: { ...GITHUB_ENV, GH_PROMPT_DISABLED: "1" },
|
||||
});
|
||||
// gh api uses the active login, just like the repository commands it gates.
|
||||
function githubHostAuthArgs(host: string): string[] {
|
||||
return ["api", "--hostname", host, "--silent", "user"];
|
||||
}
|
||||
|
||||
async function probeGitHubAuthStatusForRouting(host: string): Promise<boolean> {
|
||||
async function probeGitHubHostAuthentication(host: string): Promise<boolean> {
|
||||
const ghPath = await findExecutable("gh");
|
||||
if (!ghPath) {
|
||||
throw new GitHubCliMissingError();
|
||||
}
|
||||
|
||||
const args = ["auth", "status", "--hostname", host];
|
||||
const args = githubHostAuthArgs(host);
|
||||
try {
|
||||
await execCommand(ghPath, args, {
|
||||
envOverlay: { ...GITHUB_ENV, GH_PROMPT_DISABLED: "1" },
|
||||
timeout: CLI_AUTH_PROBE_TIMEOUT_MS,
|
||||
killSignal: "SIGKILL",
|
||||
});
|
||||
return true;
|
||||
} catch (error) {
|
||||
@@ -4455,7 +4457,7 @@ async function resolveGitHubEnterpriseHost(cwd: string): Promise<string | null>
|
||||
if (isGitHubHost(host)) {
|
||||
return null;
|
||||
}
|
||||
if (await probeGitHubAuthStatusForRouting(host)) {
|
||||
if (await probeGitHubHostAuthentication(host)) {
|
||||
return host;
|
||||
}
|
||||
// A non-cloud (Enterprise) host that isn't authenticated must NOT resolve to
|
||||
|
||||
Reference in New Issue
Block a user