Skip to content

Commit f9c1d02

Browse files
fix(gitcafe): authored lists, merge progress, and author reviews match git.cafe (pingdotgg#23887)
Co-authored-by: Claude Opus 5.5 <[email protected]>
1 parent 11adcb0 commit f9c1d02

3 files changed

Lines changed: 134 additions & 27 deletions

File tree

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

Lines changed: 74 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,23 @@ describe("GitCafePullRequestProvider", () => {
271271
}).pipe(Effect.provide(server.layer));
272272
});
273273

274+
it.effect("keeps the timeline when GitCafe no longer has the head's commits", () => {
275+
const comment = { ...root, id: "c1", threadId: "c1", body: "Looks good", path: null };
276+
const server = fakeGitCafe({
277+
"GET /repos/owner/repo/pulls/7": { ...pull, state: "closed", closedAt: timestamp },
278+
"GET /repos/owner/repo/pulls/7/comments": { items: [comment], next: null },
279+
"GET /repos/owner/repo/pulls/7/reviews": { items: [], next: null },
280+
});
281+
return Effect.gen(function* () {
282+
const provider = yield* GitCafePullRequestProvider.make;
283+
const activity = yield* provider.getChangeRequestActivity(target);
284+
assert.deepStrictEqual(
285+
activity.comments.map((entry) => entry.body),
286+
["Looks good"],
287+
);
288+
}).pipe(Effect.provide(server.layer));
289+
});
290+
274291
it.effect("submits a review against the pull request's current head", () => {
275292
const server = fakeGitCafe({
276293
"GET /repos/owner/repo/pulls/7": pull,
@@ -381,9 +398,7 @@ describe("GitCafePullRequestProvider", () => {
381398
["reviewing", "reviewers"],
382399
] as const)("filters %s pull requests by the viewer's actor on GitCafe", ([involvement, key]) => {
383400
const server = fakeGitCafe({
384-
"GET /repos/owner/repo/filter-options": {
385-
actors: [{ actorId: "act_one", handle: "Alice" }],
386-
},
401+
"GET /auth/principal": { actorId: "act_one", handle: "Alice" },
387402
"GET /repos/owner/repo/pulls": { items: [pull], next: null },
388403
});
389404
return Effect.gen(function* () {
@@ -403,9 +418,9 @@ describe("GitCafePullRequestProvider", () => {
403418
}).pipe(Effect.provide(server.layer));
404419
});
405420

406-
it.effect("lists nothing for a viewer GitCafe has no actor for", () => {
421+
it.effect("lists nothing for a viewer the token does not belong to", () => {
407422
const server = fakeGitCafe({
408-
"GET /repos/owner/repo/filter-options": { actors: [] },
423+
"GET /auth/principal": { actorId: "act_two", handle: "bob" },
409424
});
410425
return Effect.gen(function* () {
411426
const provider = yield* GitCafePullRequestProvider.make;
@@ -419,7 +434,7 @@ describe("GitCafePullRequestProvider", () => {
419434
assert.deepStrictEqual(page.items, []);
420435
assert.deepStrictEqual(
421436
server.sent.map((request) => request.path),
422-
["/repos/owner/repo/filter-options"],
437+
["/auth/principal"],
423438
);
424439
}).pipe(Effect.provide(server.layer));
425440
});
@@ -557,6 +572,59 @@ describe("GitCafePullRequestProvider", () => {
557572
}).pipe(Effect.provide(server.layer));
558573
});
559574

575+
const detailWith = (overrides: Record<string, unknown>, reasons: ReadonlyArray<string> = []) =>
576+
fakeGitCafe({
577+
"GET /repos/owner/repo/pulls/7": { ...pull, headOid: null, ...overrides },
578+
"GET /repos/owner/repo/pulls/7/reviewers": { items: [], next: null },
579+
"GET /repos/owner/repo/pulls/7/labels": { items: [], next: null },
580+
"GET /repos/owner/repo/pulls/7/status": {
581+
merge: { conflicts: "none", fastForward: null, strategies: ["squash"], reasons },
582+
checks: { pending: 0, failing: 0, total: 0 },
583+
},
584+
});
585+
586+
it.effect("shows a merge GitCafe accepted as armed until it lands", () => {
587+
const server = detailWith({}, ["provider-merge-pending"]);
588+
return Effect.gen(function* () {
589+
const provider = yield* GitCafePullRequestProvider.make;
590+
const detail = yield* provider.getChangeRequest(target);
591+
assert.strictEqual(detail.autoMergeEnabled, true);
592+
assert.notInclude(detail.viewerPermissions?.actions ?? [], "merge");
593+
}).pipe(Effect.provide(server.layer));
594+
});
595+
596+
it.effect("reads a pull request GitHub merged as merged before the sync reports it", () => {
597+
const mergedAt = "2026-09-12T13:00:00Z";
598+
const server = detailWith({ providerMerge: { mergedAt } });
599+
return Effect.gen(function* () {
600+
const provider = yield* GitCafePullRequestProvider.make;
601+
const detail = yield* provider.getChangeRequest(target);
602+
assert.strictEqual(detail.state, "merged");
603+
assert.strictEqual(detail.mergedAt, mergedAt);
604+
}).pipe(Effect.provide(server.layer));
605+
});
606+
607+
it.effect.each([
608+
["their own pull request", actor],
609+
[
610+
"the pull request GitHub sync opened for them",
611+
{
612+
kind: "github",
613+
actorId: "act_github",
614+
login: "alice",
615+
avatarUrl: null,
616+
linkedProfile: { handle: "alice", actorId: "act_one" },
617+
},
618+
],
619+
] as const)("leaves the author only a comment on %s", ([, author]) => {
620+
const server = detailWith({ author, viewerActorId: "act_one" });
621+
return Effect.gen(function* () {
622+
const provider = yield* GitCafePullRequestProvider.make;
623+
const detail = yield* provider.getChangeRequest(target);
624+
assert.deepStrictEqual(detail.viewerPermissions?.verdicts, ["comment"]);
625+
}).pipe(Effect.provide(server.layer));
626+
});
627+
560628
it.effect("requests and withdraws reviewers by rewriting the whole list", () => {
561629
const bob = { ...actor, actorId: "act_two", handle: "bob" };
562630
let requested = [{ id: "act_two", actor: bob }];

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

Lines changed: 35 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ const mergeStrategyOpen = (status: typeof Status.Type, strategy: string) =>
7373
const Id = TrimmedNonEmptyString.check(Schema.isPattern(/^[A-Za-z0-9_-]+$/u));
7474
const isId = Schema.is(Id);
7575
const isApiError = Schema.is(GitCafeApi.GitCafeApiError);
76-
const Principal = Schema.Struct({ handle: TrimmedNonEmptyString });
76+
const Principal = Schema.Struct({ actorId: TrimmedNonEmptyString, handle: TrimmedNonEmptyString });
7777
const Written = Schema.Struct({ id: Id });
7878
const Versioned = Schema.Struct({ version: NonNegativeInt });
7979
const CommentDetail = Schema.Struct({ id: Id, version: NonNegativeInt });
@@ -85,6 +85,7 @@ const Status = Schema.Struct({
8585
fastForward: Schema.NullOr(Schema.Boolean),
8686
strategies: Schema.Array(Schema.String),
8787
permitted: Schema.optional(Schema.Boolean),
88+
reasons: Schema.optional(Schema.Array(Schema.String)),
8889
blockers: Schema.optional(
8990
Schema.Array(
9091
Schema.Struct({ blockedStrategies: Schema.optional(Schema.Array(Schema.String)) }),
@@ -97,15 +98,12 @@ const Status = Schema.Struct({
9798
total: NonNegativeInt,
9899
}),
99100
});
101+
/** A merge GitCafe accepted runs on afterwards: on GitHub for a synced pull request, or natively. */
102+
const MERGE_IN_FLIGHT = new Set(["provider-merge-pending", "native-operation-pending"]);
100103
const Listed = Schema.Struct({
101104
items: Schema.Array(Schema.Struct({ id: TrimmedNonEmptyString })),
102105
next: Schema.NullOr(Schema.String),
103106
});
104-
const FilterOptions = Schema.Struct({
105-
actors: Schema.Array(
106-
Schema.Struct({ actorId: TrimmedNonEmptyString, handle: TrimmedNonEmptyString }),
107-
),
108-
});
109107
const encodeActorIds = Schema.encodeSync(Schema.fromJsonString(Schema.Array(Schema.String)));
110108
const BranchCommit = Schema.Struct({ oid: TrimmedNonEmptyString });
111109
const Snapshot = {
@@ -136,6 +134,12 @@ const StackOutcome = Schema.Struct({ id: Schema.String, state: Schema.String });
136134
const FAILED_STACK_STATES = new Set(["failed", "stopped", "cancelled", "reconciliation_required"]);
137135

138136
type PullRef = ProviderRepositoryRef & { readonly number: number };
137+
138+
/** GitHub's merge counts as soon as GitCafe records it, ahead of the sync that reports it. */
139+
const settledMerge = (pull: Json.GitCafePullDetail) =>
140+
pull.state === "open" && pull.providerMerge != null
141+
? { state: "merged" as const, mergedAt: pull.providerMerge.mergedAt }
142+
: {};
139143
type Paged<A> = { readonly items: ReadonlyArray<A>; readonly next: string | null };
140144

141145
function stackHeadsCurrent(
@@ -542,14 +546,15 @@ export const make = Effect.gen(function* () {
542546
const involved = input.involvement === "authored" || input.involvement === "reviewing";
543547
let actorId: string | undefined;
544548
if (involved) {
545-
// A host narrows as far as it can: when the actor list can't be read, the listing comes
546-
// back unnarrowed and the page's own involvement filter does the rest. A refused token
547-
// or a rate limit still fails, since the service acts on those for the whole host.
548-
const options = yield* read(
549+
// The viewer is the token's principal, whose actor also matches the pull requests GitHub
550+
// sync authored for its linked profile. When it can't be read, the listing comes back
551+
// unnarrowed and the page's own involvement filter does the rest. A refused token or a
552+
// rate limit still fails, since the service acts on those for the whole host.
553+
const principal = yield* read(
549554
"listChangeRequests",
550555
input,
551-
{ path: `${base}/filter-options` },
552-
FilterOptions,
556+
{ path: "/auth/principal" },
557+
Principal,
553558
).pipe(
554559
Effect.map(Option.some),
555560
Effect.catchTags({
@@ -559,11 +564,10 @@ export const make = Effect.gen(function* () {
559564
: Effect.succeed(Option.none()),
560565
}),
561566
);
562-
if (Option.isSome(options)) {
563-
actorId = options.value.actors.find(
564-
(actor) => actor.handle.toLowerCase() === input.viewer.toLowerCase(),
565-
)?.actorId;
566-
if (actorId === undefined) return { items: [], truncated: false, continues: false };
567+
if (Option.isSome(principal)) {
568+
if (principal.value.handle.toLowerCase() !== input.viewer.toLowerCase())
569+
return { items: [], truncated: false, continues: false };
570+
actorId = principal.value.actorId;
567571
}
568572
}
569573
const limit = Math.max(1, input.limit);
@@ -624,6 +628,7 @@ export const make = Effect.gen(function* () {
624628
...(changes === null ? {} : changes),
625629
closedAt: pull.closedAt,
626630
mergedAt: pull.mergedAt,
631+
...settledMerge(pull),
627632
};
628633
},
629634
),
@@ -658,8 +663,10 @@ export const make = Effect.gen(function* () {
658663
);
659664
const listed = { ...pull, reviewers: reviewers.items, labels: labels.items };
660665
const permissions = Json.toViewerPermissions(pull);
666+
const merging = status.merge.reasons?.some((reason) => MERGE_IN_FLIGHT.has(reason)) ?? false;
661667
return {
662668
...Json.toChangeRequest(listed, input),
669+
autoMergeEnabled: merging,
663670
headSha: pull.headOid,
664671
body: pull.description ?? "",
665672
...(pull.sourceRepo === null
@@ -670,6 +677,7 @@ export const make = Effect.gen(function* () {
670677
changedFiles: changes?.changedFiles ?? 0,
671678
closedAt: pull.closedAt,
672679
mergedAt: pull.mergedAt,
680+
...settledMerge(pull),
673681
reviewers: Json.toReviewers(listed, input.host),
674682
checks: checks === null ? [] : Json.toChecks(checks),
675683
...(status.checks.total === 0
@@ -700,7 +708,7 @@ export const make = Effect.gen(function* () {
700708
rebase: mergeStrategyOpen(status, "rebase"),
701709
},
702710
viewerPermissions:
703-
status.merge.permitted === false
711+
status.merge.permitted === false || merging
704712
? { ...permissions, actions: permissions.actions.filter((a) => a !== "merge") }
705713
: permissions,
706714
};
@@ -720,7 +728,15 @@ export const make = Effect.gen(function* () {
720728
readPages(operation, input, `${path}/reviews`, Json.GitCafeReviews),
721729
pull.headOid === null
722730
? Effect.succeed({ items: [], next: null })
723-
: readPages(operation, input, `${path}/commits`, Json.GitCafeCommits),
731+
: // A closed pull request whose branch is gone keeps its head, but git.cafe answers
732+
// 404 for commits it no longer has; the conversation still stands without them.
733+
readPages(operation, input, `${path}/commits`, Json.GitCafeCommits).pipe(
734+
Effect.catch((error) =>
735+
error.reason === "not-found"
736+
? Effect.succeed({ items: [], next: null })
737+
: Effect.fail(error),
738+
),
739+
),
724740
readPages(operation, input, `${path}/reactions/`, Json.GitCafeReactions).pipe(
725741
Effect.orElseSucceed(() => undefined),
726742
),

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

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@ const Actor = Schema.Union([
3737
actorId: Schema.String,
3838
login: TrimmedNonEmptyString,
3939
avatarUrl: Schema.NullOr(Schema.String),
40-
linkedProfile: Schema.optional(Schema.NullOr(Schema.Struct({ handle: TrimmedNonEmptyString }))),
40+
linkedProfile: Schema.optional(
41+
Schema.NullOr(
42+
Schema.Struct({ handle: TrimmedNonEmptyString, actorId: Schema.optional(Schema.String) }),
43+
),
44+
),
4145
}),
4246
Schema.Struct({ kind: Schema.Literal("unavailable"), actorId: Schema.String }),
4347
]);
@@ -81,6 +85,10 @@ export const GitCafePullDetail = Schema.Struct({
8185
observedBaseOid: Schema.optional(Schema.NullOr(Schema.String)),
8286
mergeRoute: Schema.optional(Schema.Literals(["native", "provider", "unsupported"])),
8387
lockedAt: Schema.optional(Schema.NullOr(IsoDateTime)),
88+
viewerActorId: Schema.optional(Schema.NullOr(Schema.String)),
89+
// Set once GitHub has merged a pull request GitCafe routed there, which can be before the
90+
// sync brings the merged state back.
91+
providerMerge: Schema.optional(Schema.NullOr(Schema.Struct({ mergedAt: IsoDateTime }))),
8492
capabilities: Schema.Struct({
8593
comment: Schema.Boolean,
8694
review: Schema.Boolean,
@@ -334,6 +342,16 @@ export function toChangeRequest(
334342
};
335343
}
336344

345+
function isViewerAuthor(detail: GitCafePullDetail): boolean {
346+
const viewer = detail.viewerActorId;
347+
if (viewer == null) return false;
348+
const { author } = detail;
349+
return (
350+
author.actorId === viewer ||
351+
(author.kind === "github" && author.linkedProfile?.actorId === viewer)
352+
);
353+
}
354+
337355
export function toViewerPermissions(detail: GitCafePullDetail): PullRequestViewerPermissions {
338356
const { comment, edit, review, merge, moderate } = detail.capabilities;
339357
const open = comment && (detail.lockedAt == null || moderate);
@@ -349,7 +367,12 @@ export function toViewerPermissions(detail: GitCafePullDetail): PullRequestViewe
349367
stackRebase: merge,
350368
comment: open,
351369
resolve: open,
352-
verdicts: review ? ["approve", "request-changes", "comment"] : [],
370+
// GitCafe refuses an author's approval and request for changes, as GitHub does.
371+
verdicts: !review
372+
? []
373+
: isViewerAuthor(detail)
374+
? ["comment"]
375+
: ["approve", "request-changes", "comment"],
353376
requestReviewers: edit && detail.state === "open",
354377
labels: edit && detail.state === "open",
355378
editChangeRequest: edit,

0 commit comments

Comments
 (0)