TypeScript
fix(gitcafe): authored lists, merge progress, and author reviews match git.cafe (#23887)
Julius Marmingef9c1d023f1da
diff --git a/packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.test.ts b/packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.test.tsindex b9417b319e3..2db12a28406 100644--- a/packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.test.ts+++ b/packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.test.ts@@ -271,6 +271,23 @@ describe("GitCafePullRequestProvider", () => {}).pipe(Effect.provide(server.layer));});+ it.effect("keeps the timeline when GitCafe no longer has the head's commits", () => {+ const comment = { ...root, id: "c1", threadId: "c1", body: "Looks good", path: null };+ const server = fakeGitCafe({+ "GET /repos/owner/repo/pulls/7": { ...pull, state: "closed", closedAt: timestamp },+ "GET /repos/owner/repo/pulls/7/comments": { items: [comment], next: null },+ "GET /repos/owner/repo/pulls/7/reviews": { items: [], next: null },+ });+ return Effect.gen(function* () {+ const provider = yield* GitCafePullRequestProvider.make;+ const activity = yield* provider.getChangeRequestActivity(target);+ assert.deepStrictEqual(+ activity.comments.map((entry) => entry.body),+ ["Looks good"],+ );+ }).pipe(Effect.provide(server.layer));+ });+it.effect("submits a review against the pull request's current head", () => {const server = fakeGitCafe({"GET /repos/owner/repo/pulls/7": pull,@@ -381,9 +398,7 @@ describe("GitCafePullRequestProvider", () => {["reviewing", "reviewers"],] as const)("filters %s pull requests by the viewer's actor on GitCafe", ([involvement, key]) => {const server = fakeGitCafe({- "GET /repos/owner/repo/filter-options": {- actors: [{ actorId: "act_one", handle: "Alice" }],- },+ "GET /auth/principal": { actorId: "act_one", handle: "Alice" },"GET /repos/owner/repo/pulls": { items: [pull], next: null },});return Effect.gen(function* () {@@ -403,9 +418,9 @@ describe("GitCafePullRequestProvider", () => {}).pipe(Effect.provide(server.layer));});- it.effect("lists nothing for a viewer GitCafe has no actor for", () => {+ it.effect("lists nothing for a viewer the token does not belong to", () => {const server = fakeGitCafe({- "GET /repos/owner/repo/filter-options": { actors: [] },+ "GET /auth/principal": { actorId: "act_two", handle: "bob" },});return Effect.gen(function* () {const provider = yield* GitCafePullRequestProvider.make;@@ -419,7 +434,7 @@ describe("GitCafePullRequestProvider", () => {assert.deepStrictEqual(page.items, []);assert.deepStrictEqual(server.sent.map((request) => request.path),- ["/repos/owner/repo/filter-options"],+ ["/auth/principal"],);}).pipe(Effect.provide(server.layer));});@@ -557,6 +572,59 @@ describe("GitCafePullRequestProvider", () => {}).pipe(Effect.provide(server.layer));});+ const detailWith = (overrides: Record<string, unknown>, reasons: ReadonlyArray<string> = []) =>+ fakeGitCafe({+ "GET /repos/owner/repo/pulls/7": { ...pull, headOid: null, ...overrides },+ "GET /repos/owner/repo/pulls/7/reviewers": { items: [], next: null },+ "GET /repos/owner/repo/pulls/7/labels": { items: [], next: null },+ "GET /repos/owner/repo/pulls/7/status": {+ merge: { conflicts: "none", fastForward: null, strategies: ["squash"], reasons },+ checks: { pending: 0, failing: 0, total: 0 },+ },+ });++ it.effect("shows a merge GitCafe accepted as armed until it lands", () => {+ const server = detailWith({}, ["provider-merge-pending"]);+ return Effect.gen(function* () {+ const provider = yield* GitCafePullRequestProvider.make;+ const detail = yield* provider.getChangeRequest(target);+ assert.strictEqual(detail.autoMergeEnabled, true);+ assert.notInclude(detail.viewerPermissions?.actions ?? [], "merge");+ }).pipe(Effect.provide(server.layer));+ });++ it.effect("reads a pull request GitHub merged as merged before the sync reports it", () => {+ const mergedAt = "2026-09-12T13:00:00Z";+ const server = detailWith({ providerMerge: { mergedAt } });+ return Effect.gen(function* () {+ const provider = yield* GitCafePullRequestProvider.make;+ const detail = yield* provider.getChangeRequest(target);+ assert.strictEqual(detail.state, "merged");+ assert.strictEqual(detail.mergedAt, mergedAt);+ }).pipe(Effect.provide(server.layer));+ });++ it.effect.each([+ ["their own pull request", actor],+ [+ "the pull request GitHub sync opened for them",+ {+ kind: "github",+ actorId: "act_github",+ login: "alice",+ avatarUrl: null,+ linkedProfile: { handle: "alice", actorId: "act_one" },+ },+ ],+ ] as const)("leaves the author only a comment on %s", ([, author]) => {+ const server = detailWith({ author, viewerActorId: "act_one" });+ return Effect.gen(function* () {+ const provider = yield* GitCafePullRequestProvider.make;+ const detail = yield* provider.getChangeRequest(target);+ assert.deepStrictEqual(detail.viewerPermissions?.verdicts, ["comment"]);+ }).pipe(Effect.provide(server.layer));+ });+it.effect("requests and withdraws reviewers by rewriting the whole list", () => {const bob = { ...actor, actorId: "act_two", handle: "bob" };let requested = [{ id: "act_two", actor: bob }];diff --git a/packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.ts b/packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.tsindex a9394f2a863..60b4e9a183b 100644--- a/packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.ts+++ b/packages/source-control-gitcafe/src/server/GitCafePullRequestProvider.ts@@ -73,7 +73,7 @@ const mergeStrategyOpen = (status: typeof Status.Type, strategy: string) =>const Id = TrimmedNonEmptyString.check(Schema.isPattern(/^[A-Za-z0-9_-]+$/u));const isId = Schema.is(Id);const isApiError = Schema.is(GitCafeApi.GitCafeApiError);-const Principal = Schema.Struct({ handle: TrimmedNonEmptyString });+const Principal = Schema.Struct({ actorId: TrimmedNonEmptyString, handle: TrimmedNonEmptyString });const Written = Schema.Struct({ id: Id });const Versioned = Schema.Struct({ version: NonNegativeInt });const CommentDetail = Schema.Struct({ id: Id, version: NonNegativeInt });@@ -85,6 +85,7 @@ const Status = Schema.Struct({fastForward: Schema.NullOr(Schema.Boolean),strategies: Schema.Array(Schema.String),permitted: Schema.optional(Schema.Boolean),+ reasons: Schema.optional(Schema.Array(Schema.String)),blockers: Schema.optional(Schema.Array(Schema.Struct({ blockedStrategies: Schema.optional(Schema.Array(Schema.String)) }),@@ -97,15 +98,12 @@ const Status = Schema.Struct({total: NonNegativeInt,}),});+/** A merge GitCafe accepted runs on afterwards: on GitHub for a synced pull request, or natively. */+const MERGE_IN_FLIGHT = new Set(["provider-merge-pending", "native-operation-pending"]);const Listed = Schema.Struct({items: Schema.Array(Schema.Struct({ id: TrimmedNonEmptyString })),next: Schema.NullOr(Schema.String),});-const FilterOptions = Schema.Struct({- actors: Schema.Array(- Schema.Struct({ actorId: TrimmedNonEmptyString, handle: TrimmedNonEmptyString }),- ),-});const encodeActorIds = Schema.encodeSync(Schema.fromJsonString(Schema.Array(Schema.String)));const BranchCommit = Schema.Struct({ oid: TrimmedNonEmptyString });const Snapshot = {@@ -136,6 +134,12 @@ const StackOutcome = Schema.Struct({ id: Schema.String, state: Schema.String });const FAILED_STACK_STATES = new Set(["failed", "stopped", "cancelled", "reconciliation_required"]);type PullRef = ProviderRepositoryRef & { readonly number: number };++/** GitHub's merge counts as soon as GitCafe records it, ahead of the sync that reports it. */+const settledMerge = (pull: Json.GitCafePullDetail) =>+ pull.state === "open" && pull.providerMerge != null+ ? { state: "merged" as const, mergedAt: pull.providerMerge.mergedAt }+ : {};type Paged<A> = { readonly items: ReadonlyArray<A>; readonly next: string | null };function stackHeadsCurrent(@@ -542,14 +546,15 @@ export const make = Effect.gen(function* () {const involved = input.involvement === "authored" || input.involvement === "reviewing";let actorId: string | undefined;if (involved) {- // A host narrows as far as it can: when the actor list can't be read, the listing comes- // back unnarrowed and the page's own involvement filter does the rest. A refused token- // or a rate limit still fails, since the service acts on those for the whole host.- const options = yield* read(+ // The viewer is the token's principal, whose actor also matches the pull requests GitHub+ // sync authored for its linked profile. When it can't be read, the listing comes back+ // unnarrowed and the page's own involvement filter does the rest. A refused token or a+ // rate limit still fails, since the service acts on those for the whole host.+ const principal = yield* read("listChangeRequests",input,- { path: `${base}/filter-options` },- FilterOptions,+ { path: "/auth/principal" },+ Principal,).pipe(Effect.map(Option.some),Effect.catchTags({@@ -559,11 +564,10 @@ export const make = Effect.gen(function* () {: Effect.succeed(Option.none()),}),);- if (Option.isSome(options)) {- actorId = options.value.actors.find(- (actor) => actor.handle.toLowerCase() === input.viewer.toLowerCase(),- )?.actorId;- if (actorId === undefined) return { items: [], truncated: false, continues: false };+ if (Option.isSome(principal)) {+ if (principal.value.handle.toLowerCase() !== input.viewer.toLowerCase())+ return { items: [], truncated: false, continues: false };+ actorId = principal.value.actorId;}}const limit = Math.max(1, input.limit);@@ -624,6 +628,7 @@ export const make = Effect.gen(function* () {...(changes === null ? {} : changes),closedAt: pull.closedAt,mergedAt: pull.mergedAt,+ ...settledMerge(pull),};},),@@ -658,8 +663,10 @@ export const make = Effect.gen(function* () {);const listed = { ...pull, reviewers: reviewers.items, labels: labels.items };const permissions = Json.toViewerPermissions(pull);+ const merging = status.merge.reasons?.some((reason) => MERGE_IN_FLIGHT.has(reason)) ?? false;return {...Json.toChangeRequest(listed, input),+ autoMergeEnabled: merging,headSha: pull.headOid,body: pull.description ?? "",...(pull.sourceRepo === null@@ -670,6 +677,7 @@ export const make = Effect.gen(function* () {changedFiles: changes?.changedFiles ?? 0,closedAt: pull.closedAt,mergedAt: pull.mergedAt,+ ...settledMerge(pull),reviewers: Json.toReviewers(listed, input.host),checks: checks === null ? [] : Json.toChecks(checks),...(status.checks.total === 0@@ -700,7 +708,7 @@ export const make = Effect.gen(function* () {rebase: mergeStrategyOpen(status, "rebase"),},viewerPermissions:- status.merge.permitted === false+ status.merge.permitted === false || merging? { ...permissions, actions: permissions.actions.filter((a) => a !== "merge") }: permissions,};@@ -720,7 +728,15 @@ export const make = Effect.gen(function* () {readPages(operation, input, `${path}/reviews`, Json.GitCafeReviews),pull.headOid === null? Effect.succeed({ items: [], next: null })- : readPages(operation, input, `${path}/commits`, Json.GitCafeCommits),+ : // A closed pull request whose branch is gone keeps its head, but git.cafe answers+ // 404 for commits it no longer has; the conversation still stands without them.+ readPages(operation, input, `${path}/commits`, Json.GitCafeCommits).pipe(+ Effect.catch((error) =>+ error.reason === "not-found"+ ? Effect.succeed({ items: [], next: null })+ : Effect.fail(error),+ ),+ ),readPages(operation, input, `${path}/reactions/`, Json.GitCafeReactions).pipe(Effect.orElseSucceed(() => undefined),),diff --git a/packages/source-control-gitcafe/src/server/gitCafePullRequestJson.ts b/packages/source-control-gitcafe/src/server/gitCafePullRequestJson.tsindex 2b848639a04..8c3b0ac56e2 100644--- a/packages/source-control-gitcafe/src/server/gitCafePullRequestJson.ts+++ b/packages/source-control-gitcafe/src/server/gitCafePullRequestJson.ts@@ -37,7 +37,11 @@ const Actor = Schema.Union([actorId: Schema.String,login: TrimmedNonEmptyString,avatarUrl: Schema.NullOr(Schema.String),- linkedProfile: Schema.optional(Schema.NullOr(Schema.Struct({ handle: TrimmedNonEmptyString }))),+ linkedProfile: Schema.optional(+ Schema.NullOr(+ Schema.Struct({ handle: TrimmedNonEmptyString, actorId: Schema.optional(Schema.String) }),+ ),+ ),}),Schema.Struct({ kind: Schema.Literal("unavailable"), actorId: Schema.String }),]);@@ -81,6 +85,10 @@ export const GitCafePullDetail = Schema.Struct({observedBaseOid: Schema.optional(Schema.NullOr(Schema.String)),mergeRoute: Schema.optional(Schema.Literals(["native", "provider", "unsupported"])),lockedAt: Schema.optional(Schema.NullOr(IsoDateTime)),+ viewerActorId: Schema.optional(Schema.NullOr(Schema.String)),+ // Set once GitHub has merged a pull request GitCafe routed there, which can be before the+ // sync brings the merged state back.+ providerMerge: Schema.optional(Schema.NullOr(Schema.Struct({ mergedAt: IsoDateTime }))),capabilities: Schema.Struct({comment: Schema.Boolean,review: Schema.Boolean,@@ -334,6 +342,16 @@ export function toChangeRequest(};}+function isViewerAuthor(detail: GitCafePullDetail): boolean {+ const viewer = detail.viewerActorId;+ if (viewer == null) return false;+ const { author } = detail;+ return (+ author.actorId === viewer ||+ (author.kind === "github" && author.linkedProfile?.actorId === viewer)+ );+}+export function toViewerPermissions(detail: GitCafePullDetail): PullRequestViewerPermissions {const { comment, edit, review, merge, moderate } = detail.capabilities;const open = comment && (detail.lockedAt == null || moderate);@@ -349,7 +367,12 @@ export function toViewerPermissions(detail: GitCafePullDetail): PullRequestViewestackRebase: merge,comment: open,resolve: open,- verdicts: review ? ["approve", "request-changes", "comment"] : [],+ // GitCafe refuses an author's approval and request for changes, as GitHub does.+ verdicts: !review+ ? []+ : isViewerAuthor(detail)+ ? ["comment"]+ : ["approve", "request-changes", "comment"],requestReviewers: edit && detail.state === "open",labels: edit && detail.state === "open",editChangeRequest: edit,