diff --git a/packages/source-control-github/src/server/GitHubSourceControlProvider.ts b/packages/source-control-github/src/server/GitHubSourceControlProvider.ts index 4abb3d8d6669..5c3708543a4b 100644 --- a/packages/source-control-github/src/server/GitHubSourceControlProvider.ts +++ b/packages/source-control-github/src/server/GitHubSourceControlProvider.ts @@ -305,28 +305,30 @@ export const makeDiscovery = Effect.gen(function* () { } satisfies SourceControlProviderDiscoveryItem); }), // Claim disabled hosts so errors identify the setting. - refineUnknownRemote: ({ context }) => { + refineUnknownRemote: Effect.fn("GitHubSourceControlProvider.refineUnknownRemote")(function* ({ + context, + }) { const url = URL.parse(context.provider.baseUrl); - if (!url?.host) return Effect.succeed(null); + if (!url?.host) return null; // A timeout or keyring failure propagates: the registry does not keep that answer. - return api.credential(url.host).pipe( + const known = yield* api.credential(url.host).pipe( + // Most unknown hosts are not GitHub: only this span records a failure, and only a real one. + Effect.withTracerEnabled(false), Effect.as(true), Effect.catchTags({ GitHubCliMissingError: () => Effect.succeed(false), GitHubHostDisabledError: () => Effect.succeed(true), GitHubNotSignedInError: () => Effect.succeed(false), }), - Effect.map((known) => - known - ? ({ - kind: SourceControlProviderKind.make("github"), - name: "GitHub Self-Hosted", - baseUrl: context.provider.baseUrl, - } as const) - : null, - ), ); - }, + return known + ? ({ + kind: SourceControlProviderKind.make("github"), + name: "GitHub Self-Hosted", + baseUrl: context.provider.baseUrl, + } as const) + : null; + }), } satisfies SourceControlManagedCliDiscoverySpec; }); diff --git a/packages/source-control-github/src/server/gitHubDiscovery.test.ts b/packages/source-control-github/src/server/gitHubDiscovery.test.ts index c1f4304f4b04..9b0b00cd5b68 100644 --- a/packages/source-control-github/src/server/gitHubDiscovery.test.ts +++ b/packages/source-control-github/src/server/gitHubDiscovery.test.ts @@ -1,11 +1,17 @@ -import { ServerSettings, SourceControlProviderKind } from "@t3tools/contracts"; +import { + ServerSettings, + SourceControlProviderKind, + VcsProcessTimeoutError, +} from "@t3tools/contracts"; import * as HostProcess from "@t3tools/shared/HostProcess"; import { assert, it } from "@effect/vitest"; import * as Effect from "effect/Effect"; +import * as Exit from "effect/Exit"; import * as Layer from "effect/Layer"; import * as Option from "effect/Option"; import * as Redacted from "effect/Redacted"; import * as Schema from "effect/Schema"; +import * as Tracer from "effect/Tracer"; import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner"; import * as TestSourceControlHost from "@t3tools/source-control-testing/TestSourceControlHost"; import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost"; @@ -180,3 +186,49 @@ it.effect.each([ assert.include(Option.getOrThrow(item.auth.detail), scenario.detail); }), ); + +it.effect.each([ + { + name: "no failed span when gh is not signed in", + error: (host: string) => new GitHubCredentials.GitHubNotSignedInError({ host }), + failedSpans: [], + }, + { + name: "a failed probe span when the lookup fails for real", + error: (host: string) => + new GitHubCredentials.GitHubCliFailedError({ + host, + cause: new VcsProcessTimeoutError({ + operation: "GitHubCredentials.get", + command: "gh", + cwd: "/repo", + timeoutMs: 10_000, + }), + }), + failedSpans: ["GitHubSourceControlProvider.refineUnknownRemote"], + }, +])("records $name for an unknown host", (scenario) => + Effect.gen(function* () { + const spans: Array = []; + const tracer = Tracer.make({ + span: (options) => { + const span = new Tracer.NativeSpan(options); + spans.push(span); + return span; + }, + }); + const discovery = yield* makeDiscovery((host) => + Effect.fail(scenario.error(host)).pipe(Effect.withSpan("GitHubRest.credential")), + ); + if (discovery.type !== "managed-cli") return assert.fail("expected a managed CLI"); + yield* discovery + .refineUnknownRemote({ cwd: "/repo", context: unknownContext("https://code.example.test") }) + .pipe(Effect.withTracer(tracer), Effect.exit); + assert.deepStrictEqual( + spans.flatMap((span) => + span.status._tag === "Ended" && Exit.isFailure(span.status.exit) ? [span.name] : [], + ), + scenario.failedSpans, + ); + }), +);