Skip to content

Commit e47408b

Browse files
Tr1Fecta-7claude
andcommitted
fix(server): review guides work in multi-repo workspaces
A multi-repo workspace folder has no remote of its own, so writing a review guide always failed with "this pull request is in another repository". The guide now looks through the workspace's repositories for the checkout of the pull request's repository and writes the guide from there. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent e8284fa commit e47408b

2 files changed

Lines changed: 106 additions & 37 deletions

File tree

‎apps/server/src/pullRequest/PullRequestReviewGuideService.test.ts‎

Lines changed: 59 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,20 @@ import {
1616
type PullRequestDetail,
1717
type PullRequestStack,
1818
type PullRequestRef,
19+
type RepositoryIdentity,
1920
} from "@t3tools/contracts";
2021

2122
import * as ServerConfig from "../config.ts";
2223
import * as SqlitePersistence from "../persistence/Sqlite.ts";
2324
import * as PullRequestReviewGuides from "../persistence/PullRequestReviewGuides.ts";
2425
import * as ProjectService from "../project/ProjectService.ts";
26+
import * as RepositoryIdentityResolver from "../project/RepositoryIdentityResolver.ts";
2527
import * as ProviderRegistry from "../provider/ProviderRegistry.ts";
2628
import * as ServerSettings from "../serverSettings.ts";
2729
import * as TextGeneration from "../textGeneration/TextGeneration.ts";
2830
import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
2931
import * as VcsProcess from "../vcs/VcsProcess.ts";
32+
import * as WorkspaceRepositories from "../workspace/WorkspaceRepositories.ts";
3033
import * as PullRequestReviewGuideService from "./PullRequestReviewGuideService.ts";
3134
import * as PullRequestService from "./PullRequestService.ts";
3235

@@ -159,6 +162,8 @@ const makeLayer = (input: {
159162
readonly enabled?: boolean;
160163
readonly currentHead?: Ref.Ref<string>;
161164
readonly identity?: typeof ACME_APP;
165+
/** Opens the clones' parent folder as a multi-repo workspace project instead of the clone. */
166+
readonly multiRepo?: boolean;
162167
}) =>
163168
PullRequestReviewGuideService.layer.pipe(
164169
Layer.provide(
@@ -191,16 +196,34 @@ const makeLayer = (input: {
191196
Layer.provide(
192197
Layer.succeed(ProjectService.ProjectService, {
193198
getShell: () =>
194-
Effect.succeedSome({
195-
id: projectId,
196-
workspaceRoot: input.workspace,
197-
repositoryIdentity: {
198-
...(input.identity ?? ACME_APP),
199-
locator: { ...ACME_APP.locator, remoteUrl: input.remote },
200-
},
201-
}),
199+
Effect.succeedSome(
200+
input.multiRepo === true
201+
? { id: projectId, workspaceRoot: input.root, repositoryIdentity: null }
202+
: {
203+
id: projectId,
204+
workspaceRoot: input.workspace,
205+
repositoryIdentity: {
206+
...(input.identity ?? ACME_APP),
207+
locator: { ...ACME_APP.locator, remoteUrl: input.remote },
208+
},
209+
},
210+
),
202211
} as unknown as ProjectService.ProjectService["Service"]),
203212
),
213+
Layer.provide(
214+
Layer.succeed(RepositoryIdentityResolver.RepositoryIdentityResolver, {
215+
resolve: (cwd) =>
216+
Effect.succeed(
217+
cwd === input.workspace
218+
? ({
219+
...ACME_APP,
220+
locator: { ...ACME_APP.locator, remoteUrl: input.remote },
221+
} as RepositoryIdentity)
222+
: null,
223+
),
224+
}),
225+
),
226+
Layer.provide(WorkspaceRepositories.layer),
204227
Layer.provide(
205228
Layer.succeed(ProviderRegistry.ProviderRegistry, {
206229
getProviders: Effect.succeed([]),
@@ -516,6 +539,34 @@ it.layer(layerBase, { excludeTestServices: true })("PullRequestReviewGuideServic
516539
),
517540
);
518541

542+
it.effect("writes the guide from the matching repository of a multi-repo workspace", () =>
543+
Effect.scoped(
544+
Effect.gen(function* () {
545+
const repositories = yield* makeRepositories;
546+
const generatedIn = yield* Deferred.make<string>();
547+
const stored = yield* Deferred.make<void>();
548+
const context = yield* Layer.build(
549+
makeLayer({
550+
...repositories,
551+
stored,
552+
multiRepo: true,
553+
generate: (input) =>
554+
Deferred.succeed(generatedIn, input.cwd).pipe(Effect.as({ stops: [] })),
555+
}),
556+
);
557+
const service = Context.get(
558+
context,
559+
PullRequestReviewGuideService.PullRequestReviewGuideService,
560+
);
561+
yield* service.generate(reference);
562+
yield* Deferred.await(stored);
563+
assert.notStrictEqual(yield* Deferred.await(generatedIn), repositories.root);
564+
const result = yield* service.get(reference);
565+
assert.strictEqual(result.guide?.headSha, repositories.headSha);
566+
}),
567+
),
568+
);
569+
519570
it.effect("refuses a pull request from a repository the project is not a checkout of", () =>
520571
Effect.scoped(
521572
Effect.gen(function* () {

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

Lines changed: 47 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -23,16 +23,19 @@ import {
2323
type ThreadPullRequestKey,
2424
} from "@t3tools/contracts";
2525
import type { ReviewGuideStackLayer } from "@t3tools/provider-core/server/textGeneration";
26+
import { joinWorkspaceRepositoryPath } from "@t3tools/shared/path";
2627
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
2728
import { sourceControlDefinitions } from "../sourceControl/sourceControlDefinitions.ts";
2829

2930
import * as ServerConfig from "../config.ts";
3031
import * as PullRequestReviewGuides from "../persistence/PullRequestReviewGuides.ts";
3132
import * as ProjectService from "../project/ProjectService.ts";
33+
import * as RepositoryIdentityResolver from "../project/RepositoryIdentityResolver.ts";
3234
import * as ProviderRegistry from "../provider/ProviderRegistry.ts";
3335
import * as ServerSettings from "../serverSettings.ts";
3436
import * as TextGeneration from "../textGeneration/TextGeneration.ts";
3537
import * as GitVcsDriver from "../vcs/GitVcsDriver.ts";
38+
import * as WorkspaceRepositories from "../workspace/WorkspaceRepositories.ts";
3639
import { PATCH_RENDER_PREFIX_ARGS } from "../vcs/GitVcsDriverCore.ts";
3740
import * as PullRequestService from "./PullRequestService.ts";
3841
import { pullRequestSyncKey } from "./pullRequestSyncKey.ts";
@@ -72,6 +75,8 @@ const make = Effect.gen(function* () {
7275
const git = yield* GitVcsDriver.GitVcsDriver;
7376
const fileSystem = yield* FileSystem.FileSystem;
7477
const config = yield* ServerConfig.ServerConfig;
78+
const workspaceRepositories = yield* WorkspaceRepositories.WorkspaceRepositories;
79+
const repositoryIdentities = yield* RepositoryIdentityResolver.RepositoryIdentityResolver;
7580
const serviceScope = yield* Effect.scope;
7681
const runs = yield* Ref.make<ReadonlyMap<string, PullRequestReviewGuideRun>>(new Map());
7782
let runCounter = 0;
@@ -94,34 +99,42 @@ const make = Effect.gen(function* () {
9499
return { key, workspaceRoot: project.workspaceRoot, identity: project.repositoryIdentity };
95100
});
96101

97-
const requireOwnRepository = Effect.fn("PullRequestReviewGuideService.requireOwnRepository")(
98-
function* (key: ThreadPullRequestKey, identity: RepositoryIdentity | null | undefined) {
99-
const own =
100-
identity == null
101-
? null
102-
: pullRequestSyncKey(
103-
{
104-
projectId: ProjectId.make("own"),
105-
number: key.number,
106-
repository: sourceControlDefinitions.repositorySelector(identity) ?? "",
107-
},
108-
identity,
109-
);
110-
if (
111-
identity == null ||
112-
own === null ||
113-
own.host !== key.host ||
114-
own.repository !== key.repository
115-
) {
116-
return yield* new PullRequestOperationError({
117-
operation: "generateReviewGuide",
118-
detail:
119-
"Guides are written from this project's checkout, and this pull request is in another repository. Write it from a project that is a checkout of that repository.",
120-
});
102+
const isOwnRepository = (
103+
key: ThreadPullRequestKey,
104+
identity: RepositoryIdentity | null | undefined,
105+
): identity is RepositoryIdentity => {
106+
if (identity == null) return false;
107+
const own = pullRequestSyncKey(
108+
{
109+
projectId: ProjectId.make("own"),
110+
number: key.number,
111+
repository: sourceControlDefinitions.repositorySelector(identity) ?? "",
112+
},
113+
identity,
114+
);
115+
return own !== null && own.host === key.host && own.repository === key.repository;
116+
};
117+
118+
// A multi-repo workspace folder has no remote of its own; one of its repositories may be the checkout.
119+
const requireCheckout = Effect.fn("PullRequestReviewGuideService.requireCheckout")(function* (
120+
key: ThreadPullRequestKey,
121+
workspaceRoot: string,
122+
identity: RepositoryIdentity | null | undefined,
123+
) {
124+
if (isOwnRepository(key, identity)) return { workspaceRoot, locator: identity.locator };
125+
for (const repository of yield* workspaceRepositories.list(workspaceRoot)) {
126+
const cwd = joinWorkspaceRepositoryPath(workspaceRoot, repository.relativePath);
127+
const repositoryIdentity = yield* repositoryIdentities.resolve(cwd);
128+
if (isOwnRepository(key, repositoryIdentity)) {
129+
return { workspaceRoot: cwd, locator: repositoryIdentity.locator };
121130
}
122-
return identity.locator;
123-
},
124-
);
131+
}
132+
return yield* new PullRequestOperationError({
133+
operation: "generateReviewGuide",
134+
detail:
135+
"Guides are written from this project's checkout, and this pull request is in another repository. Write it from a project that is a checkout of that repository.",
136+
});
137+
});
125138

126139
const readStored = (key: ThreadPullRequestKey) =>
127140
guides.get(key).pipe(
@@ -440,8 +453,13 @@ const make = Effect.gen(function* () {
440453
detail: "Review guides are off. Turn them on in Settings → Source Control.",
441454
});
442455
}
443-
const { key, workspaceRoot, identity } = yield* keyFor(input);
444-
const locator = yield* requireOwnRepository(key, identity);
456+
const project = yield* keyFor(input);
457+
const { key } = project;
458+
const { workspaceRoot, locator } = yield* requireCheckout(
459+
key,
460+
project.workspaceRoot,
461+
project.identity,
462+
);
445463
const runKey = keyString(key);
446464
yield* pullRequests.invalidate({ reference: input });
447465
const [detail, stored] = yield* Effect.all([pullRequests.detail(input), readStored(key)], {

0 commit comments

Comments
 (0)