Skip to content

Commit 3c4e0cc

Browse files
committed
fix(server): address review guide failure feedback
1 parent 3137118 commit 3c4e0cc

2 files changed

Lines changed: 42 additions & 21 deletions

File tree

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

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import * as Deferred from "effect/Deferred";
55
import * as Effect from "effect/Effect";
66
import * as FileSystem from "effect/FileSystem";
77
import * as Layer from "effect/Layer";
8+
import * as Logger from "effect/Logger";
89
import * as Ref from "effect/Ref";
910
import * as Schedule from "effect/Schedule";
1011
import {
@@ -505,14 +506,16 @@ it.layer(layerBase, { excludeTestServices: true })("PullRequestReviewGuideServic
505506
),
506507
);
507508

508-
/** The detail a run reports when ssh to the project's remote refuses with `refusal`. */
509-
const failedRunDetail = (refusal: string) =>
509+
const failedRunDetail = (refusal: string, remote = "origin") =>
510510
Effect.gen(function* () {
511511
const repositories = yield* makeRepositories;
512+
if (remote !== "origin") {
513+
yield* git(repositories.workspace, ["remote", "rename", "origin", remote]);
514+
}
512515
yield* git(repositories.workspace, [
513516
"remote",
514517
"set-url",
515-
"origin",
518+
remote,
516519
"[email protected]:acme/app.git",
517520
]);
518521
yield* git(repositories.workspace, [
@@ -521,6 +524,13 @@ it.layer(layerBase, { excludeTestServices: true })("PullRequestReviewGuideServic
521524
`sh -c 'echo "${refusal}" >&2; exit 255'`,
522525
]);
523526
const stored = yield* Deferred.make<void>();
527+
const completed = yield* Deferred.make<void>();
528+
const logger = Logger.make(({ fiber, message }) => {
529+
if (Array.isArray(message) && message.includes("Review guide generation failed")) {
530+
// The warning precedes the state update; wait for its fiber to finish both.
531+
fiber.addObserver(() => Deferred.doneUnsafe(completed, Effect.void));
532+
}
533+
});
524534
const context = yield* Layer.build(
525535
makeLayer({
526536
...repositories,
@@ -532,23 +542,21 @@ it.layer(layerBase, { excludeTestServices: true })("PullRequestReviewGuideServic
532542
context,
533543
PullRequestReviewGuideService.PullRequestReviewGuideService,
534544
);
535-
yield* service.generate(reference);
536-
return yield* service.get(reference).pipe(
537-
Effect.flatMap((result) =>
538-
result.run?._tag === "Failed"
539-
? Effect.succeed(result.run.detail)
540-
: Effect.fail("still running" as const),
541-
),
542-
Effect.retry(Schedule.spaced("20 millis")),
543-
);
545+
yield* service
546+
.generate(reference)
547+
.pipe(Effect.provide(Logger.layer([logger], { mergeWithExisting: false })));
548+
yield* Deferred.await(completed);
549+
const result = yield* service.get(reference);
550+
assert.strictEqual(result.run?._tag, "Failed");
551+
return result.run?._tag === "Failed" ? result.run.detail : undefined;
544552
});
545553

546554
it.effect("tells the reader what to fix when git does not trust the remote's host key", () =>
547555
Effect.scoped(
548556
Effect.gen(function* () {
549557
assert.strictEqual(
550558
yield* failedRunDetail("Host key verification failed."),
551-
'Git does not trust the SSH host key of the project\'s "origin" remote. Run "git fetch origin" in the project once, then write the guide again.',
559+
'Git does not trust the SSH host key of the project\'s "origin" remote. Once "git fetch origin" works in the project, write the guide again.',
552560
);
553561
}),
554562
),
@@ -559,9 +567,22 @@ it.layer(layerBase, { excludeTestServices: true })("PullRequestReviewGuideServic
559567
Effect.gen(function* () {
560568
assert.strictEqual(
561569
yield* failedRunDetail("[email protected]: Permission denied (publickey)."),
562-
'Git could not sign in to the project\'s "origin" remote. Run "git fetch origin" in the project once, then write the guide again.',
570+
'Git could not sign in to the project\'s "origin" remote. Check the Git credentials or SSH key it uses. Once "git fetch origin" works in the project, write the guide again.',
563571
);
564572
}),
565573
),
566574
);
575+
576+
it.effect("keeps long or unsafe remote names out of the failure detail", () =>
577+
Effect.scoped(
578+
Effect.gen(function* () {
579+
for (const remote of ["remote".repeat(20), "remote;echo-secret"]) {
580+
assert.strictEqual(
581+
yield* failedRunDetail("Host key verification failed.", remote),
582+
"Git does not trust the SSH host key of the project's configured remote. Once fetching from that remote works in the project, write the guide again.",
583+
);
584+
}
585+
}),
586+
),
587+
);
567588
});

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,23 +77,23 @@ export class PullRequestReviewGuideService extends Context.Service<
7777

7878
const keyString = (key: ThreadPullRequestKey) => `${key.host}/${key.repository}#${key.number}`;
7979

80-
/**
81-
* A fetch the remote refused for a reason the reader can fix names that fix; any other failure
82-
* keeps git's own error.
83-
*/
8480
const fetchFailure = (remote: string, cause: GitCommandError) => {
85-
const retry = `Run "git fetch ${remote}" in the project once, then write the guide again.`;
81+
const safeRemote = /^[a-zA-Z0-9][a-zA-Z0-9._-]{0,63}$/.test(remote);
82+
const remoteLabel = safeRemote ? `"${remote}"` : "configured";
83+
const retry = safeRemote
84+
? `Once "git fetch ${remote}" works in the project, write the guide again.`
85+
: "Once fetching from that remote works in the project, write the guide again.";
8686
switch (cause.reason) {
8787
case "host_key_unverified":
8888
return new PullRequestOperationError({
8989
operation: "generateReviewGuide",
90-
detail: `Git does not trust the SSH host key of the project's "${remote}" remote. ${retry}`,
90+
detail: `Git does not trust the SSH host key of the project's ${remoteLabel} remote. ${retry}`,
9191
cause,
9292
});
9393
case "authentication_failed":
9494
return new PullRequestOperationError({
9595
operation: "generateReviewGuide",
96-
detail: `Git could not sign in to the project's "${remote}" remote. ${retry}`,
96+
detail: `Git could not sign in to the project's ${remoteLabel} remote. Check the Git credentials or SSH key it uses. ${retry}`,
9797
cause,
9898
});
9999
default:

0 commit comments

Comments
 (0)