fix(server): review guides find Bitbucket heads reported by short hash (#18900)
Co-authored-by: Pawel Mikolajczuk <[email protected]> Co-authored-by: Claude Opus 5.5 <[email protected]>
This commit is contained in:
3 files changed
+400
-15
No files matched your search
@@ -9,6 +9,7 @@ import * as Ref from "effect/Ref";
|
||||
import * as Schedule from "effect/Schedule";
|
||||
import {
|
||||
ProjectId,
|
||||
GitCommandError,
|
||||
type PullRequestActivity,
|
||||
type PullRequestDetail,
|
||||
type PullRequestStack,
|
||||
@@ -144,7 +145,7 @@ const stack = (repositories: {
|
||||
{
|
||||
number: 8,
|
||||
title: "Upper",
|
||||
headSha: repositories.upperSha,
|
||||
headSha: repositories.upperSha.slice(0, 12),
|
||||
headBranch: "upper",
|
||||
state: "open",
|
||||
},
|
||||
@@ -228,8 +229,328 @@ const layerBase = Layer.mergeAll(
|
||||
SqlitePersistence.layerMemory,
|
||||
).pipe(Layer.provideMerge(NodeServices.layer));
|
||||
|
||||
type Repositories = Effect.Success<typeof makeRepositories>;
|
||||
|
||||
const awaitRunFailure = (
|
||||
service: PullRequestReviewGuideService.PullRequestReviewGuideService["Service"],
|
||||
input: PullRequestRef,
|
||||
) =>
|
||||
service.get(input).pipe(
|
||||
Effect.flatMap((result) =>
|
||||
result.run?._tag === "Failed"
|
||||
? Effect.succeed(result.run.detail)
|
||||
: Effect.fail("still running" as const),
|
||||
),
|
||||
Effect.retry(Schedule.spaced("20 millis")),
|
||||
);
|
||||
|
||||
/** A workspace cloned with only `main`, so the change's head is not there until fetched. */
|
||||
const startSingleBranchRun = (input: {
|
||||
readonly repositoryName: string;
|
||||
readonly reportedHead: (repositories: Repositories) => string;
|
||||
readonly refuseCommitFetch: boolean;
|
||||
readonly prepare: (
|
||||
repositories: Repositories,
|
||||
workspace: string,
|
||||
) => Effect.Effect<void, GitCommandError, GitVcsDriver.GitVcsDriver>;
|
||||
/** Runs inside the head branch's fetch, after the service asked for it and before git runs. */
|
||||
readonly onBranchFetch?:
|
||||
| ((
|
||||
repositories: Repositories,
|
||||
) => Effect.Effect<void, GitCommandError, GitVcsDriver.GitVcsDriver>)
|
||||
| undefined;
|
||||
}) =>
|
||||
Effect.gen(function* () {
|
||||
const repositories = yield* makeRepositories;
|
||||
const workspace = `${repositories.root}/single-branch`;
|
||||
yield* git(repositories.root, [
|
||||
"clone",
|
||||
"--quiet",
|
||||
"--no-local",
|
||||
"--single-branch",
|
||||
"--branch",
|
||||
"main",
|
||||
`${repositories.root}/remote`,
|
||||
workspace,
|
||||
]);
|
||||
yield* input.prepare(repositories, workspace);
|
||||
const reportedHead = input.reportedHead(repositories);
|
||||
const stored = yield* Deferred.make<void>();
|
||||
const driver = yield* GitVcsDriver.GitVcsDriver;
|
||||
const fetchedRefs: Array<string> = [];
|
||||
const observed: Array<{ cwd: string; head: string; changed: string }> = [];
|
||||
const onBranchFetch = input.onBranchFetch;
|
||||
const observedDriver = {
|
||||
...driver,
|
||||
execute: (call: GitVcsDriver.ExecuteGitInput) =>
|
||||
Effect.gen(function* () {
|
||||
const ref = call.args.at(-1);
|
||||
if (call.args[0] !== "fetch" || ref === undefined) return yield* driver.execute(call);
|
||||
fetchedRefs.push(ref);
|
||||
if (input.refuseCommitFetch && ref === repositories.headSha) {
|
||||
return yield* new GitCommandError({
|
||||
operation: call.operation,
|
||||
command: "git",
|
||||
cwd: call.cwd,
|
||||
detail: "The test remote refuses fetches by commit.",
|
||||
exitCode: 128,
|
||||
});
|
||||
}
|
||||
if (ref === "refs/heads/feature" && onBranchFetch !== undefined) {
|
||||
yield* onBranchFetch(repositories).pipe(
|
||||
Effect.provideService(GitVcsDriver.GitVcsDriver, driver),
|
||||
);
|
||||
}
|
||||
return yield* driver.execute(call);
|
||||
}),
|
||||
};
|
||||
const readCheckout = (cwd: string, args: ReadonlyArray<string>) =>
|
||||
driver
|
||||
.execute({ operation: "test.readCheckout", cwd, args })
|
||||
.pipe(Effect.map((result) => result.stdout.trim()));
|
||||
const context = yield* Layer.build(
|
||||
makeLayer({
|
||||
...repositories,
|
||||
headSha: reportedHead,
|
||||
workspace,
|
||||
stored,
|
||||
identity: {
|
||||
...ACME_APP,
|
||||
canonicalKey: `github.com/acme/${input.repositoryName}`,
|
||||
displayName: `acme/${input.repositoryName}`,
|
||||
name: input.repositoryName,
|
||||
},
|
||||
generate: (generation) =>
|
||||
Effect.gen(function* () {
|
||||
observed.push({
|
||||
cwd: generation.cwd,
|
||||
head: yield* readCheckout(generation.cwd, ["rev-parse", "HEAD"]),
|
||||
changed: yield* readCheckout(generation.cwd, [
|
||||
"diff",
|
||||
"--name-only",
|
||||
`${generation.baseCommit}...HEAD`,
|
||||
]),
|
||||
});
|
||||
return { stops: [] };
|
||||
}).pipe(Effect.orDie),
|
||||
}).pipe(Layer.provide(Layer.succeed(GitVcsDriver.GitVcsDriver, observedDriver))),
|
||||
);
|
||||
const service = Context.get(
|
||||
context,
|
||||
PullRequestReviewGuideService.PullRequestReviewGuideService,
|
||||
);
|
||||
const guideReference = { ...reference, repository: `acme/${input.repositoryName}` };
|
||||
yield* service.generate(guideReference);
|
||||
return {
|
||||
repositories,
|
||||
reportedHead,
|
||||
service,
|
||||
reference: guideReference,
|
||||
stored,
|
||||
fetchedRefs,
|
||||
observed,
|
||||
};
|
||||
});
|
||||
|
||||
// Runs drive real git, which the test clock does not advance, so waits use real time.
|
||||
it.layer(layerBase, { excludeTestServices: true })("PullRequestReviewGuideService", (it) => {
|
||||
it.effect.each([
|
||||
{
|
||||
name: "a short head already fetched",
|
||||
shortHash: true,
|
||||
prepare: "fetch-feature",
|
||||
pushDuringFetch: false,
|
||||
refuseCommitFetch: true,
|
||||
expectedFetches: [],
|
||||
},
|
||||
{
|
||||
name: "a short head on its branch",
|
||||
shortHash: true,
|
||||
prepare: "none",
|
||||
pushDuringFetch: false,
|
||||
refuseCommitFetch: true,
|
||||
expectedFetches: ["refs/heads/feature"],
|
||||
},
|
||||
{
|
||||
name: "a short head whose branch moves during the fetch",
|
||||
shortHash: true,
|
||||
prepare: "none",
|
||||
pushDuringFetch: true,
|
||||
refuseCommitFetch: true,
|
||||
expectedFetches: ["refs/heads/feature"],
|
||||
},
|
||||
{
|
||||
name: "a full head served by commit",
|
||||
shortHash: false,
|
||||
prepare: "none",
|
||||
pushDuringFetch: false,
|
||||
refuseCommitFetch: false,
|
||||
expectedFetches: ["<head>"],
|
||||
},
|
||||
{
|
||||
name: "a full head on its branch",
|
||||
shortHash: false,
|
||||
prepare: "none",
|
||||
pushDuringFetch: false,
|
||||
refuseCommitFetch: true,
|
||||
expectedFetches: ["<head>", "refs/heads/feature"],
|
||||
},
|
||||
{
|
||||
name: "a full head only on its pull ref",
|
||||
shortHash: false,
|
||||
prepare: "refs/pull/7/head",
|
||||
pushDuringFetch: false,
|
||||
refuseCommitFetch: true,
|
||||
expectedFetches: ["<head>", "refs/heads/feature", "refs/pull/7/head"],
|
||||
},
|
||||
{
|
||||
name: "a full head only on its merge request ref",
|
||||
shortHash: false,
|
||||
prepare: "refs/merge-requests/7/head",
|
||||
pushDuringFetch: false,
|
||||
refuseCommitFetch: true,
|
||||
expectedFetches: [
|
||||
"<head>",
|
||||
"refs/heads/feature",
|
||||
"refs/pull/7/head",
|
||||
"refs/merge-requests/7/head",
|
||||
],
|
||||
},
|
||||
])(
|
||||
"writes a guide for the reported commit: $name",
|
||||
({ shortHash, prepare, pushDuringFetch, refuseCommitFetch, expectedFetches }) =>
|
||||
Effect.scoped(
|
||||
Effect.gen(function* () {
|
||||
const run = yield* startSingleBranchRun({
|
||||
repositoryName: "fetched-head",
|
||||
reportedHead: (repositories) =>
|
||||
shortHash ? repositories.headSha.slice(0, 12) : repositories.headSha,
|
||||
refuseCommitFetch,
|
||||
prepare: (repositories, workspace) => {
|
||||
const remote = `${repositories.root}/remote`;
|
||||
if (prepare === "fetch-feature") {
|
||||
return git(workspace, ["fetch", "--quiet", "origin", "feature"]).pipe(
|
||||
Effect.asVoid,
|
||||
);
|
||||
}
|
||||
if (prepare === "none") return Effect.void;
|
||||
return git(remote, ["update-ref", prepare, repositories.headSha]).pipe(
|
||||
Effect.andThen(
|
||||
git(remote, ["update-ref", "refs/heads/feature", repositories.lowerSha]),
|
||||
),
|
||||
Effect.asVoid,
|
||||
);
|
||||
},
|
||||
onBranchFetch: pushDuringFetch
|
||||
? (repositories) => {
|
||||
const remote = `${repositories.root}/remote`;
|
||||
return git(remote, ["checkout", "--quiet", "feature"]).pipe(
|
||||
Effect.andThen(
|
||||
git(remote, [
|
||||
"commit",
|
||||
"--quiet",
|
||||
"--allow-empty",
|
||||
"-m",
|
||||
"push during fetch",
|
||||
]),
|
||||
),
|
||||
Effect.asVoid,
|
||||
);
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
yield* Effect.raceFirst(
|
||||
Deferred.await(run.stored),
|
||||
awaitRunFailure(run.service, run.reference).pipe(
|
||||
Effect.flatMap((detail) => Effect.die(`run failed: ${detail}`)),
|
||||
),
|
||||
);
|
||||
const { headSha } = run.repositories;
|
||||
const result = yield* run.service.get(run.reference);
|
||||
assert.strictEqual(result.guide?.headSha, run.reportedHead);
|
||||
assert.deepStrictEqual(result.guide?.stops[0]?.files, [{ path: "feature.txt" }]);
|
||||
assert.deepStrictEqual(run.observed, [
|
||||
{ cwd: run.observed[0]?.cwd, head: headSha, changed: "feature.txt" },
|
||||
]);
|
||||
const headRefs = new Set([
|
||||
headSha,
|
||||
run.reportedHead,
|
||||
"refs/heads/feature",
|
||||
"refs/pull/7/head",
|
||||
"refs/merge-requests/7/head",
|
||||
]);
|
||||
assert.deepStrictEqual(
|
||||
run.fetchedRefs.filter((ref) => headRefs.has(ref)),
|
||||
expectedFetches.map((ref) => (ref === "<head>" ? headSha : ref)),
|
||||
);
|
||||
const fs = yield* FileSystem.FileSystem;
|
||||
assert.isFalse(yield* fs.exists(run.observed[0]?.cwd ?? ""));
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
it.effect.each([
|
||||
{
|
||||
name: "a branch named like the short head",
|
||||
reportedHead: (repositories: Repositories) => repositories.headSha.slice(0, 12),
|
||||
shadow: "branch",
|
||||
expected: ["a branch or tag rather than the commit"],
|
||||
},
|
||||
{
|
||||
name: "a tag named like the short head",
|
||||
reportedHead: (repositories: Repositories) => repositories.headSha.slice(0, 12),
|
||||
shadow: "tag",
|
||||
expected: ["a branch or tag rather than the commit"],
|
||||
},
|
||||
{
|
||||
name: "a head that is not a commit hash",
|
||||
reportedHead: (repositories: Repositories) =>
|
||||
`--upload-pack=touch ${repositories.root}/injected`,
|
||||
shadow: "none",
|
||||
expected: ["not a commit hash"],
|
||||
},
|
||||
{
|
||||
name: "a head no source has",
|
||||
reportedHead: () => "e".repeat(40),
|
||||
shadow: "none",
|
||||
expected: [
|
||||
"local objects:",
|
||||
"the commit:",
|
||||
"branch feature:",
|
||||
"refs/pull/7/head:",
|
||||
"refs/merge-requests/7/head:",
|
||||
],
|
||||
},
|
||||
])("refuses to write a guide for $name", ({ reportedHead, shadow, expected }) =>
|
||||
Effect.scoped(
|
||||
Effect.gen(function* () {
|
||||
const run = yield* startSingleBranchRun({
|
||||
repositoryName: "refused-head",
|
||||
reportedHead,
|
||||
refuseCommitFetch: false,
|
||||
prepare: (repositories, workspace) =>
|
||||
git(workspace, ["fetch", "--quiet", "origin", "feature"]).pipe(
|
||||
Effect.andThen(
|
||||
shadow === "none"
|
||||
? Effect.void
|
||||
: git(workspace, [shadow, repositories.headSha.slice(0, 12), "HEAD"]),
|
||||
),
|
||||
Effect.asVoid,
|
||||
),
|
||||
});
|
||||
const detail = yield* Effect.raceFirst(
|
||||
awaitRunFailure(run.service, run.reference),
|
||||
Deferred.await(run.stored).pipe(Effect.andThen(Effect.die("a guide was written"))),
|
||||
);
|
||||
for (const part of expected) assert.include(detail, part);
|
||||
assert.deepStrictEqual(run.observed, []);
|
||||
assert.isFalse(run.fetchedRefs.some((ref) => ref.startsWith("-")));
|
||||
const fs = yield* FileSystem.FileSystem;
|
||||
assert.isFalse(yield* fs.exists(`${run.repositories.root}/injected`));
|
||||
}),
|
||||
),
|
||||
);
|
||||
|
||||
it.effect("briefs the change against its own base in a checkout it then removes", () =>
|
||||
Effect.scoped(
|
||||
Effect.gen(function* () {
|
||||
|
||||
@@ -21,6 +21,7 @@ import * as FileSystem from "effect/FileSystem";
|
||||
import * as Layer from "effect/Layer";
|
||||
import * as Option from "effect/Option";
|
||||
import * as Ref from "effect/Ref";
|
||||
import * as Result from "effect/Result";
|
||||
import * as Semaphore from "effect/Semaphore";
|
||||
|
||||
import {
|
||||
@@ -52,6 +53,8 @@ import { pullRequestSyncKey } from "./pullRequestSyncKey.ts";
|
||||
import { reviewGuideInputFromPullRequest } from "./pullRequestReviewGuideInput.ts";
|
||||
import { normalizeReviewGuide, parseReviewGuideChange } from "./pullRequestReviewGuideNormalize.ts";
|
||||
|
||||
const COMMIT_HASH_PATTERN = /^[0-9a-f]{7,64}$/;
|
||||
|
||||
export class PullRequestReviewGuideService extends Context.Service<
|
||||
PullRequestReviewGuideService,
|
||||
{
|
||||
@@ -173,9 +176,9 @@ const make = Effect.gen(function* () {
|
||||
});
|
||||
|
||||
/**
|
||||
* A detached checkout of the head beside the project, removed when the run ends. Fetched by
|
||||
* commit rather than by branch so a fork's head and a branch checked out elsewhere both work,
|
||||
* and so the checkout is exactly what the host reported.
|
||||
* A detached checkout of the head beside the project, removed when the run ends. Pinned to
|
||||
* the reported commit, so a branch checked out elsewhere works and the checkout is exactly
|
||||
* what the host reported.
|
||||
*/
|
||||
const checkoutHead = Effect.fn("PullRequestReviewGuideService.checkoutHead")(function* (
|
||||
workspaceRoot: string,
|
||||
@@ -192,9 +195,7 @@ const make = Effect.gen(function* () {
|
||||
args: ["fetch", "--quiet", "--no-tags", remote, ...args],
|
||||
timeoutMs: 5 * 60_000,
|
||||
});
|
||||
// The base branch is what the change is measured from; the head commit is the change. Hosts
|
||||
// that do not serve an unadvertised commit still serve their pull refs, which GitHub and
|
||||
// Forgejo name the same way.
|
||||
// The base branch is what the change is measured from; the head commit is the change.
|
||||
// The host names the base branch. A name git rejects, or one that would change the refspec
|
||||
// (a leading `+`, a `:`), is refused before it reaches `git fetch`.
|
||||
yield* git
|
||||
@@ -227,9 +228,63 @@ const make = Effect.gen(function* () {
|
||||
.pipe(Effect.ignore({ log: true })),
|
||||
);
|
||||
const baseTip = yield* git.resolveCommit({ cwd: workspaceRoot, revision: baseRef });
|
||||
const fetchHead = (number: number, sha: string) =>
|
||||
fetch([sha]).pipe(Effect.catch(() => fetch([`refs/pull/${number}/head`])));
|
||||
yield* fetchHead(detail.number, headSha);
|
||||
const fetchHead = Effect.fn("PullRequestReviewGuideService.fetchHead")(function* (
|
||||
number: number,
|
||||
reportedSha: string,
|
||||
branch: string,
|
||||
) {
|
||||
const sha = reportedSha.toLowerCase();
|
||||
if (!COMMIT_HASH_PATTERN.test(sha)) {
|
||||
return yield* new PullRequestOperationError({
|
||||
operation: "generateReviewGuide",
|
||||
detail: `The host reported a head for #${number} that is not a commit hash.`,
|
||||
});
|
||||
}
|
||||
// Not redundant: rev-parse prefers a branch or tag named like a short hash over the commit.
|
||||
const resolveHead = git.resolveCommit({ cwd: workspaceRoot, revision: sha }).pipe(
|
||||
Effect.filterOrFail(
|
||||
({ commitSha }) => commitSha.startsWith(sha),
|
||||
({ commitSha }) =>
|
||||
new PullRequestOperationError({
|
||||
operation: "generateReviewGuide",
|
||||
detail: `${sha} names ${commitSha} here, a branch or tag rather than the commit.`,
|
||||
}),
|
||||
),
|
||||
);
|
||||
const fetchAndResolve = (ref: string) => fetch([ref]).pipe(Effect.andThen(resolveHead));
|
||||
const branchRef = `refs/heads/${branch}`;
|
||||
const checkBranch = git.execute({
|
||||
operation: "PullRequestReviewGuideService.checkHeadBranch",
|
||||
cwd: workspaceRoot,
|
||||
args: ["check-ref-format", branchRef],
|
||||
});
|
||||
const sources = [
|
||||
{ source: "local objects", head: resolveHead },
|
||||
...(sha.length === 40 || sha.length === 64
|
||||
? [{ source: "the commit", head: fetchAndResolve(sha) }]
|
||||
: []),
|
||||
{
|
||||
source: `branch ${branch}`,
|
||||
head: checkBranch.pipe(Effect.andThen(fetchAndResolve(branchRef))),
|
||||
},
|
||||
{ source: `refs/pull/${number}/head`, head: fetchAndResolve(`refs/pull/${number}/head`) },
|
||||
{
|
||||
source: `refs/merge-requests/${number}/head`,
|
||||
head: fetchAndResolve(`refs/merge-requests/${number}/head`),
|
||||
},
|
||||
];
|
||||
const failures: Array<string> = [];
|
||||
for (const { source, head } of sources) {
|
||||
const result = yield* Effect.result(head);
|
||||
if (Result.isSuccess(result)) return result.success;
|
||||
failures.push(`${source}: ${result.failure.detail}`);
|
||||
}
|
||||
return yield* new PullRequestOperationError({
|
||||
operation: "generateReviewGuide",
|
||||
detail: `Could not find head ${sha} of #${number}. ${failures.join(" ")}`,
|
||||
});
|
||||
});
|
||||
const resolvedHead = yield* fetchHead(detail.number, headSha, detail.headBranch);
|
||||
// The other layers of a stack are context the model may read across a seam. A layer that
|
||||
// cannot be fetched is still named in the prompt, without a head to read.
|
||||
const layers: ReadonlyArray<ReviewGuideStackLayer> = yield* Effect.forEach(
|
||||
@@ -239,10 +294,14 @@ const make = Effect.gen(function* () {
|
||||
if (layer.number === detail.number) return Effect.succeed({ ...described, headSha });
|
||||
const sha = layer.headSha;
|
||||
if (sha === undefined) return Effect.succeed({ ...described, headSha: null });
|
||||
return fetchHead(layer.number, sha).pipe(
|
||||
Effect.andThen(git.resolveCommit({ cwd: workspaceRoot, revision: sha })),
|
||||
Effect.as({ ...described, headSha: sha }),
|
||||
Effect.orElseSucceed(() => ({ ...described, headSha: null })),
|
||||
return fetchHead(layer.number, sha, layer.headBranch).pipe(
|
||||
Effect.map(({ commitSha }) => ({ ...described, headSha: commitSha })),
|
||||
Effect.catch((error) =>
|
||||
Effect.logInfo("Review guide could not read a stack layer's head", {
|
||||
layer: layer.number,
|
||||
detail: error.detail,
|
||||
}).pipe(Effect.as({ ...described, headSha: null })),
|
||||
),
|
||||
);
|
||||
},
|
||||
{ concurrency: 4 },
|
||||
@@ -255,7 +314,7 @@ const make = Effect.gen(function* () {
|
||||
yield* git.execute({
|
||||
operation: "PullRequestReviewGuideService.addWorktree",
|
||||
cwd: workspaceRoot,
|
||||
args: ["worktree", "add", "--detach", "--quiet", path, headSha],
|
||||
args: ["worktree", "add", "--detach", "--quiet", path, resolvedHead.commitSha],
|
||||
timeoutMs: 5 * 60_000,
|
||||
});
|
||||
yield* Effect.addFinalizer(() =>
|
||||
|
||||
@@ -18,6 +18,11 @@ it.effect.each([
|
||||
commit: { hash: "9f3a7c2d8b1e6f045a9c3d7e2b8f1a6c0d4e5f93" },
|
||||
headSha: "9f3a7c2d8b1e6f045a9c3d7e2b8f1a6c0d4e5f93",
|
||||
},
|
||||
{
|
||||
metadata: "short source commit hash",
|
||||
commit: { hash: "9f3a7c2d8b1e" },
|
||||
headSha: "9f3a7c2d8b1e",
|
||||
},
|
||||
{ metadata: "absent commit", commit: undefined, headSha: undefined },
|
||||
{ metadata: "null commit", commit: null, headSha: undefined },
|
||||
{ metadata: "absent hash", commit: {}, headSha: undefined },
|
||||
|
||||
Reference in new issue
Block a user