Skip to content

Commit 89cb812

Browse files
LannyBoseclaude
andcommitted
fix(server): pull requests load for credentials that cannot read /user
Before any pull request read, T3 checks who the credential belongs to with REST GET /user. A GitHub App installation token is refused there with 403 "Resource not accessible by integration", so the pull request list and watches fail even though every GraphQL read they make would succeed. When /user answers a plain 403, ask GraphQL `viewer { databaseId login }` instead. Rate limits, 401s, and other failures behave as before, and credentials that can read /user make the same single request. Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
1 parent 511d20c commit 89cb812

2 files changed

Lines changed: 65 additions & 4 deletions

File tree

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3278,6 +3278,36 @@ layer("GitHubPullRequestApi.layer", (it) => {
32783278
}),
32793279
);
32803280

3281+
it.effect("asks GraphQL who the account is when it may not read /user", () =>
3282+
Effect.gen(function* () {
3283+
mockedExecute
3284+
.mockReturnValueOnce(
3285+
Effect.fail(
3286+
new GitHubApi.GitHubApiResponseError({
3287+
host: "github.app-installation.test",
3288+
operation: "getRoutingIdentity",
3289+
status: 403,
3290+
githubErrors: ["Resource not accessible by integration"],
3291+
}),
3292+
),
3293+
)
3294+
.mockReturnValueOnce(
3295+
Effect.succeed(output('{"data":{"viewer":{"databaseId":789,"login":"acme-app[bot]"}}}')),
3296+
);
3297+
const cli = yield* GitHubPullRequestApi.GitHubPullRequestApi;
3298+
3299+
expect(
3300+
yield* cli.getRoutingIdentity({ cwd: "/w", host: "github.app-installation.test" }),
3301+
).toEqual({ accountId: "789", viewer: "acme-app[bot]" });
3302+
expect(callAt(0)).toMatchObject({ kind: "rest", path: "user" });
3303+
expect(callAt(1)).toMatchObject({
3304+
kind: "graphql",
3305+
host: "github.app-installation.test",
3306+
operation: "getRoutingIdentity",
3307+
});
3308+
}),
3309+
);
3310+
32813311
it.effect("reuses verified credentials offline and refuses an unverified replacement", () =>
32823312
Effect.gen(function* () {
32833313
const cli = yield* GitHubPullRequestApi.GitHubPullRequestApi;

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

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1124,6 +1124,15 @@ export const make = Effect.gen(function* () {
11241124
}),
11251125
),
11261126
);
1127+
const decodeViewerIdentity = Schema.decodeUnknownEffect(
1128+
Schema.fromJsonString(
1129+
Schema.Struct({
1130+
data: Schema.Struct({
1131+
viewer: Schema.Struct({ databaseId: PositiveInt, login: TrimmedNonEmptyString }),
1132+
}),
1133+
}),
1134+
),
1135+
);
11271136
const captureVerifiedCredential = Effect.fn("GitHubPullRequestApi.captureVerifiedCredential")(
11281137
function* (input: { readonly cwd: string; readonly host: string }) {
11291138
const unavailable = () => new GitHubViewerLoginUnavailableError({ cwd: input.cwd });
@@ -1148,15 +1157,37 @@ export const make = Effect.gen(function* () {
11481157
if (cached !== undefined && now - cached.at < 10 * 60_000)
11491158
return { ...credential, ...cached.value };
11501159
// Pin this read so an auth switch cannot poison its cache entry.
1151-
const response = yield* api
1160+
const identity = yield* api
11521161
.rest({ host, operation: "getRoutingIdentity", path: "user", allowReserve: true })
11531162
.pipe(
1163+
Effect.flatMap((response) =>
1164+
decodeRoutingIdentity(response.body).pipe(Effect.mapError(unavailable)),
1165+
),
1166+
// A GitHub App's installation token may not read `/user`, but GraphQL still
1167+
// answers who it is.
1168+
Effect.catchIf(
1169+
(error) => error._tag === "GitHubApiResponseError" && error.status === 403,
1170+
() =>
1171+
api
1172+
.graphql({
1173+
host,
1174+
operation: "getRoutingIdentity",
1175+
query: "query { viewer { databaseId login } }",
1176+
allowReserve: true,
1177+
})
1178+
.pipe(
1179+
Effect.flatMap((body) =>
1180+
decodeViewerIdentity(body).pipe(Effect.mapError(unavailable)),
1181+
),
1182+
Effect.map(({ data }) => ({
1183+
id: data.viewer.databaseId,
1184+
login: data.viewer.login,
1185+
})),
1186+
),
1187+
),
11541188
Effect.provideService(GitHubApi.PinnedGitHubCredential, credential),
11551189
Effect.provideService(SourceControlRateLimit.CredentialScope, key),
11561190
);
1157-
const identity = yield* decodeRoutingIdentity(response.body).pipe(
1158-
Effect.mapError(unavailable),
1159-
);
11601191
const value = { accountId: String(identity.id), viewer: identity.login };
11611192
if (routingIdentities.size >= 128)
11621193
routingIdentities.delete(routingIdentities.keys().next().value!);

0 commit comments

Comments
 (0)