Skip to content

Commit 3a2253c

Browse files
fix(gitcafe): name the missing scopes when a token cannot request reviewers
Reviewer controls show for anyone who may edit the pull request, but the candidate list and reviewer writes also need pr:review:read and pr:reviewers:write. A token without them got GitCafe's bare refusal; it now gets an error naming the scopes. GitCafe offers no way to read a token's scopes up front. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent b587109 commit 3a2253c

2 files changed

Lines changed: 49 additions & 6 deletions

File tree

‎packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.test.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,19 @@ describe("GitCafePullRequestProvider", () => {
605605
}).pipe(Effect.provide(server.layer));
606606
});
607607

608+
it.effect("names the missing scopes when the token may not request reviewers", () => {
609+
const server = fakeGitCafe({
610+
"GET /repos/owner/repo/pulls/7": pull,
611+
"GET /repos/owner/repo/pulls/7/reviewer-candidates": new Response("", { status: 403 }),
612+
"GET /repos/owner/repo/pulls/7/reviewers": { items: [], next: null },
613+
});
614+
return Effect.gen(function* () {
615+
const provider = yield* GitCafePullRequestProvider.make;
616+
const error = yield* Effect.flip(provider.listReviewerCandidates(target));
617+
assert.match(error.detail, /pr:reviewers:write/u);
618+
}).pipe(Effect.provide(server.layer));
619+
});
620+
608621
it.effect("applies and removes labels by name through their ids", () => {
609622
const server = fakeGitCafe({
610623
"GET /repos/owner/repo/pulls/7": pull,

‎packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.ts‎

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,31 @@ export const make = Effect.gen(function* () {
261261
),
262262
);
263263

264+
const isForbidden = (error: PullRequestProviderError) =>
265+
isApiError(error.cause) && error.cause.status === 403;
266+
267+
/**
268+
* GitCafe offers no way to read a token's scopes, so reviewer controls show
269+
* for anyone who may edit the pull request; a token without the reviewer
270+
* scopes learns which ones it lacks when it uses them.
271+
*/
272+
const explainReviewerScopes =
273+
(operation: string) =>
274+
<A, R>(effect: Effect.Effect<A, PullRequestProviderError, R>) =>
275+
effect.pipe(
276+
Effect.catch((error) =>
277+
isForbidden(error)
278+
? Effect.fail(
279+
failure(
280+
operation,
281+
"This GitCafe token cannot request reviewers. It needs the pr:review:read and pr:reviewers:write scopes.",
282+
error,
283+
),
284+
)
285+
: Effect.fail(error),
286+
),
287+
);
288+
264289
/**
265290
* GitCafe replaces a pull request's whole reviewer or label list, so one toggle reads the
266291
* list, changes it, and writes it back fenced on the pull request's version. The version is
@@ -595,9 +620,7 @@ export const make = Effect.gen(function* () {
595620
// Reviewers need `pr:review:read`, which a contents-only token lacks.
596621
readPages("getChangeRequest", input, `${path}/reviewers`, Json.GitCafeActorEntries).pipe(
597622
Effect.catch((error) =>
598-
isApiError(error.cause) && error.cause.status === 403
599-
? Effect.succeed({ items: [], next: null })
600-
: Effect.fail(error),
623+
isForbidden(error) ? Effect.succeed({ items: [], next: null }) : Effect.fail(error),
601624
),
602625
),
603626
readPages("getChangeRequest", input, `${path}/labels`, Json.GitCafeLabels),
@@ -818,8 +841,15 @@ export const make = Effect.gen(function* () {
818841
const [pull, candidates, reviewers] = yield* Effect.all(
819842
[
820843
readPull(operation, input),
821-
readPages(operation, input, `${path}/reviewer-candidates`, Json.GitCafeActorEntries),
822-
readPages(operation, input, `${path}/reviewers`, Listed),
844+
readPages(
845+
operation,
846+
input,
847+
`${path}/reviewer-candidates`,
848+
Json.GitCafeActorEntries,
849+
).pipe(explainReviewerScopes(operation)),
850+
readPages(operation, input, `${path}/reviewers`, Listed).pipe(
851+
explainReviewerScopes(operation),
852+
),
823853
],
824854
{ concurrency: 3 },
825855
);
@@ -840,7 +870,7 @@ export const make = Effect.gen(function* () {
840870
{ name: "reviewers", key: "actorIds" },
841871
input.reviewers.map((reviewer) => reviewer.id),
842872
input.requested,
843-
),
873+
).pipe(explainReviewerScopes("setReviewerRequest")),
844874
listLabelCandidates: Effect.fn("GitCafePullRequestProvider.listLabelCandidates")(
845875
function* (input) {
846876
const operation = "listLabelCandidates";

0 commit comments

Comments
 (0)