feat(prs): navigate, merge and rebase GitHub stacks (#10875)

This commit is contained in:
Julius Marminge authored and GitHub committed 2026-09-09 15:50:59 -07:00
1 parent f0401c6290
commit de37964db2
53 files changed
+3106 -466

No files matched your search

@@ -3,6 +3,7 @@ import * as Exit from "effect/Exit";
import * as Result from "effect/Result";
import * as Schema from "effect/Schema";
import type {
PullRequestStackMembership,
PullRequestActor,
PullRequestCheck,
PullRequestCheckStatus,
@@ -111,12 +112,22 @@ const RawListItemSchema = Schema.Struct({
statusCheckRollup: Schema.optional(Schema.NullOr(Schema.Array(RawCheckSchema))),
});
const RawStackMembershipSchema = Schema.Struct({
stack: Schema.optional(
Schema.NullOr(
Schema.Struct({ number: Schema.Int, size: Schema.Int, baseRefName: Schema.String }),
),
),
stackEntry: Schema.optional(Schema.NullOr(Schema.Struct({ position: Schema.Int }))),
});
/**
* A search's own answer, which is the listing's row one connection deeper: `gh pr list --json`
* flattens reviewers and labels, and GraphQL does not. Everything below the row is optional
* because a node that is not a pull request decodes as an empty object, which is skipped.
*/
const RawSearchItemSchema = Schema.Struct({
...RawStackMembershipSchema.fields,
number: Schema.Int,
title: Schema.String,
url: Schema.String,
@@ -221,6 +232,13 @@ const RawStatsSchema = Schema.Struct({
),
});
const RawStackMembershipsSchema = Schema.Struct({
data: Schema.Record(
Schema.String,
Schema.NullOr(Schema.Struct({ pullRequest: Schema.NullOr(RawStackMembershipSchema) })),
),
});
/** How many of a reaction's people the hover names before it counts the rest. */
const REACTORS_PER_GROUP = 10;
@@ -648,12 +666,13 @@ export const PULL_REQUEST_SEARCH_MAX_ROWS = GRAPHQL_PAGE_SIZE;
* than twenty labels shows twenty, and one that has asked more than twenty people for a review
* is already past what a row can say.
*/
export function pullRequestSearchGraphQlQuery(rows: number): string {
export function pullRequestSearchGraphQlQuery(rows: number, includeStacks = false): string {
return `query($q: String!) {
search(query: $q, type: ISSUE, first: ${Math.min(Math.max(Math.trunc(rows), 1), PULL_REQUEST_SEARCH_MAX_ROWS)}) {
pageInfo { hasNextPage }
nodes {
... on PullRequest {
${includeStacks ? "stack { number size baseRefName } stackEntry { position }" : ""}
number
title
url
@@ -1019,6 +1038,7 @@ export const REPOSITORY_ACCESS_JSON_FIELDS =
"mergeCommitAllowed,squashMergeAllowed,rebaseMergeAllowed,viewerPermission";
export interface GitHubPullRequestListItem {
readonly stack?: PullRequestStackMembership;
/** The author's node id, kept so a batch can resolve the avatar the listing does not carry. */
readonly authorId: string | null;
readonly number: number;
@@ -1526,6 +1546,7 @@ export function decodePullRequestSearchJson(
const node = decodedNode.value;
const repository = trimmed(node.repository?.nameWithOwner);
if (repository === null) continue;
const stack = toStackMembership(node);
items.push({
...toListItem({
...node,
@@ -1541,6 +1562,7 @@ export function decodePullRequestSearchJson(
return state === null ? [] : [{ state }];
}),
}),
...(stack === undefined ? {} : { stack }),
repository,
});
}
@@ -1551,6 +1573,19 @@ export function decodePullRequestSearchJson(
});
}
function toStackMembership(
raw: Schema.Schema.Type<typeof RawStackMembershipSchema>,
): PullRequestStackMembership | undefined {
return raw.stack && raw.stackEntry
? {
number: raw.stack.number,
size: raw.stack.size,
base: raw.stack.baseRefName,
position: raw.stackEntry.position,
}
: undefined;
}
/** What a repository selector may hold before it is written into a GraphQL document unquoted. */
const REPOSITORY_PART = /^[A-Za-z0-9._-]+$/;
@@ -1581,6 +1616,42 @@ export function buildPullRequestStatsGraphQlQuery(
return `query {\n${selections.join("\n")}\n}`;
}
/** Stack membership for the visible rows of a per-repository listing. */
export function buildPullRequestStackMembershipsGraphQlQuery(
repository: string,
numbers: ReadonlyArray<number>,
): string | null {
if (numbers.length === 0) return null;
const [owner, name, ...rest] = repository.trim().split("/");
if (rest.length > 0 || owner === undefined || name === undefined) return null;
if (!REPOSITORY_PART.test(owner) || !REPOSITORY_PART.test(name)) return null;
const selections: string[] = [];
for (const [index, number] of numbers.entries()) {
if (!Number.isSafeInteger(number) || number <= 0) return null;
selections.push(
` s${index}: repository(owner: "${owner}", name: "${name}") { pullRequest(number: ${number}) { stack { number size baseRefName } stackEntry { position } } }`,
);
}
return `query PullRequestStackMemberships {\n${selections.join("\n")}\n}`;
}
const decodeStackMemberships = decodeJsonResult(RawStackMembershipsSchema);
export function decodePullRequestStackMembershipsJson(
raw: string,
): Result.Result<ReadonlyMap<number, PullRequestStackMembership>, DecodeFailure> {
const decoded = decodeStackMemberships(raw);
if (!Result.isSuccess(decoded)) return Result.fail(decoded.failure);
const memberships = new Map<number, PullRequestStackMembership>();
for (const [alias, value] of Object.entries(decoded.success.data)) {
const index = /^s(\d+)$/.exec(alias)?.[1];
if (index === undefined || value?.pullRequest == null) continue;
const stack = toStackMembership(value.pullRequest);
if (stack !== undefined) memberships.set(Number(index), stack);
}
return Result.succeed(memberships);
}
/**
* The counts by the position they were asked in. A repository or a pull request GitHub answered
* nothing for is simply absent, which leaves the row with whatever it already had.
@@ -2448,8 +2519,10 @@ export function decodePullRequestFilesJson(
/** One pull request as the stacks API lists it: a number, a head, and whether it is done. */
const RawStackPullRequestSchema = Schema.Struct({
title: Schema.optional(Schema.String),
draft: Schema.optional(Schema.Boolean),
number: Schema.Int,
head: Schema.Struct({ ref: Schema.String }),
head: Schema.Struct({ ref: Schema.String, sha: Schema.optional(Schema.String) }),
state: Schema.optional(Schema.NullOr(Schema.String)),
merged_at: Schema.optional(Schema.NullOr(Schema.String)),
});
@@ -2473,6 +2546,9 @@ const RawStackSchema = Schema.Struct({
const decodeStacks = decodeJsonResult(Schema.Array(RawStackSchema));
export interface GitHubPullRequestStackLayer {
readonly title?: string;
readonly isDraft?: boolean;
readonly headSha?: string;
readonly number: number;
readonly headBranch: string;
readonly state: PullRequestState;
@@ -2505,6 +2581,9 @@ export function decodePullRequestStacksJson(
url: trimmed(stack.html_url) ?? stack.url,
base: typeof stack.base === "string" ? stack.base : stack.base.ref,
layers: stack.pull_requests.map((pullRequest) => ({
...(pullRequest.title === undefined ? {} : { title: pullRequest.title }),
...(pullRequest.draft === undefined ? {} : { isDraft: pullRequest.draft }),
...(pullRequest.head.sha === undefined ? {} : { headSha: pullRequest.head.sha }),
number: pullRequest.number,
headBranch: pullRequest.head.ref,
state: toState({ state: pullRequest.state, mergedAt: pullRequest.merged_at }),