Skip to content

Commit bda1039

Browse files
committed
feat(source-control): report expired Tangled authentication
1 parent 47a346a commit bda1039

5 files changed

Lines changed: 68 additions & 14 deletions

File tree

‎packages/source-control-tangled/src/server/TangledCli.ts‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ export class TangledCliError extends Schema.TaggedError<TangledCliError>()("Tang
2828
/** `tg auth status --json` names the signed-in account, and nothing else when logged out. */
2929
export const TangledAuthStatusSchema = Schema.Struct({
3030
authenticated: Schema.Boolean,
31+
status: Schema.optional(Schema.String),
3132
did: Schema.optional(Schema.String),
3233
handle: Schema.optional(Schema.String),
3334
});
@@ -161,10 +162,14 @@ export const make = Effect.gen(function* () {
161162
})
162163
.pipe(Effect.mapError((cause: VcsError) => fromVcsError(input.cwd, cause)));
163164
if (result.exitCode !== 0) {
165+
const detail = tangledErrorMessage(result) ?? `tg ${input.args[0] ?? ""} failed.`;
166+
const authentication = /invalid_grant|not authenticated|not logged in|session expired/iu.test(
167+
detail,
168+
);
164169
return yield* new TangledCliError({
165170
cwd: input.cwd,
166-
reason: "failed",
167-
detail: tangledErrorMessage(result) ?? `tg ${input.args[0] ?? ""} failed.`,
171+
reason: authentication ? "authentication" : "failed",
172+
detail: authentication ? `${detail} Run \`tg auth login\` and retry.` : detail,
168173
});
169174
}
170175
return result.stdout;

‎packages/source-control-tangled/src/server/TangledPullRequestProvider.test.ts‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,33 @@ import { HttpClient, HttpClientResponse } from "effect/http";
55
import * as Host from "@t3tools/source-control-testing/TestSourceControlHost";
66
import * as Cli from "./TangledCli.ts";
77
import * as Provider from "./TangledPullRequestProvider.ts";
8+
import { ChildProcessSpawner } from "effect/process";
9+
10+
it.effect("preserves OAuth refresh failures as an authentication error", () => {
11+
const host = Host.layer({
12+
process: {
13+
run: () =>
14+
Effect.succeed(
15+
Host.processOutput("", {
16+
exitCode: ChildProcessSpawner.ExitCode(1),
17+
stderr: "Error: failed to refresh OAuth tokens: token refresh failed: invalid_grant",
18+
}),
19+
),
20+
},
21+
});
22+
const client = HttpClient.make(() => Effect.die("No HTTP request is expected"));
23+
return Effect.gen(function* () {
24+
const provider = yield* Provider.make;
25+
const error = yield* provider
26+
.getViewer({ cwd: "/repo", host: "tangled.org" })
27+
.pipe(Effect.flip);
28+
assert.strictEqual(error.reason, "unauthenticated");
29+
assert.match(error.detail, /tg auth login/u);
30+
}).pipe(
31+
Effect.provide(Cli.layer.pipe(Layer.provideMerge(host))),
32+
Effect.provideService(HttpClient.HttpClient, client),
33+
);
34+
});
835

936
it.effect("reads a closed PR's activity and details through its list number and record key", () => {
1037
const calls: Array<ReadonlyArray<string>> = [];

‎packages/source-control-tangled/src/server/TangledPullRequestProvider.ts‎

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
type PullRequestProviderApi,
1919
} from "@t3tools/source-control-core/server/PullRequestProvider";
2020

21-
import { isTangledCliError, TangledAuthStatusSchema, TangledCli } from "./TangledCli.ts";
21+
import { TangledAuthStatusSchema, TangledCli, type TangledCliError } from "./TangledCli.ts";
2222
import {
2323
numberPulls,
2424
offsetFromRows,
@@ -61,8 +61,22 @@ export const make = Effect.gen(function* () {
6161
const cli = yield* TangledCli;
6262
const httpClient = yield* HttpClient.HttpClient;
6363

64-
const failure = (operation: string, detail: string, reason: "failed" | "not-found" = "failed") =>
65-
new PullRequestProviderError({ provider: KIND, operation, reason, detail });
64+
const failure = (
65+
operation: string,
66+
detail: string,
67+
reason: PullRequestProviderError["reason"] = "failed",
68+
) => new PullRequestProviderError({ provider: KIND, operation, reason, detail });
69+
70+
const cliFailure = (operation: string, cause: TangledCliError) =>
71+
failure(
72+
operation,
73+
cause.detail,
74+
cause.reason === "authentication"
75+
? "unauthenticated"
76+
: cause.reason === "missing-cli"
77+
? "missing-tool"
78+
: "failed",
79+
);
6680

6781
const unsupported = (operation: string, detail: string) => () =>
6882
Effect.fail(failure(operation, detail));
@@ -81,12 +95,7 @@ export const make = Effect.gen(function* () {
8195
})
8296
.pipe(
8397
Effect.map((answer) => numberPulls(answer.items)),
84-
Effect.mapError((cause) =>
85-
failure(
86-
input.operation,
87-
isTangledCliError(cause) ? cause.detail : "Tangled could not list pull requests.",
88-
),
89-
),
98+
Effect.mapError((cause) => cliFailure(input.operation, cause)),
9099
);
91100
});
92101

@@ -209,7 +218,7 @@ export const make = Effect.gen(function* () {
209218
args: ["pr", "view", entry.pull.rkey, "--repo", input.repository],
210219
schema: TangledPullViewSchema,
211220
})
212-
.pipe(Effect.mapError((cause) => failure(input.operation, cause.detail)));
221+
.pipe(Effect.mapError((cause) => cliFailure(input.operation, cause)));
213222
return {
214223
pull: toChangeRequest(
215224
{ ...entry, pull: { ...entry.pull, ...detail } },
@@ -234,7 +243,7 @@ export const make = Effect.gen(function* () {
234243
})
235244
.pipe(
236245
Effect.map((status) => status.handle ?? status.did ?? ""),
237-
Effect.mapError((cause) => failure("getViewer", cause.detail)),
246+
Effect.mapError((cause) => cliFailure("getViewer", cause)),
238247
),
239248

240249
listChangeRequests: (input) =>

‎packages/source-control-tangled/src/server/TangledSourceControlProvider.test.ts‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,15 @@ const serve =
6767
);
6868

6969
describe("TangledSourceControlProvider.parseAuth", () => {
70+
it("reports an expired saved session as unauthenticated", () => {
71+
const expired = TangledSourceControlProvider.parseAuth({
72+
stdout: encodeJson({ authenticated: true, status: "expired", handle: "noz.am" }),
73+
stderr: "",
74+
exitCode: ChildProcessSpawner.ExitCode(0),
75+
});
76+
assert.strictEqual(expired.status, "unauthenticated");
77+
});
78+
7079
it("reads the signed-in account from tg auth status", () => {
7180
const signedIn = TangledSourceControlProvider.parseAuth({
7281
stdout: encodeJson({ authenticated: true, handle: "alice.example", did: "did:plc:abc" }),

‎packages/source-control-tangled/src/server/TangledSourceControlProvider.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ export function isTangledHostname(hostname: string): boolean {
4242
/** `tg auth status --json` names the account and the DID when one is signed in. */
4343
export function parseAuth(input: SourceControlAuthProbeInput) {
4444
const decoded = decodeJsonResult(TangledAuthStatusSchema)(input.stdout);
45-
if (decoded._tag === "Success" && decoded.success.authenticated) {
45+
if (
46+
decoded._tag === "Success" &&
47+
decoded.success.authenticated &&
48+
decoded.success.status !== "expired"
49+
) {
4650
return providerAuth({
4751
status: "authenticated",
4852
account: decoded.success.handle ?? decoded.success.did,

0 commit comments

Comments
 (0)