Skip to content

Commit d47779b

Browse files
fix(github): address checkout credential review findings
1 parent b198850 commit d47779b

3 files changed

Lines changed: 34 additions & 30 deletions

File tree

‎apps/server/src/assets/GitHubMediaFetch.ts‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,12 @@ const SVG_CONTENT_TYPE = "image/svg+xml";
5050
// body cannot run script against this origin.
5151
const SVG_CONTENT_SECURITY_POLICY = "default-src 'none'; style-src 'unsafe-inline'; sandbox";
5252

53-
const githubToken = GitHubCredentials.GitHubCredentials.pipe(
54-
Effect.flatMap((credentials) => credentials.get("github.com")),
55-
Effect.map((credential) => credential.token),
56-
Effect.orElseSucceed(() => null),
57-
);
53+
const githubToken = (cwd: string) =>
54+
GitHubCredentials.GitHubCredentials.pipe(
55+
Effect.flatMap((credentials) => credentials.get("github.com", cwd)),
56+
Effect.map((credential) => credential.token),
57+
Effect.orElseSucceed(() => null),
58+
);
5859

5960
const fetchFollowingRedirects = Effect.fn("GitHubMediaFetch.fetchFollowingRedirects")(function* (
6061
url: string,
@@ -92,7 +93,7 @@ export const githubMediaResponse = Effect.fn("GitHubMediaFetch.githubMediaRespon
9293
requestHeaders: Record<string, string | undefined>,
9394
) {
9495
// Both media hosts are served by github.com's account, which is the host `gh` stores it under.
95-
const token = yield* githubToken;
96+
const token = yield* githubToken(asset.cwd);
9697
const forwarded: Record<string, string> = {};
9798
for (const name of FORWARDED_REQUEST_HEADERS) {
9899
const value = requestHeaders[name];

‎packages/source-control-github/src/server/GitHubCredentials.ts‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import * as Hex from "effect/encoding/Hex";
77
import * as Exit from "effect/Exit";
88
import * as Layer from "effect/Layer";
99
import * as Option from "effect/Option";
10-
import * as PlatformError from "effect/PlatformError";
1110
import * as Redacted from "effect/Redacted";
1211
import * as Schema from "effect/Schema";
1312

1413
import * as HostProcess from "@t3tools/shared/HostProcess";
1514

1615
import * as SourceControlHost from "@t3tools/source-control-core/server/SourceControlHost";
16+
import { isMissingExecutable } from "@t3tools/source-control-core/server/discovery";
1717
import { readSourceControlHostSettings } from "@t3tools/source-control-core/client/definition";
1818
import type { ServerSettings } from "@t3tools/contracts";
1919

@@ -151,9 +151,7 @@ export const make = Effect.gen(function* () {
151151
})
152152
.pipe(
153153
Effect.mapError((error) =>
154-
error._tag === "VcsProcessSpawnError" &&
155-
error.cause instanceof PlatformError.PlatformError &&
156-
error.cause.reason._tag === "NotFound"
154+
isMissingExecutable(error)
157155
? new GitHubCliMissingError({ host })
158156
: // gh exits non-zero with "no oauth token" when it has no login for the host.
159157
error._tag === "VcsProcessExitError"
@@ -209,7 +207,7 @@ export const make = Effect.gen(function* () {
209207
});
210208

211209
const cache = yield* Cache.makeWith(lookup, {
212-
capacity: 32,
210+
capacity: 512,
213211
timeToLive: (exit) =>
214212
Exit.isSuccess(exit)
215213
? TOKEN_TTL

‎packages/source-control-github/src/server/GitHubRest.test.ts‎

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -317,29 +317,34 @@ describe("GitHubCredentials", () => {
317317
Layer.provide(NodeCrypto.layer),
318318
);
319319

320-
it.effect("fails with GitHubCliMissingError when gh is not on PATH", () =>
321-
Effect.gen(function* () {
322-
const credentials = yield* GitHubCredentials.GitHubCredentials;
323-
const error = yield* Effect.flip(credentials.get("git.acme.internal"));
324-
expect(error._tag).toBe("GitHubCliMissingError");
325-
}).pipe(
326-
Effect.provide(
327-
credentialsWith(() =>
328-
Effect.fail(
329-
new VcsProcessSpawnError({
330-
operation: "GitHubCredentials.get",
331-
command: "gh",
332-
cwd: "/",
333-
cause: PlatformError.systemError({
334-
_tag: "NotFound",
335-
module: "ChildProcess",
336-
method: "spawn",
320+
it.effect.each([
321+
{ module: "ChildProcess", method: "spawn", expected: "GitHubCliMissingError" },
322+
{ module: "FileSystem", method: "access", expected: "GitHubCliFailedError" },
323+
] as const)(
324+
"classifies a missing $module resource as $expected",
325+
({ module, method, expected }) =>
326+
Effect.gen(function* () {
327+
const credentials = yield* GitHubCredentials.GitHubCredentials;
328+
const error = yield* Effect.flip(credentials.get("git.acme.internal", "/repo"));
329+
expect(error._tag).toBe(expected);
330+
}).pipe(
331+
Effect.provide(
332+
credentialsWith(() =>
333+
Effect.fail(
334+
new VcsProcessSpawnError({
335+
operation: "GitHubCredentials.get",
336+
command: "gh",
337+
cwd: "/repo",
338+
cause: PlatformError.systemError({
339+
_tag: "NotFound",
340+
module,
341+
method,
342+
}),
337343
}),
338-
}),
344+
),
339345
),
340346
),
341347
),
342-
),
343348
);
344349

345350
it.effect("does not cache a gh failure that is not about signing in", () => {

0 commit comments

Comments
 (0)