Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/docs/src/content/docs/extend/github-plugin.md
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ export const plugins = defineJuniorPlugins([

This watch always includes `pull_request.review.approved` and `pull_request.merged`, even when `events` omits them. When the pull request is approved or merged, Junior mentions the people involved in the thread. These are the pull request author and assignees, the commit authors, and the people in `Co-authored-by` commit trailers. Junior only mentions people whose GitHub account or email links to a Slack user. When the reviewer or commenter links to a Slack user, Junior knows that the GitHub login and the Slack user are the same person, so it does not name them twice.

When the base of a new pull request is the head branch of another open pull request in the same repository, `github_createPullRequest` links it into a GitHub stack. It adds the pull request to the stack of the pull request below, or creates a stack from the chain of base branches down to the trunk. A stack merges as one group, and GitHub rebases it after each merge. When the repository has no stacked pull requests, or the pull request below is not on top of its stack, the pull request stays unstacked.
When the base of a new pull request is the head branch of another open pull request in the same repository, `github_createPullRequest` links it into a GitHub stack. It adds the pull request to the stack of the pull request below, or creates a stack from the chain of base branches down to the trunk. A stack merges as one group, and GitHub rebases it after each merge. The pull request stays unstacked when the repository has no stacked pull requests, the pull request below is not on top of its stack, the head is the default branch, or the chain of base branches loops.

`guidance` adds short app guidance when a matching pull request event reaches the agent. It applies within the watch or event automation instruction. It cannot replace or expand that instruction, grant credentials, or bypass action review. Keep each value short.

Expand Down
16 changes: 13 additions & 3 deletions packages/junior-github/src/tools/stack-pull-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ const GITHUB_STACKS_API_VERSION = "2026-03-10";
export const stackTargetSchema = z.object({
number: z.number().int().positive(),
head: z.object({
ref: z.string().min(1),
repo: z.object({ full_name: z.string().min(1) }).nullable(),
}),
base: z.object({
Expand Down Expand Up @@ -65,9 +66,12 @@ export async function stackGitHubPullRequest(
target: GitHubStackTarget,
): Promise<GitHubPullRequestStack | undefined> {
const repo = target.base.repo.full_name;
// Stacks need every branch in one repository.
const trunk = target.base.repo.default_branch;
// Stacks need every branch in one repository. A pull request from the trunk,
// such as a sync of the trunk into a long-lived branch, is not stacked work.
if (
target.base.ref === target.base.repo.default_branch ||
target.base.ref === trunk ||
target.head.ref === trunk ||
target.head.repo?.full_name !== repo
) {
return undefined;
Expand Down Expand Up @@ -108,7 +112,11 @@ export async function stackGitHubPullRequest(
// Bottom to top. Each pull request is based on the head of the one below.
const chain = [target.number];
let branch = target.base.ref;
for (let depth = 0; depth < MAX_STACK_DEPTH; depth += 1) {
for (
let depth = 0;
depth < MAX_STACK_DEPTH && branch !== trunk;
depth += 1
) {
const parents = await github(
"github.pull.list",
`${api}/pulls?state=open&per_page=2&head=${encodeURIComponent(`${owner}:${branch}`)}`,
Expand All @@ -118,6 +126,8 @@ export async function stackGitHubPullRequest(
// No parent: `branch` is the trunk. Two parents: the chain is ambiguous.
if (parents.length !== 1) break;
const parent = parents[0]!;
// Base branches that loop back are not a stack.
if (chain.includes(parent.number)) return undefined;
const stacks = await github(
"github.stack.read",
`${api}/stacks?pull_request=${parent.number}`,
Expand Down
72 changes: 69 additions & 3 deletions packages/junior-github/tests/github-plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1815,10 +1815,15 @@ Conversation: \`local:test:old-conversation\`
);
}
if (operation === "github.pull.list") {
// Only feat/bottom has an open pull request, so main is the trunk.
// An open sync pull request from main must not join the stack.
const head = new URL(request.url).searchParams.get("head");
if (head === "getsentry:main") {
return Response.json([
{ number: 600, base: { ref: "feat/long-lived" } },
]);
}
return Response.json(
new URL(request.url).searchParams.get("head") ===
"getsentry:feat/bottom"
head === "getsentry:feat/bottom"
? [{ number: 700, base: { ref: "main" } }]
: [],
);
Expand Down Expand Up @@ -1856,6 +1861,67 @@ Conversation: \`local:test:old-conversation\`
},
);

it.each([
{ name: "a sync from the trunk", head: "main", bottomBase: "main" },
{ name: "base branches that loop", head: "feat/a", bottomBase: "feat/a" },
])(
"does not stack a pull request on a loop of base branches: $name",
async ({ head, bottomBase }) => {
const repo = { default_branch: "main", full_name: "getsentry/junior" };
const ctx = githubToolsContext({
egressFetch: async ({ operation, request }) => {
if (operation === "github.pull.create") {
return Response.json(
{
number: 701,
html_url: "https://github.com/getsentry/junior/pull/701",
head: { ref: head, repo },
base: { ref: "feat/long-lived", repo },
},
{ status: 201 },
);
}
if (operation === "github.pull.list") {
// Pull request 700 merges feat/long-lived into the head of 701.
const branch = new URL(request.url).searchParams.get("head");
if (branch === "getsentry:feat/long-lived") {
return Response.json([
{ number: 700, base: { ref: bottomBase } },
]);
}
return Response.json(
branch === `getsentry:${head}`
? [{ number: 701, base: { ref: "feat/long-lived" } }]
: [],
);
}
if (operation === "github.stack.read") return Response.json([]);
return Response.json({ number: 9, pull_requests: [] });
},
});
const tool = githubPlugin().hooks?.tools?.(ctx as any)?.createPullRequest;

const result = await tool?.execute?.(
{
repo: "getsentry/junior",
title: "Update long-lived branch",
head,
base: "feat/long-lived",
},
{ toolCallId: "call-create-looped-pull-request" },
);

expect(result).toMatchObject({ number: 701 });
expect(result).not.toHaveProperty("stack");
expect(
ctx
.egressRequests()
.filter(({ request }) => request.method === "POST")
.map(({ operation }) => operation),
).not.toContainEqual(expect.stringMatching(/^github\.stack\./));
},
);

it("omits requester attribution when identity and actor names are unresolved", async () => {
const ctx = githubToolsContext({
actor: {
Expand Down
Loading