Skip to content

Commit 8621df1

Browse files
committed
fix(source-control): invalidate github reads after stack pushes
1 parent 4884e29 commit 8621df1

2 files changed

Lines changed: 153 additions & 0 deletions

File tree

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

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import * as Effect from "effect/Effect";
55
import * as Clock from "effect/Clock";
66
import * as Deferred from "effect/Deferred";
77
import * as Fiber from "effect/Fiber";
8+
import * as FileSystem from "effect/FileSystem";
89
import * as Layer from "effect/Layer";
10+
import * as ChildProcessSpawner from "effect/process/ChildProcessSpawner";
911
import * as Schema from "effect/Schema";
1012
import * as TestClock from "effect/testing/TestClock";
1113
import * as Redacted from "effect/Redacted";
@@ -637,6 +639,156 @@ it.effect("separates overlapping unpinned permission reads after a saved account
637639
}),
638640
);
639641

642+
it.effect.each([false, true])(
643+
"starts fresh reads after a stack rebase (partial failure: %s)",
644+
(partialFailure) =>
645+
Effect.gen(function* () {
646+
const detailStarted = yield* Deferred.make<void>();
647+
const runsStarted = yield* Deferred.make<void>();
648+
const freshRunsStarted = yield* Deferred.make<void>();
649+
const release = yield* Deferred.make<void>();
650+
let pushed = false;
651+
let rebases = 0;
652+
let pushes = 0;
653+
const input = { cwd: "/w", repository: "acme/web", host: "github.com", number: 7 };
654+
mockedExecute.mockImplementation((call) =>
655+
Effect.gen(function* () {
656+
if (call.kind === "rest" && call.path.includes("/stacks?")) {
657+
return output(
658+
encodeJson([
659+
{
660+
number: 50,
661+
url: "https://api.github.com/repos/acme/web/stacks/50",
662+
base: { ref: "main" },
663+
pull_requests: [
664+
{ number: 7, head: { ref: "feature", sha: "abc123" }, state: "open" },
665+
{ number: 8, head: { ref: "top", sha: "def456" }, state: "open" },
666+
],
667+
},
668+
]),
669+
);
670+
}
671+
if (call.operation === "runGitHubStackAction") {
672+
return output(
673+
encodeJson({
674+
data: {
675+
repository: {
676+
pr7: {
677+
headRepository: { viewerPermission: "WRITE" },
678+
maintainerCanModify: false,
679+
},
680+
pr8: {
681+
headRepository: { viewerPermission: "WRITE" },
682+
maintainerCanModify: false,
683+
},
684+
},
685+
},
686+
}),
687+
);
688+
}
689+
if (call.kind === "graphql" && call.query.includes("headRefName: $head")) {
690+
if (pushed) yield* Deferred.await(freshRunsStarted);
691+
return output(encodeJson(heads(pushed ? [] : [7])));
692+
}
693+
const beforePush = !pushed;
694+
if (beforePush) {
695+
yield* Deferred.succeed(call.kind === "rest" ? runsStarted : detailStarted, undefined);
696+
yield* Deferred.await(release);
697+
}
698+
if (call.kind === "rest" && !beforePush) {
699+
yield* Deferred.succeed(freshRunsStarted, undefined);
700+
}
701+
return output(
702+
encodeJson(
703+
call.kind === "rest"
704+
? workflowRuns(beforePush ? [10] : [])
705+
: coreResponse({ headRefOid: beforePush ? "abc123" : "new-sha" }),
706+
),
707+
);
708+
}),
709+
);
710+
const cli = yield* GitHubPullRequestApi.make.pipe(
711+
Effect.provide(
712+
Layer.mergeAll(
713+
mockApi.pipe(Layer.provide(GitHubQuota.layer), Layer.provide(NodeCrypto.layer)),
714+
FileSystem.layerNoop({
715+
makeTempDirectoryScoped: () => Effect.succeed("/scratch"),
716+
}),
717+
TestSourceControlHost.layer({
718+
process: {
719+
run: (request) =>
720+
Effect.sync(() => {
721+
expect(request.command).toBe("git");
722+
expect(request.operation).toBe("cascadeRebaseStack");
723+
if (request.args[0] === "push") {
724+
pushed = true;
725+
pushes++;
726+
}
727+
const conflict =
728+
request.args[0] === "rebase" &&
729+
request.args[1] !== "--abort" &&
730+
++rebases === 2 &&
731+
partialFailure;
732+
return TestSourceControlHost.processOutput(
733+
["rev-parse", "merge-base"].includes(request.args[0]!) ? "new-sha\n" : "",
734+
{ exitCode: ChildProcessSpawner.ExitCode(conflict ? 1 : 0) },
735+
);
736+
}),
737+
},
738+
}),
739+
),
740+
),
741+
);
742+
const workflowInput = {
743+
...input,
744+
headSha: "abc123",
745+
headBranch: "feat/page",
746+
headRepositoryOwner: "octocat",
747+
isCrossRepository: true as const,
748+
};
749+
const olderDetail = yield* cli.getPullRequestDetail(input).pipe(Effect.forkChild);
750+
const olderRuns = yield* cli
751+
.listWorkflowRunsRequiringApproval(workflowInput)
752+
.pipe(Effect.forkChild);
753+
yield* Deferred.await(detailStarted);
754+
yield* Deferred.await(runsStarted);
755+
const action = yield* cli
756+
.runPullRequestAction({
757+
...input,
758+
number: 8,
759+
stackNumber: 50,
760+
action: "update-branch",
761+
expectedStackHeads: [
762+
{ number: 7, headSha: "abc123" },
763+
{ number: 8, headSha: "def456" },
764+
],
765+
})
766+
.pipe(Effect.result);
767+
expect(action._tag).toBe(partialFailure ? "Failure" : "Success");
768+
if (partialFailure) {
769+
expect(action).toMatchObject({
770+
failure: { _tag: "GitHubStackRebaseFailedError", completed: 1 },
771+
});
772+
}
773+
expect(pushes).toBe(partialFailure ? 1 : 2);
774+
expect((yield* cli.getPullRequestDetail(input)).headSha).toBe("new-sha");
775+
expect(
776+
yield* cli.listWorkflowRunsRequiringApproval(workflowInput).pipe(Effect.result),
777+
).toMatchObject({
778+
_tag: "Failure",
779+
failure: { _tag: "GitHubWorkflowApprovalRefusedError", reason: "head-not-unique" },
780+
});
781+
// The stack action still needs only its membership and permission reads.
782+
expect(
783+
mockedExecute.mock.calls.filter(([call]) => call.operation === "runGitHubStackAction"),
784+
).toHaveLength(2);
785+
expect(mockedExecute.mock.calls).toHaveLength(8);
786+
yield* Deferred.succeed(release, undefined);
787+
expect((yield* Fiber.join(olderDetail)).headSha).toBe("abc123");
788+
expect((yield* Fiber.join(olderRuns)).map((run) => run.id)).toEqual([10]);
789+
}),
790+
);
791+
640792
layer("GitHubPullRequestApi.layer", (it) => {
641793
it.effect("holds background previews once an answer reports the reserve, until it resets", () =>
642794
Effect.gen(function* () {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2600,6 +2600,7 @@ export const make = Effect.gen(function* () {
26002600
runPullRequestAction: (input) => {
26012601
if (input.stackNumber !== undefined)
26022602
return runGitHubStackAction({ ...input, stackNumber: input.stackNumber }).pipe(
2603+
Effect.ensuring(forgetReads),
26032604
Effect.provideService(GitHubApi.GitHubApi, api),
26042605
Effect.provideService(SourceControlHost.SourceControlHost, sourceControlHost),
26052606
Effect.provideService(FileSystem.FileSystem, fileSystem),

0 commit comments

Comments
 (0)