Skip to content

Commit d07b0fe

Browse files
fix: scope a checkout's GitHub calls to GH_REPO when it is set
The source control provider sends a checkout's requests to GH_REPO when it parses, so pick the credential for that repository, not the remote's. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent d39dc62 commit d07b0fe

3 files changed

Lines changed: 258 additions & 212 deletions

File tree

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,4 +580,22 @@ describe("scopeMethodsToRepository", () => {
580580
expect(yield* scoped.viewer({ host: "github.com" })).toBeNull();
581581
});
582582
});
583+
584+
it.effect("scopes a checkout's calls to the repository GH_REPO targets", () => {
585+
const scoped = GitHubRequestPolicy.scopeMethodsToRepository(
586+
{
587+
push: (_input: { readonly context: { readonly remoteUrl: string } }) =>
588+
GitHubRequestPolicy.GitHubRepositoryScope,
589+
read: (_input: { readonly repository: string }) =>
590+
GitHubRequestPolicy.GitHubRepositoryScope,
591+
},
592+
{ checkoutTarget: "upstream/main-repo" },
593+
);
594+
return Effect.gen(function* () {
595+
expect(
596+
yield* scoped.push({ context: { remoteUrl: "[email protected]:fork/main-repo.git" } }),
597+
).toEqual(["upstream/main-repo"]);
598+
expect(yield* scoped.read({ repository: "acme/one" })).toEqual(["acme/one"]);
599+
});
600+
});
583601
});

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

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,10 @@ export const GitHubRepositoryScope = Context.Reference<ReadonlyArray<string> | n
4141
const isRecord = (value: unknown): value is Record<string, unknown> =>
4242
typeof value === "object" && value !== null;
4343

44-
function repositoriesOf(input: unknown): ReadonlyArray<string> | null {
44+
function repositoriesOf(
45+
input: unknown,
46+
checkoutTarget: string | null,
47+
): ReadonlyArray<string> | null {
4548
if (!isRecord(input)) return null;
4649
if (typeof input.repository === "string") return [input.repository];
4750
// Batched reads name several repositories, or change requests that each name one.
@@ -53,7 +56,9 @@ function repositoriesOf(input: unknown): ReadonlyArray<string> | null {
5356
isRecord(entry) && typeof entry.repository === "string" ? [entry.repository] : [],
5457
);
5558
}
56-
// A checkout names its repository through the remote it pushes to.
59+
// A checkout names its repository through the remote it pushes to, unless the provider
60+
// targets another one for every checkout.
61+
if (isRecord(input.context) && checkoutTarget !== null) return [checkoutTarget];
5762
const remote =
5863
isRecord(input.context) && typeof input.context.remoteUrl === "string"
5964
? repositoryPathFromRemoteUrl(input.context.remoteUrl)
@@ -65,15 +70,21 @@ function repositoriesOf(input: unknown): ReadonlyArray<string> | null {
6570
* Every method whose first argument names its repositories (`repository`, `repositories`,
6671
* `changeRequests`, or a checkout `context`) runs in their scope.
6772
*/
68-
export function scopeMethodsToRepository<T extends object>(methods: T): T {
73+
export function scopeMethodsToRepository<T extends object>(
74+
methods: T,
75+
options?: {
76+
/** `owner/name` a checkout's calls go to instead of its remote, as `GH_REPO` sets. */
77+
readonly checkoutTarget?: string | null;
78+
},
79+
): T {
6980
const scoped: Record<string, unknown> = {};
7081
for (const [key, value] of Object.entries(methods)) {
7182
scoped[key] =
7283
typeof value !== "function"
7384
? value
7485
: (...args: Array<unknown>) => {
7586
const result: unknown = Reflect.apply(value, methods, args);
76-
const repositories = repositoriesOf(args[0]);
87+
const repositories = repositoriesOf(args[0], options?.checkoutTarget ?? null);
7788
if (repositories === null || !Effect.isEffect(result)) return result;
7889
// @effect-diagnostics-next-line anyUnknownInErrorContext:off - forwards each method's own effect, whose channels `T` keeps.
7990
return Effect.provideService(result, GitHubRepositoryScope, repositories);

0 commit comments

Comments
 (0)