Skip to content

Commit 2ff2db7

Browse files
rookmateclaude
andcommitted
fix(source-control): group repositories by the credential they are read with
A rule whose login gh no longer holds gives way to the next choice, so two repositories that name the same unavailable login can be read with different credentials. Grouping by the configured login put them under one viewer and one batched request. The Pull Requests list and GraphQL batches now key on the fingerprint of the credential each repository is actually read with. Mobile also tells a server with no GitHub login to sign in, rather than to add another login. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent c076a27 commit 2ff2db7

7 files changed

Lines changed: 106 additions & 84 deletions

File tree

‎apps/mobile/src/features/settings/SettingsGitHubAccountsRouteScreen.tsx‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,9 @@ function EnvironmentGitHubAccounts({
130130
? "Could not check which GitHub logins this server holds."
131131
: discovery.data === null
132132
? "Checking which GitHub logins this server holds…"
133-
: "This server is signed in to one GitHub login per host. Run gh auth login on the server to add another."}
133+
: (github?.auth.accounts ?? []).some((account) => account.authenticated)
134+
? "This server is signed in to one GitHub login per host. Run gh auth login on the server to add another."
135+
: "This server is not signed in to GitHub. Run gh auth login on the server to sign in."}
134136
</Text>
135137
</SettingsSection>
136138
);

‎apps/server/src/pullRequest/PullRequestService.ts‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,8 @@ export interface SupportedProject {
309309
readonly remote: string;
310310
readonly repositoryKey: string | null;
311311
/**
312-
* The account the repository is read as (the provider's `credentialScope`), empty for the
313-
* host's own. Repositories with one share a viewer and batched reads.
312+
* Which credential the repository is read with (the provider's `credentialScope`), empty for a
313+
* provider with one account per host. Repositories with one share a viewer and batched reads.
314314
*/
315315
readonly credentialScope: string;
316316
}

‎packages/source-control-core/src/server/PullRequestProvider.ts‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,9 +276,9 @@ export interface PullRequestProviderApi {
276276
readonly repositoryKey?: (input: { readonly canonicalKey: string }) => string;
277277

278278
/**
279-
* The account a repository is read with, for a host whose settings choose one per repository
280-
* rather than once; empty for the host's own. Repositories with the same scope share a viewer
281-
* and may share one batched read. Absent means one account for the whole host.
279+
* Which credential a repository is read with, for a host whose settings choose one per
280+
* repository rather than once. Repositories with the same scope share a viewer and may share
281+
* one batched read. Absent means one account for the whole host.
282282
*/
283283
readonly credentialScope?: (input: {
284284
readonly host: string;

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,8 @@ export const make = Effect.gen(function* () {
245245
kind: SourceControlProviderKind.make("github"),
246246
capabilities: CAPABILITIES,
247247
mergeMessageRewrite: removeAgentCredits,
248-
// Settings choose an account per repository or owner; repositories read as one share it.
249-
credentialScope: ({ host, repository }) =>
250-
policy.accountFor(host, repository).pipe(Effect.map((account) => account ?? "")),
248+
// Repositories read with one credential share a viewer and batched reads.
249+
credentialScope: ({ host, repository }) => policy.credentialScope(host, repository),
251250
getRoutingIdentity: (input) =>
252251
policy
253252
.withRepositoryAccount(asRepository(input), cli.getRoutingIdentity(input))

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ export const PinnedGitHubCredential = Context.Reference<{
3434

3535
/**
3636
* Which repository the requests inside are about, set by `withRepositoryAccount`: the credential
37-
* is the one Settings choose for `repository` (`owner/name`, or just an owner), and `account` is
38-
* that choice (absent for the host's own), which GraphQL batches split by so one batch never
39-
* mixes two accounts' lookups.
37+
* is the one Settings choose for `repository` (`owner/name`, or just an owner), and `credential`
38+
* is the fingerprint of the one it is actually read with, which GraphQL batches split by so one
39+
* batch never mixes two credentials' lookups.
4040
*/
4141
export const GitHubCredentialRepository = Context.Reference<{
4242
readonly repository: string;
43-
readonly account: string | undefined;
43+
readonly credential: string | undefined;
4444
} | null>("@t3tools/source-control-github/server/GitHubRequestPolicy/GitHubCredentialRepository", {
4545
defaultValue: () => null,
4646
});
@@ -256,8 +256,11 @@ export class GitHubRequestPolicy extends Context.Service<
256256
input: { readonly host: string; readonly repository: string | null },
257257
effect: Effect.Effect<A, E, R>,
258258
) => Effect.Effect<A, E, R>;
259-
/** The login Settings choose for `repository` on `host`, or undefined for the host's own. */
260-
readonly accountFor: (host: string, repository: string) => Effect.Effect<string | undefined>;
259+
/**
260+
* The fingerprint of the credential `repository` is read with on `host`, after any chosen
261+
* login `gh` no longer holds gave way; empty when there is none, which the read then reports.
262+
*/
263+
readonly credentialScope: (host: string, repository: string) => Effect.Effect<string>;
261264
}
262265
>()("@t3tools/source-control-github/server/GitHubRequestPolicy") {}
263266

@@ -290,6 +293,12 @@ export const make = Effect.gen(function* () {
290293
return { token: held.token, fingerprint: held.fingerprint };
291294
});
292295

296+
const credentialScope: GitHubRequestPolicy["Service"]["credentialScope"] = (host, repository) =>
297+
credentials.get(normalizeHost(host), repository).pipe(
298+
Effect.map((credential) => credential.fingerprint),
299+
Effect.orElseSucceed(() => ""),
300+
);
301+
293302
const withRepositoryAccount: GitHubRequestPolicy["Service"]["withRepositoryAccount"] = (
294303
input,
295304
effect,
@@ -299,9 +308,12 @@ export const make = Effect.gen(function* () {
299308
if (repository === null || (yield* GitHubCredentialRepository) !== null) {
300309
return yield* effect;
301310
}
302-
const account = yield* credentials.accountFor(input.host, repository);
311+
const credential = yield* credentialScope(input.host, repository);
303312
return yield* effect.pipe(
304-
Effect.provideService(GitHubCredentialRepository, { repository, account }),
313+
Effect.provideService(GitHubCredentialRepository, {
314+
repository,
315+
credential: credential === "" ? undefined : credential,
316+
}),
305317
);
306318
});
307319

@@ -400,7 +412,7 @@ export const make = Effect.gen(function* () {
400412
request,
401413
credential,
402414
withRepositoryAccount,
403-
accountFor: credentials.accountFor,
415+
credentialScope,
404416
});
405417
});
406418

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

Lines changed: 73 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -110,74 +110,83 @@ describe("GitHubGraphQlAuth", () => {
110110
},
111111
);
112112

113-
it.effect("batches a repository's lookups only with lookups read as the same account", () => {
114-
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
115-
const layer = GitHubRest.layer.pipe(
116-
Layer.provideMerge(
117-
Layer.mergeAll(
118-
Layer.succeed(GitHubCredentials.GitHubCredentials, {
119-
get: (host, repository) =>
120-
Effect.succeed({
121-
host,
122-
token: Redacted.make(repository === "acme/web" ? "work-token" : "personal-token"),
123-
source: "gh" as const,
124-
fingerprint: `${host}:${repository === "acme/web" ? "work" : "personal"}`,
125-
}),
126-
accountFor: (_host, repository) =>
127-
Effect.succeed(repository === "acme/web" ? "work" : undefined),
128-
invalidate: () => Effect.void,
129-
}),
130-
Layer.succeed(
131-
HttpClient.HttpClient,
132-
HttpClient.make((request) => {
133-
requests.push(request);
134-
return Effect.succeed(
135-
HttpClientResponse.fromWeb(
136-
request,
137-
json({
138-
data: {
139-
_0_repository: { pullRequest: stats },
140-
_1_repository: { pullRequest: stats },
141-
},
142-
}),
143-
),
144-
);
113+
it.effect(
114+
"batches a repository's lookups only with lookups read with the same credential",
115+
() => {
116+
const requests: Array<HttpClientRequest.HttpClientRequest> = [];
117+
const layer = GitHubRest.layer.pipe(
118+
Layer.provideMerge(
119+
Layer.mergeAll(
120+
Layer.succeed(GitHubCredentials.GitHubCredentials, {
121+
get: (host, repository) =>
122+
Effect.succeed({
123+
host,
124+
token: Redacted.make(repository === "acme/web" ? "work-token" : "personal-token"),
125+
source: "gh" as const,
126+
fingerprint: `${host}:${repository === "acme/web" ? "work" : "personal"}`,
127+
}),
128+
// Every repository names a login gh no longer holds, and each falls back to the
129+
// credential above: batches follow that credential, not the name.
130+
accountFor: () => Effect.succeed("gone"),
131+
invalidate: () => Effect.void,
145132
}),
133+
Layer.succeed(
134+
HttpClient.HttpClient,
135+
HttpClient.make((request) => {
136+
requests.push(request);
137+
return Effect.succeed(
138+
HttpClientResponse.fromWeb(
139+
request,
140+
json({
141+
data: {
142+
_0_repository: { pullRequest: stats },
143+
_1_repository: { pullRequest: stats },
144+
},
145+
}),
146+
),
147+
);
148+
}),
149+
),
150+
GitHubQuota.layer,
151+
SourceControlRateLimit.layer,
146152
),
147-
GitHubQuota.layer,
148-
SourceControlRateLimit.layer,
149153
),
150-
),
151-
);
152-
return Effect.gen(function* () {
153-
const policy = yield* GitHubRequestPolicy.GitHubRequestPolicy;
154-
const client = yield* GraphQLClient.make(
155-
PullRequestBatchesGraphQLGroup.middleware(GitHubGraphQlClient.GitHubGraphQlAuth),
156-
).pipe(Effect.provide(GitHubGraphQlClient.layer));
157-
const resolver = GraphQLResolver.aliased(client.PullRequestStats, {
158-
batchKey: GitHubGraphQlClient.batchKey,
159-
});
160-
const lookup = (owner: string, number: number) =>
161-
policy.withRepositoryAccount(
162-
{ host: "github.com", repository: `${owner}/web` },
163-
GraphQLResolver.request({ owner, name: "web", number }, resolver).pipe(
164-
Effect.provideService(GitHubGraphQlClient.GitHubGraphQlTarget, {
165-
host: "github.com",
166-
operation: "stats",
167-
}),
168-
),
154+
);
155+
return Effect.gen(function* () {
156+
const policy = yield* GitHubRequestPolicy.GitHubRequestPolicy;
157+
const client = yield* GraphQLClient.make(
158+
PullRequestBatchesGraphQLGroup.middleware(GitHubGraphQlClient.GitHubGraphQlAuth),
159+
).pipe(Effect.provide(GitHubGraphQlClient.layer));
160+
const resolver = GraphQLResolver.aliased(client.PullRequestStats, {
161+
batchKey: GitHubGraphQlClient.batchKey,
162+
});
163+
const lookup = (owner: string, number: number) =>
164+
policy.withRepositoryAccount(
165+
{ host: "github.com", repository: `${owner}/web` },
166+
GraphQLResolver.request({ owner, name: "web", number }, resolver).pipe(
167+
Effect.provideService(GitHubGraphQlClient.GitHubGraphQlTarget, {
168+
host: "github.com",
169+
operation: "stats",
170+
}),
171+
),
172+
);
173+
yield* Effect.all([lookup("acme", 1), lookup("someone", 2), lookup("other", 3)], {
174+
concurrency: "unbounded",
175+
});
176+
// The company's lookup went alone under its credential; the two read with the personal
177+
// one still shared a document.
178+
expect(requests.map((request) => request.headers["authorization"]).toSorted()).toEqual([
179+
"Bearer personal-token",
180+
"Bearer work-token",
181+
]);
182+
// The Pull Requests list groups repositories the same way.
183+
expect(yield* policy.credentialScope("github.com", "acme/web")).toBe("github.com:work");
184+
expect(yield* policy.credentialScope("github.com", "someone/web")).toBe(
185+
"github.com:personal",
169186
);
170-
yield* Effect.all([lookup("acme", 1), lookup("someone", 2), lookup("other", 3)], {
171-
concurrency: "unbounded",
172-
});
173-
// The company's lookup went alone under its account; the two read as the default account
174-
// still shared a document.
175-
expect(requests.map((request) => request.headers["authorization"]).toSorted()).toEqual([
176-
"Bearer personal-token",
177-
"Bearer work-token",
178-
]);
179-
}).pipe(Effect.provide(layer));
180-
});
187+
}).pipe(Effect.provide(layer));
188+
},
189+
);
181190

182191
it.effect("pauses the host on a RATE_LIMITED answer sent with HTTP 200", () => {
183192
const reset = Math.floor(NOW / 1000) + 600;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ export const layer = Layer.merge(GitHubGraphQlAuth.layer, layerProtocol);
218218

219219
/**
220220
* What decides which lookups to one host may share a batched request: the credential, the
221-
* account chosen for the repositories, and the rate-limit scope it will be sent under.
221+
* credential the repositories are read with, and the rate-limit scope it will be sent under.
222222
* Read from the context the lookup was made in, which is the context a batch then runs in.
223223
* Resolvers already keep hosts apart, since they batch by the transport's URL too.
224224
*/
@@ -227,7 +227,7 @@ export const batchKey = (context: Context.Context<GitHubGraphQlTarget>) =>
227227
Context.getOrElse(context, GitHubRequestPolicy.PinnedGitHubCredential, () => null)
228228
?.credentialFingerprint ?? null,
229229
Context.getOrElse(context, GitHubRequestPolicy.GitHubCredentialRepository, () => null)
230-
?.account ?? null,
230+
?.credential ?? null,
231231
Context.getOrElse(context, SourceControlRateLimit.CredentialScope, () => ""),
232232
]);
233233

0 commit comments

Comments
 (0)