Skip to content

Commit aba872f

Browse files
refactor(server): GitHub transport errors say it in message, hash with effect/Crypto, read the host env reference
The credential and API errors drop the separate detail getter; the sentence lives in message. The credential fingerprint is a SHA-256 from effect/Crypto, and the env token and gh's working directory come from HostProcessEnvironment and HostProcessWorkingDirectory instead of globalThis.process. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 740b93f commit aba872f

3 files changed

Lines changed: 38 additions & 54 deletions

File tree

‎apps/server/src/sourceControl/GitHubApi.test.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { describe, expect, it } from "@effect/vitest";
2+
import * as NodeServices from "@effect/platform-node/NodeServices";
23
import * as Effect from "effect/Effect";
34
import * as Layer from "effect/Layer";
45
import * as Redacted from "effect/Redacted";
@@ -223,7 +224,10 @@ describe("GitHubApi", () => {
223224

224225
describe("GitHubCredentials", () => {
225226
const credentialsWith = (run: VcsProcess.VcsProcess["Service"]["run"]) =>
226-
GitHubCredentials.layer.pipe(Layer.provide(Layer.mock(VcsProcess.VcsProcess)({ run })));
227+
GitHubCredentials.layer.pipe(
228+
Layer.provide(Layer.mock(VcsProcess.VcsProcess)({ run })),
229+
Layer.provide(NodeServices.layer),
230+
);
227231

228232
it.effect("fails with GitHubCliMissingError when gh is not on PATH", () =>
229233
Effect.gen(function* () {

‎apps/server/src/sourceControl/GitHubApi.ts‎

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -36,25 +36,17 @@ export class GitHubApiRequestError extends Schema.TaggedError<GitHubApiRequestEr
3636
"GitHubApiRequestError",
3737
{ host: Schema.String, operation: Schema.String, cause: Schema.Defect() },
3838
) {
39-
get detail(): string {
40-
return `Could not reach GitHub at ${this.host}.`;
41-
}
42-
4339
override get message(): string {
44-
return `GitHub API failed in ${this.operation}: ${this.detail}`;
40+
return `Could not reach GitHub at ${this.host}.`;
4541
}
4642
}
4743

4844
export class GitHubApiAuthenticationError extends Schema.TaggedError<GitHubApiAuthenticationError>()(
4945
"GitHubApiAuthenticationError",
5046
{ host: Schema.String, operation: Schema.String },
5147
) {
52-
get detail(): string {
53-
return `GitHub refused the credential for ${this.host}. Run \`gh auth login --hostname ${this.host}\` and retry.`;
54-
}
55-
5648
override get message(): string {
57-
return `GitHub API failed in ${this.operation}: ${this.detail}`;
49+
return `GitHub refused the credential for ${this.host}. Run \`gh auth login --hostname ${this.host}\` and retry.`;
5850
}
5951
}
6052

@@ -66,25 +58,17 @@ export class GitHubApiRateLimitError extends Schema.TaggedError<GitHubApiRateLim
6658
retryAt: Schema.optionalKey(Schema.Finite),
6759
},
6860
) {
69-
get detail(): string {
70-
return "GitHub API rate limit exceeded.";
71-
}
72-
7361
override get message(): string {
74-
return `GitHub API failed in ${this.operation}: ${this.detail}`;
62+
return "GitHub API rate limit exceeded.";
7563
}
7664
}
7765

7866
export class GitHubApiNotFoundError extends Schema.TaggedError<GitHubApiNotFoundError>()(
7967
"GitHubApiNotFoundError",
8068
{ host: Schema.String, operation: Schema.String },
8169
) {
82-
get detail(): string {
83-
return "GitHub could not find the requested resource, or the credential cannot see it.";
84-
}
85-
8670
override get message(): string {
87-
return `GitHub API failed in ${this.operation}: ${this.detail}`;
71+
return "GitHub could not find the requested resource, or the credential cannot see it.";
8872
}
8973
}
9074

@@ -101,15 +85,11 @@ export class GitHubApiResponseError extends Schema.TaggedError<GitHubApiResponse
10185
graphqlErrors: Schema.optionalKey(Schema.Array(Schema.String)),
10286
},
10387
) {
104-
get detail(): string {
88+
override get message(): string {
10589
return this.graphqlErrors !== undefined && this.graphqlErrors.length > 0
10690
? `GitHub returned an error: ${this.graphqlErrors.join("; ")}`
10791
: `GitHub returned HTTP ${this.status}.`;
10892
}
109-
110-
override get message(): string {
111-
return `GitHub API failed in ${this.operation}: ${this.detail}`;
112-
}
11393
}
11494

11595
export type GitHubApiError =
@@ -355,16 +335,14 @@ export const make = Effect.gen(function* () {
355335
}),
356336
{
357337
Ok: () =>
358-
limits
359-
.recordSuccess({ ...key, lease })
360-
.pipe(
361-
Effect.as({
362-
status,
363-
headers,
364-
body: collected.text,
365-
truncated: collected.truncated,
366-
}),
367-
),
338+
limits.recordSuccess({ ...key, lease }).pipe(
339+
Effect.as({
340+
status,
341+
headers,
342+
body: collected.text,
343+
truncated: collected.truncated,
344+
}),
345+
),
368346
RateLimited: () =>
369347
Effect.gen(function* () {
370348
const retryAt = retryAtFrom(headers, yield* Clock.currentTimeMillis);

‎apps/server/src/sourceControl/GitHubCredentials.ts‎

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
import * as Cache from "effect/Cache";
22
import * as Context from "effect/Context";
3+
import * as Crypto from "effect/Crypto";
34
import * as Duration from "effect/Duration";
45
import * as Effect from "effect/Effect";
6+
import * as Hex from "effect/encoding/Hex";
57
import * as Exit from "effect/Exit";
68
import * as Layer from "effect/Layer";
79
import * as PlatformError from "effect/PlatformError";
810
import * as Redacted from "effect/Redacted";
911
import * as Schema from "effect/Schema";
10-
import * as NodeCrypto from "node:crypto";
12+
13+
import { HostProcessEnvironment, HostProcessWorkingDirectory } from "@t3tools/shared/hostProcess";
1114

1215
import * as VcsProcess from "../vcs/VcsProcess.ts";
1316

@@ -32,12 +35,8 @@ export class GitHubCliMissingError extends Schema.TaggedError<GitHubCliMissingEr
3235
"GitHubCliMissingError",
3336
{ host: Schema.String },
3437
) {
35-
get detail(): string {
36-
return `No GitHub credential for ${this.host}: set GH_TOKEN, or install the GitHub CLI and run \`gh auth login\`.`;
37-
}
38-
3938
override get message(): string {
40-
return this.detail;
39+
return `No GitHub credential for ${this.host}: set GH_TOKEN, or install the GitHub CLI and run \`gh auth login\`.`;
4140
}
4241
}
4342

@@ -46,12 +45,8 @@ export class GitHubNotSignedInError extends Schema.TaggedError<GitHubNotSignedIn
4645
"GitHubNotSignedInError",
4746
{ host: Schema.String },
4847
) {
49-
get detail(): string {
50-
return `No GitHub credential for ${this.host}: run \`gh auth login --hostname ${this.host}\`.`;
51-
}
52-
5348
override get message(): string {
54-
return this.detail;
49+
return `No GitHub credential for ${this.host}: run \`gh auth login --hostname ${this.host}\`.`;
5550
}
5651
}
5752

@@ -97,21 +92,28 @@ export function environmentToken(
9792
return null;
9893
}
9994

100-
export function credentialFingerprint(host: string, token: string): string {
101-
return `${host}:${NodeCrypto.createHash("sha256").update(token).digest("hex")}`;
102-
}
103-
10495
/** @public Service construction is part of the canonical Effect module API. */
10596
export const make = Effect.gen(function* () {
10697
const process = yield* VcsProcess.VcsProcess;
98+
const crypto = yield* Crypto.Crypto;
99+
const environment = yield* HostProcessEnvironment;
100+
const workingDirectory = yield* HostProcessWorkingDirectory;
101+
102+
/** `host:sha256(token)`, safe for cache keys and rate-limit scopes. */
103+
const fingerprintOf = (host: string, token: string) =>
104+
crypto.digest("SHA-256", new TextEncoder().encode(token)).pipe(
105+
Effect.map((digest) => `${host}:${Hex.encode(digest)}`),
106+
// Hashing a string in memory has no platform failure worth a typed error.
107+
Effect.orDie,
108+
);
107109

108110
const fromGh = (host: string) =>
109111
process
110112
.run({
111113
operation: "GitHubCredentials.get",
112114
command: "gh",
113115
args: ["auth", "token", "--hostname", host],
114-
cwd: globalThis.process.cwd(),
116+
cwd: workingDirectory,
115117
// Never let gh print the token into a debug log.
116118
env: { GH_DEBUG: "", GH_PROMPT_DISABLED: "1" },
117119
timeoutMs: 10_000,
@@ -132,13 +134,13 @@ export const make = Effect.gen(function* () {
132134
);
133135

134136
const lookup = Effect.fn("GitHubCredentials.lookup")(function* (host: string) {
135-
const fromEnv = environmentToken(host, globalThis.process.env);
137+
const fromEnv = environmentToken(host, environment);
136138
const token = fromEnv ?? (yield* fromGh(host));
137139
return {
138140
host,
139141
token: Redacted.make(token),
140142
source: fromEnv !== null ? "env" : "gh",
141-
fingerprint: credentialFingerprint(host, token),
143+
fingerprint: yield* fingerprintOf(host, token),
142144
} satisfies GitHubCredential;
143145
});
144146

0 commit comments

Comments
 (0)