Skip to content

Commit bedb2a1

Browse files
committed
fix(server): verify PR access before stacks fallback
1 parent 3bfbe59 commit bedb2a1

2 files changed

Lines changed: 122 additions & 38 deletions

File tree

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

Lines changed: 105 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -652,50 +652,122 @@ layer("GitHubPullRequestCli.layer", (it) => {
652652
);
653653

654654
it.effect.each([
655-
["gh: Not Found (HTTP 404)", null],
656-
["HTTP 404: Not Found (https://github.example/api/v3/repos/acme/web/stacks)", null],
657-
["gh: Resource not accessible by integration (HTTP 403)", "GitHubCliCommandError"],
658-
["gh: Service Unavailable (HTTP 503)", "GitHubCliCommandError"],
659-
["dial tcp: lookup github.example: no such host", "GitHubCliCommandError"],
660-
["To get started with GitHub CLI, please run: gh auth login", "GitHubCliAuthenticationError"],
661-
["gh: Too Many Requests (HTTP 429)", "GitHubCliRateLimitError"],
662-
] as const)("classifies the raw stacks API failure: %s", ([stderr, expectedError]) =>
663-
Effect.gen(function* () {
664-
const vcs = yield* VcsProcess.make.pipe(
665-
Effect.provideService(ProcessRunner.ProcessRunner, {
666-
run: () =>
655+
["gh: Not Found (HTTP 404)", "", null],
656+
["HTTP 404: Not Found (https://github.example/api/v3/repos/acme/web/stacks)", "", null],
657+
["gh: Resource not accessible by integration (HTTP 403)", undefined, "GitHubCliCommandError"],
658+
["gh: Service Unavailable (HTTP 503)", undefined, "GitHubCliCommandError"],
659+
["dial tcp: lookup github.example: no such host", undefined, "GitHubCliCommandError"],
660+
[
661+
"To get started with GitHub CLI, please run: gh auth login",
662+
undefined,
663+
"GitHubCliAuthenticationError",
664+
],
665+
["gh: Too Many Requests (HTTP 429)", undefined, "GitHubCliRateLimitError"],
666+
["gh: Not Found (HTTP 404)", "gh: Not Found (HTTP 404)", "GitHubPullRequestNotFoundError"],
667+
["gh: Not Found (HTTP 404)", "gh: Bad credentials (HTTP 401)", "GitHubCliCommandError"],
668+
[
669+
"gh: Not Found (HTTP 404)",
670+
"gh: Resource not accessible by integration (HTTP 403)",
671+
"GitHubCliCommandError",
672+
],
673+
[
674+
"gh: Not Found (HTTP 404)",
675+
"To get started with GitHub CLI, please run: gh auth login",
676+
"GitHubCliAuthenticationError",
677+
],
678+
["gh: Not Found (HTTP 404)", "gh: Service Unavailable (HTTP 503)", "GitHubCliCommandError"],
679+
["gh: Not Found (HTTP 404)", "gh: Too Many Requests (HTTP 429)", "GitHubCliRateLimitError"],
680+
] as const)(
681+
"classifies the raw stacks API failure: %s; PR access: %s",
682+
([stderr, accessStderr, expectedError]) =>
683+
Effect.gen(function* () {
684+
const run = vi.fn<ProcessRunner.ProcessRunner["Service"]["run"]>();
685+
for (const response of accessStderr === undefined ? [stderr] : [stderr, accessStderr]) {
686+
run.mockReturnValueOnce(
667687
Effect.succeed({
668688
stdout: "",
669-
stderr,
670-
code: ChildProcessSpawner.ExitCode(1),
689+
stderr: response,
690+
code: ChildProcessSpawner.ExitCode(response === "" ? 0 : 1),
671691
timedOut: false,
672692
stdoutTruncated: false,
673693
stderrTruncated: false,
674694
stdoutInvalidUtf8: false,
675695
stderrInvalidUtf8: false,
676696
}),
677-
}),
678-
);
679-
const github = yield* GitHubCli.make.pipe(
680-
Effect.provideService(VcsProcess.VcsProcess, vcs),
681-
Effect.provide(Layer.merge(GitHubGraphQlBudget.layer, SourceControlRateLimit.layer)),
682-
);
683-
const cli = yield* GitHubPullRequestCli.make.pipe(
684-
Effect.provideService(GitHubCli.GitHubCli, github),
685-
Effect.provide(GitHubGraphQlBudget.layer),
697+
);
698+
}
699+
const vcs = yield* VcsProcess.make.pipe(
700+
Effect.provideService(ProcessRunner.ProcessRunner, { run }),
701+
);
702+
const github = yield* GitHubCli.make.pipe(
703+
Effect.provideService(VcsProcess.VcsProcess, vcs),
704+
Effect.provide(Layer.merge(GitHubGraphQlBudget.layer, SourceControlRateLimit.layer)),
705+
);
706+
const cli = yield* GitHubPullRequestCli.make.pipe(
707+
Effect.provideService(GitHubCli.GitHubCli, github),
708+
Effect.provide(GitHubGraphQlBudget.layer),
709+
);
710+
const read = cli.getPullRequestStack({
711+
cwd: "/w",
712+
repository: "acme/web",
713+
host: "github.example",
714+
number: 7,
715+
includeDetails: true,
716+
});
717+
if (expectedError === null) {
718+
assert.isNull(yield* read);
719+
} else {
720+
const error = yield* Effect.flip(read);
721+
assert.strictEqual(error._tag, expectedError);
722+
}
723+
expect(run).toHaveBeenCalledTimes(accessStderr === undefined ? 1 : 2);
724+
if (accessStderr !== undefined) {
725+
expect(run.mock.calls[1]?.[0]?.args).toEqual([
726+
"api",
727+
"--hostname",
728+
"github.example",
729+
"repos/acme/web/pulls/7/commits?per_page=1",
730+
"--silent",
731+
]);
732+
}
733+
}),
734+
);
735+
736+
it.effect("preserves a known stack when its detail request returns not found", () =>
737+
Effect.gen(function* () {
738+
mockedExecute.mockReturnValueOnce(
739+
Effect.succeed(
740+
output(
741+
encodeJson([
742+
{
743+
number: 3,
744+
url: "https://api.github.com/repos/acme/web/stacks/3",
745+
base: { ref: "main" },
746+
pull_requests: [{ number: 7, head: { ref: "feat/two" }, state: "open" }],
747+
},
748+
]),
749+
),
750+
),
686751
);
687-
const read = cli.getPullRequestStack({
752+
const failure = new GitHubCli.GitHubPullRequestNotFoundError({
753+
command: "gh",
688754
cwd: "/w",
689-
repository: "acme/web",
690-
host: "github.example",
691-
number: 7,
755+
cause: new Error("gh: Not Found (HTTP 404)"),
692756
});
693-
if (expectedError === null) {
694-
assert.isNull(yield* read);
695-
} else {
696-
const error = yield* Effect.flip(read);
697-
assert.strictEqual(error._tag, expectedError);
698-
}
757+
mockedExecute.mockReturnValueOnce(Effect.fail(failure));
758+
const cli = yield* GitHubPullRequestCli.GitHubPullRequestCli;
759+
const error = yield* Effect.flip(
760+
cli.getPullRequestStack({
761+
cwd: "/w",
762+
repository: "acme/web",
763+
host: "github.com",
764+
number: 7,
765+
includeDetails: true,
766+
}),
767+
);
768+
769+
assert.strictEqual(error, failure);
770+
expect(mockedExecute).toHaveBeenCalledTimes(2);
699771
}),
700772
);
701773

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

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2028,6 +2028,23 @@ export const make = Effect.gen(function* () {
20282028
}),
20292029
);
20302030
}),
2031+
// A listing 404 can mean an unsupported stacks API or denied access. The PR
2032+
// commits endpoint requires the same pull-request read permission as stacks.
2033+
Effect.catchTags({
2034+
GitHubPullRequestNotFoundError: () =>
2035+
github
2036+
.execute({
2037+
cwd: input.cwd,
2038+
args: [
2039+
"api",
2040+
"--hostname",
2041+
input.host,
2042+
`repos/${owner}/${name}/pulls/${input.number}/commits?per_page=1`,
2043+
"--silent",
2044+
],
2045+
})
2046+
.pipe(Effect.as(null)),
2047+
}),
20312048
Effect.flatMap((stack) => {
20322049
if (!input.includeDetails || stack === null) return Effect.succeed(stack);
20332050
return github
@@ -2056,11 +2073,6 @@ export const make = Effect.gen(function* () {
20562073
}),
20572074
);
20582075
}),
2059-
// Hosts without the stacks preview return 404. Other failures must preserve the
2060-
// previously synced stack and let the caller retry.
2061-
Effect.catchTags({
2062-
GitHubPullRequestNotFoundError: () => Effect.succeed(null),
2063-
}),
20642076
);
20652077
},
20662078

0 commit comments

Comments
 (0)