Skip to content

Commit 63c0d76

Browse files
feat(gitcafe): request reviewers and apply labels on GitCafe pull requests
GitCafe pull requests now support reviewer requests and labels. The PR detail view also shows reviewers and labels, which it never did: a single pull request read from GitCafe carries neither, so they are read from their own endpoints. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 0bdf33a commit 63c0d76

4 files changed

Lines changed: 248 additions & 16 deletions

File tree

‎docs/user/source-control.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ kept in the server's secret store, applies right away, and is used before `CAFE_
116116
sent to `git.cafe`; `CAFE_TOKEN` follows `CAFE_HOST` if you set it. Repositories on
117117
`staging.git.cafe` need their own login with `--host https://staging.git.cafe/api`.
118118

119-
Line comments, reviewer requests, and labels are not available for GitCafe pull requests yet.
119+
Line comments are not available for GitCafe pull requests yet.
120120
Merging a stack lands it through GitCafe, and updating a branch restacks the layers above it.
121121

122122
## Start, clone, or publish a project

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

Lines changed: 94 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ const pull = {
104104
sourceRepo: null,
105105
capabilities: { comment: true, review: true, merge: true, edit: true, moderate: true },
106106
};
107+
const labels = [
108+
{ id: "label_bug", name: "bug", color: "#d73a4a", description: null },
109+
{ id: "label_docs", name: "docs", color: "#0075ca", description: "Documentation" },
110+
];
107111
const root = {
108112
id: "root",
109113
threadId: "root",
@@ -439,13 +443,15 @@ describe("GitCafePullRequestProvider", () => {
439443
},
440444
);
441445

442-
it.effect("reads line counts, open merge strategies and requested reviewers", () => {
446+
it.effect("reads line counts, open merge strategies, reviewers and labels", () => {
443447
const snapshot = { version: 4, headOid, comparisonBaseOid: baseOid };
444448
const files = (from: number, count: number) =>
445449
Array.from({ length: count }, (_, index) => ({ path: `f${from + index}.ts` }));
446450
let changePage = 0;
447451
const server = fakeGitCafe({
448-
"GET /repos/owner/repo/pulls/7": { ...pull, reviewers: [{ actor }] },
452+
"GET /repos/owner/repo/pulls/7": pull,
453+
"GET /repos/owner/repo/pulls/7/reviewers": { items: [{ id: "act_one", actor }], next: null },
454+
"GET /repos/owner/repo/pulls/7/labels": { items: [labels[0]], next: null },
449455
"GET /repos/owner/repo/pulls/7/status": {
450456
merge: {
451457
conflicts: "unknown",
@@ -501,6 +507,92 @@ describe("GitCafePullRequestProvider", () => {
501507
detail.reviewers.map((reviewer) => reviewer.login),
502508
["alice"],
503509
);
510+
assert.deepStrictEqual(detail.reviewRequestLogins, ["alice"]);
511+
assert.deepStrictEqual(detail.labels, [{ name: "bug", color: "#d73a4a" }]);
512+
}).pipe(Effect.provide(server.layer));
513+
});
514+
515+
it.effect("requests and withdraws reviewers by rewriting the whole list", () => {
516+
const bob = { ...actor, actorId: "act_two", handle: "bob" };
517+
let requested = [{ id: "act_two", actor: bob }];
518+
const server = fakeGitCafe({
519+
"GET /repos/owner/repo/pulls/7": pull,
520+
"GET /repos/owner/repo/pulls/7/reviewer-candidates": {
521+
items: [
522+
{ id: "act_one", actor },
523+
{ id: "act_two", actor: bob },
524+
{ id: "act_bot", actor: { ...actor, actorId: "act_bot", handle: "lint", bot: true } },
525+
],
526+
next: null,
527+
},
528+
"GET /repos/owner/repo/pulls/7/reviewers": () => ({ items: requested, next: null }),
529+
"PUT /repos/owner/repo/pulls/7/reviewers": { version: 5, items: [] },
530+
});
531+
return Effect.gen(function* () {
532+
const provider = yield* GitCafePullRequestProvider.make;
533+
const list = yield* provider.listReviewerCandidates(target);
534+
// The author, alice, cannot review her own pull request.
535+
assert.deepStrictEqual(
536+
list.candidates.map((c) => [c.id, c.login, c.isRequested, c.isBot ?? false]),
537+
[
538+
["act_two", "bob", true, false],
539+
["act_bot", "lint", false, true],
540+
],
541+
);
542+
543+
const set = (id: string, on: boolean) =>
544+
provider.setReviewerRequest({
545+
...target,
546+
reviewers: [{ id, kind: "user" }],
547+
requested: on,
548+
});
549+
yield* set("act_bot", true);
550+
assert.deepStrictEqual(server.sent.at(-1)?.body, {
551+
expectedVersion: 4,
552+
actorIds: ["act_two", "act_bot"],
553+
});
554+
yield* set("act_two", false);
555+
assert.deepStrictEqual(server.sent.at(-1)?.body, { expectedVersion: 4, actorIds: [] });
556+
557+
requested = [];
558+
yield* set("act_two", false);
559+
assert.strictEqual(server.writes().length, 2);
560+
}).pipe(Effect.provide(server.layer));
561+
});
562+
563+
it.effect("applies and removes labels by name through their ids", () => {
564+
const server = fakeGitCafe({
565+
"GET /repos/owner/repo/pulls/7": pull,
566+
"GET /repos/owner/repo/labels": { items: labels, next: null },
567+
"GET /repos/owner/repo/pulls/7/labels": { items: [labels[0]], next: null },
568+
"PUT /repos/owner/repo/pulls/7/labels": { version: 5, items: [] },
569+
});
570+
return Effect.gen(function* () {
571+
const provider = yield* GitCafePullRequestProvider.make;
572+
const list = yield* provider.listLabelCandidates!(target);
573+
assert.deepStrictEqual(
574+
list.candidates.map((label) => [label.name, label.isApplied]),
575+
[
576+
["bug", true],
577+
["docs", false],
578+
],
579+
);
580+
581+
yield* provider.setLabels!({ ...target, labels: ["docs"], applied: true });
582+
assert.deepStrictEqual(server.sent.at(-1)?.body, {
583+
expectedVersion: 4,
584+
labelIds: ["label_bug", "label_docs"],
585+
});
586+
yield* provider.setLabels!({ ...target, labels: ["bug"], applied: false });
587+
assert.deepStrictEqual(server.sent.at(-1)?.body, { expectedVersion: 4, labelIds: [] });
588+
589+
const missing = yield* provider.setLabels!({
590+
...target,
591+
labels: ["nope"],
592+
applied: true,
593+
}).pipe(Effect.result);
594+
assert.strictEqual(missing._tag, "Failure");
595+
assert.strictEqual(server.writes().length, 2);
504596
}).pipe(Effect.provide(server.layer));
505597
});
506598

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

Lines changed: 113 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,15 @@ const CAPABILITIES: PullRequestCapabilities = {
3131
stacks: true,
3232
stackActions: true,
3333
reactions: true,
34-
labels: false,
34+
labels: true,
3535
// Line comments go through GitCafe's review drafts, which arrive with revision fencing.
3636
review: {
3737
inlineComment: false,
3838
reply: true,
3939
resolve: true,
4040
verdicts: ["approve", "request-changes", "comment"],
4141
},
42-
reviewers: { request: false, listCandidates: false },
42+
reviewers: { request: true, listCandidates: true },
4343
edit: { changeRequest: true, comment: true },
4444
};
4545

@@ -93,6 +93,10 @@ const Status = Schema.Struct({
9393
total: NonNegativeInt,
9494
}),
9595
});
96+
const Listed = Schema.Struct({
97+
items: Schema.Array(Schema.Struct({ id: TrimmedNonEmptyString })),
98+
next: Schema.NullOr(Schema.String),
99+
});
96100
const FilterOptions = Schema.Struct({
97101
actors: Schema.Array(
98102
Schema.Struct({ actorId: TrimmedNonEmptyString, handle: TrimmedNonEmptyString }),
@@ -254,6 +258,39 @@ export const make = Effect.gen(function* () {
254258
),
255259
);
256260

261+
/**
262+
* GitCafe replaces a pull request's whole reviewer or label list, so one toggle reads the
263+
* list, changes it, and writes it back fenced on the pull request's version.
264+
*/
265+
const toggleListed = Effect.fn("GitCafePullRequestProvider.toggleListed")(function* (
266+
operation: string,
267+
input: PullRef,
268+
list: { readonly name: "reviewers" | "labels"; readonly key: "actorIds" | "labelIds" },
269+
ids: ReadonlyArray<string>,
270+
on: boolean,
271+
) {
272+
const path = yield* pullPath(operation, input);
273+
const [pull, listed] = yield* Effect.all(
274+
[readPull(operation, input), readPages(operation, input, `${path}/${list.name}`, Listed)],
275+
{ concurrency: 2 },
276+
);
277+
const current = listed.items.map((item) => item.id);
278+
const next = on
279+
? [...new Set([...current, ...ids])]
280+
: current.filter((id) => !ids.includes(id));
281+
if (next.length === current.length) return;
282+
yield* read(
283+
operation,
284+
input,
285+
{
286+
method: "PUT",
287+
path: `${path}/${list.name}`,
288+
body: { expectedVersion: pull.version, [list.key]: next },
289+
},
290+
Versioned,
291+
);
292+
});
293+
257294
/**
258295
* GitCafe's stack lists no heads, and they are what a land or restack is fenced on, so each
259296
* open layer's pull is read.
@@ -537,7 +574,8 @@ export const make = Effect.gen(function* () {
537574
const pull = yield* readPull("getChangeRequest", input);
538575
const path = yield* pullPath("getChangeRequest", input);
539576
const base = yield* repositoryPath("getChangeRequest", input);
540-
const [status, checks, changes] = yield* Effect.all(
577+
// A single pull request read carries neither its reviewers nor its labels.
578+
const [status, checks, changes, reviewers, labels] = yield* Effect.all(
541579
[
542580
read("getChangeRequest", input, { path: `${path}/status` }, Status),
543581
pull.headOid === null
@@ -551,12 +589,15 @@ export const make = Effect.gen(function* () {
551589
pull.headOid === null
552590
? Effect.succeed(null)
553591
: readLineStats(input, pull.version).pipe(Effect.orElseSucceed(() => null)),
592+
readPages("getChangeRequest", input, `${path}/reviewers`, Json.GitCafeActorEntries),
593+
readPages("getChangeRequest", input, `${path}/labels`, Json.GitCafeLabels),
554594
],
555-
{ concurrency: 3 },
595+
{ concurrency: 5 },
556596
);
597+
const listed = { ...pull, reviewers: reviewers.items, labels: labels.items };
557598
const permissions = Json.toViewerPermissions(pull);
558599
return {
559-
...Json.toChangeRequest(pull, input),
600+
...Json.toChangeRequest(listed, input),
560601
headSha: pull.headOid,
561602
body: pull.description ?? "",
562603
...(pull.sourceRepo === null
@@ -567,7 +608,7 @@ export const make = Effect.gen(function* () {
567608
changedFiles: changes?.changedFiles ?? 0,
568609
closedAt: pull.closedAt,
569610
mergedAt: pull.mergedAt,
570-
reviewers: Json.toReviewers(pull, input.host),
611+
reviewers: Json.toReviewers(listed, input.host),
571612
checks: checks === null ? [] : Json.toChecks(checks),
572613
...(status.checks.total === 0
573614
? {}
@@ -758,12 +799,73 @@ export const make = Effect.gen(function* () {
758799
Written,
759800
);
760801
}),
761-
listReviewerCandidates: () =>
762-
Effect.fail(
763-
failure("listReviewerCandidates", "GitCafe reviewer requests are not supported."),
802+
listReviewerCandidates: Effect.fn("GitCafePullRequestProvider.listReviewerCandidates")(
803+
function* (input) {
804+
const operation = "listReviewerCandidates";
805+
const path = yield* pullPath(operation, input);
806+
const [pull, candidates, reviewers] = yield* Effect.all(
807+
[
808+
readPull(operation, input),
809+
readPages(operation, input, `${path}/reviewer-candidates`, Json.GitCafeActorEntries),
810+
readPages(operation, input, `${path}/reviewers`, Listed),
811+
],
812+
{ concurrency: 3 },
813+
);
814+
return {
815+
candidates: Json.toReviewerCandidates(candidates, {
816+
requested: new Set(reviewers.items.map((reviewer) => reviewer.id)),
817+
authorActorId: pull.author.actorId,
818+
host: input.host,
819+
}),
820+
truncated: candidates.next !== null,
821+
};
822+
},
823+
),
824+
setReviewerRequest: (input) =>
825+
toggleListed(
826+
"setReviewerRequest",
827+
input,
828+
{ name: "reviewers", key: "actorIds" },
829+
input.reviewers.map((reviewer) => reviewer.id),
830+
input.requested,
764831
),
765-
setReviewerRequest: () =>
766-
Effect.fail(failure("setReviewerRequest", "GitCafe reviewer requests are not supported.")),
832+
listLabelCandidates: Effect.fn("GitCafePullRequestProvider.listLabelCandidates")(
833+
function* (input) {
834+
const operation = "listLabelCandidates";
835+
const base = yield* repositoryPath(operation, input);
836+
const [labels, applied] = yield* Effect.all(
837+
[
838+
readPages(operation, input, `${base}/labels`, Json.GitCafeLabels),
839+
readPages(operation, input, `${base}/pulls/${input.number}/labels`, Listed),
840+
],
841+
{ concurrency: 2 },
842+
);
843+
const appliedIds = new Set(applied.items.map((label) => label.id));
844+
return {
845+
candidates: labels.items.map((label) => ({
846+
name: label.name,
847+
color: label.color,
848+
description: label.description ?? null,
849+
isApplied: appliedIds.has(label.id),
850+
})),
851+
truncated: labels.next !== null,
852+
};
853+
},
854+
),
855+
setLabels: Effect.fn("GitCafePullRequestProvider.setLabels")(function* (input) {
856+
const base = yield* repositoryPath("setLabels", input);
857+
const labels = yield* readPages("setLabels", input, `${base}/labels`, Json.GitCafeLabels);
858+
const selected = labels.items.filter((label) => input.labels.includes(label.name));
859+
if (selected.length !== input.labels.length)
860+
return yield* failure("setLabels", "One or more requested labels could not be found.");
861+
yield* toggleListed(
862+
"setLabels",
863+
input,
864+
{ name: "labels", key: "labelIds" },
865+
selected.map((label) => label.id),
866+
input.applied,
867+
);
868+
}),
767869
replyToThread: Effect.fn("GitCafePullRequestProvider.replyToThread")(function* (input) {
768870
if (!isId(input.threadId))
769871
return yield* failure("replyToThread", "GitCafe thread ids are opaque slugs.");

0 commit comments

Comments
 (0)