diff --git a/entry b/entry index 6829cd3..b734474 100755 --- a/entry +++ b/entry @@ -39511,7 +39511,7 @@ async function reportProgress({ body }) { } const issueNumber = ctx.payload.event.issue_number; if (issueNumber === void 0) { - throw new Error("cannot create progress comment: no issue_number found in the payload event"); + return void 0; } const result = await ctx.octokit.rest.issues.createComment({ owner: ctx.owner, @@ -39679,6 +39679,12 @@ var init_comment = __esm({ parameters: ReportProgress, execute: contextualize(async ({ body }) => { const result = await reportProgress({ body }); + if (!result) { + return { + success: false, + message: "cannot create progress comment: no issue_number found in the payload event. this may occur for workflow_dispatch events or when there is no associated issue/PR. if you need to comment on a specific issue or PR, use create_issue_comment with an explicit issueNumber." + }; + } return { success: true, ...result @@ -124233,7 +124239,8 @@ function $(cmd, args3, options) { const result = spawnSync4(cmd, args3, { stdio: ["ignore", "pipe", "pipe"], encoding, - cwd: options?.cwd + cwd: options?.cwd, + env: options?.env ? { ...process.env, ...options.env } : void 0 }); const stdout = result.stdout ?? ""; const stderr = result.stderr ?? ""; @@ -124719,6 +124726,7 @@ var PullRequestTool = tool({ // mcp/prInfo.ts init_out4(); init_cli(); +init_github(); init_shared3(); var PullRequestInfo = type({ pull_number: type.number.describe("The pull request number to fetch") @@ -124743,7 +124751,10 @@ var PullRequestInfoTool = tool({ const headRepo = data.head.repo.full_name; const isFork = headRepo !== baseRepo; log.info(`Checking out PR #${pull_number} using gh pr checkout`); - $("gh", ["pr", "checkout", pull_number.toString()]); + const token = getGitHubInstallationToken(); + $("gh", ["pr", "checkout", pull_number.toString()], { + env: { GH_TOKEN: token } + }); log.info(`Fetching base branch: origin/${baseBranch}`); $("git", ["fetch", "origin", baseBranch, "--depth=20"]); const currentBranch = $("git", ["rev-parse", "--abbrev-ref", "HEAD"], { log: false }).trim(); @@ -125103,6 +125114,7 @@ init_github(); // utils/setup.ts import { execSync } from "node:child_process"; init_cli(); +init_github(); function setupGitConfig() { const repoDir = process.cwd(); log.info("\u{1F527} Setting up git configuration..."); @@ -125150,9 +125162,10 @@ function setupGitBranch(payload) { const prNumber = payload.event.issue_number; try { log.debug(`Checking out PR #${prNumber} using gh pr checkout`); - execSync(`gh pr checkout ${prNumber}`, { + const token = getGitHubInstallationToken(); + $("gh", ["pr", "checkout", prNumber.toString()], { cwd: repoDir, - stdio: "pipe" + env: { GH_TOKEN: token } }); log.info(`\u2713 Successfully checked out PR branch: ${branch}`); return; diff --git a/external.ts b/external.ts index 42d7806..dadf230 100644 --- a/external.ts +++ b/external.ts @@ -193,7 +193,6 @@ interface FixReviewEvent extends BasePayloadEvent { review_id: number; /** "all" to fix all comments, or specific comment IDs to fix */ comment_ids: number[] | "all"; - branch: string; } interface UnknownEvent extends BasePayloadEvent { diff --git a/mcp/comment.ts b/mcp/comment.ts index cec52ad..611fb89 100644 --- a/mcp/comment.ts +++ b/mcp/comment.ts @@ -177,7 +177,8 @@ export async function reportProgress({ body }: { body: string }): Promise< // no existing comment - create one const issueNumber = ctx.payload.event.issue_number; if (issueNumber === undefined) { - throw new Error("cannot create progress comment: no issue_number found in the payload event"); + // cannot create comment without issue_number (e.g., workflow_dispatch events) + return undefined; } const result = await ctx.octokit.rest.issues.createComment({ @@ -207,6 +208,16 @@ export const ReportProgressTool = tool({ execute: contextualize(async ({ body }) => { const result = await reportProgress({ body }); + if (!result) { + // gracefully handle case where no comment can be created + // this happens for workflow_dispatch events or when there's no associated issue/PR + return { + success: false, + message: + "cannot create progress comment: no issue_number found in the payload event. this may occur for workflow_dispatch events or when there is no associated issue/PR. if you need to comment on a specific issue or PR, use create_issue_comment with an explicit issueNumber.", + }; + } + return { success: true, ...result, diff --git a/mcp/prInfo.ts b/mcp/prInfo.ts index ad8039c..fedffc6 100644 --- a/mcp/prInfo.ts +++ b/mcp/prInfo.ts @@ -1,6 +1,4 @@ import { type } from "arktype"; -import { log } from "../utils/cli.ts"; -import { $ } from "../utils/shell.ts"; import { contextualize, tool } from "./shared.ts"; export const PullRequestInfo = type({ @@ -9,7 +7,8 @@ export const PullRequestInfo = type({ export const PullRequestInfoTool = tool({ name: "get_pull_request", - description: "Retrieve PR information. Automatically fetches and checks out the PR branch.", + description: + "Retrieve PR information (metadata only). PR branch is already checked out during setup.", parameters: PullRequestInfo, execute: contextualize(async ({ pull_number }, ctx) => { const pr = await ctx.octokit.rest.pulls.get({ @@ -20,38 +19,8 @@ export const PullRequestInfoTool = tool({ const data = pr.data; - const baseBranch = data.base.ref; - const headBranch = data.head.ref; - - if (!baseBranch) { - throw new Error(`Base branch not found for PR #${pull_number}`); - } - // detect fork PRs - head repo differs from base repo - const baseRepo = data.base.repo.full_name; - const headRepo = data.head.repo.full_name; - const isFork = headRepo !== baseRepo; - - // use gh pr checkout which handles fork PRs automatically - // it adds the fork as a remote if needed and checks out the PR branch - log.info(`Checking out PR #${pull_number} using gh pr checkout`); - $("gh", ["pr", "checkout", pull_number.toString()]); - - // fetch base branch for diff comparison - log.info(`Fetching base branch: origin/${baseBranch}`); - $("git", ["fetch", "origin", baseBranch, "--depth=20"]); - - // get current git status for summary - const currentBranch = $("git", ["rev-parse", "--abbrev-ref", "HEAD"], { log: false }).trim(); - const currentSha = $("git", ["rev-parse", "HEAD"], { log: false }).trim(); - const baseSha = $("git", ["rev-parse", `origin/${baseBranch}`], { log: false }).trim(); - - // build summary - const summary = `PR branch has been fetched and checked out: -- Base branch: \`origin/${baseBranch}\` (${baseSha.substring(0, 7)}) -- PR branch: \`${headBranch}\` (checked out locally, ${currentSha.substring(0, 7)}) -- Current branch: \`${currentBranch}\` -- View diff: \`git diff origin/${baseBranch}...HEAD\``; + const isFork = data.head.repo.full_name !== data.base.repo.full_name; return { number: data.number, @@ -60,10 +29,9 @@ export const PullRequestInfoTool = tool({ state: data.state, draft: data.draft, merged: data.merged, - base: baseBranch, - head: headBranch, + base: data.base.ref, + head: data.head.ref, isFork, - summary, }; }), }); diff --git a/utils/setup.ts b/utils/setup.ts index eecce85..c0b39d6 100644 --- a/utils/setup.ts +++ b/utils/setup.ts @@ -2,7 +2,7 @@ import { execSync } from "node:child_process"; import { existsSync, rmSync } from "node:fs"; import type { Payload } from "../external.ts"; import { log } from "./cli.ts"; -import type { RepoContext } from "./github.ts"; +import { getGitHubInstallationToken, type RepoContext } from "./github.ts"; import { $ } from "./shell.ts"; export interface SetupOptions { @@ -95,62 +95,28 @@ export function setupGitAuth(ctx: { } /** - * Setup git branch based on payload event context - * Automatically checks out the appropriate branch before agent execution + * Setup git branch based on payload event context. + * For PR events, uses `gh pr checkout` which handles fork PRs automatically. + * For non-PR events, stays on the default branch. */ export function setupGitBranch(payload: Payload): void { - const branch = payload.event.branch; - const repoDir = process.cwd(); - - if (!branch) { - log.debug("No branch specified in payload, using default branch"); + // only checkout for PR events - use issue_number directly (no dependency on branch field) + if (payload.event.is_pr !== true || !payload.event.issue_number) { + log.debug("Not a PR event, staying on default branch"); return; } - log.info(`🌿 Setting up git branch: ${branch}`); + const prNumber = payload.event.issue_number; + const repoDir = process.cwd(); - // if this is a PR-related event, use gh pr checkout which handles fork PRs automatically - if (payload.event.is_pr === true && payload.event.issue_number) { - const prNumber = payload.event.issue_number; - try { - // gh pr checkout handles fork PRs by setting up remotes automatically - log.debug(`Checking out PR #${prNumber} using gh pr checkout`); - execSync(`gh pr checkout ${prNumber}`, { - cwd: repoDir, - stdio: "pipe", - }); + log.info(`🌿 Checking out PR #${prNumber}...`); - log.info(`✓ Successfully checked out PR branch: ${branch}`); - return; - } catch (error) { - // if gh pr checkout fails, fall back to branch name fetch - log.debug( - `gh pr checkout failed, falling back to branch name fetch: ${error instanceof Error ? error.message : String(error)}` - ); - } - } + // gh pr checkout handles fork PRs by setting up remotes automatically + const token = getGitHubInstallationToken(); + $("gh", ["pr", "checkout", prNumber.toString()], { + cwd: repoDir, + env: { GH_TOKEN: token }, + }); - // fallback: fetch by branch name (for non-PR contexts or if PR ref fetch failed) - try { - log.debug(`Fetching branch from origin: ${branch}`); - execSync(`git fetch origin ${branch}`, { - cwd: repoDir, - stdio: "pipe", - }); - - // checkout the branch, creating local tracking branch - log.debug(`Checking out branch: ${branch}`); - execSync(`git checkout -B ${branch} origin/${branch}`, { - cwd: repoDir, - stdio: "pipe", - }); - - log.info(`✓ Successfully checked out branch: ${branch}`); - } catch (error) { - // if git operations fail, log warning but don't fail the action - // the agent might still be able to work with the default branch - log.warning( - `Failed to checkout branch ${branch}: ${error instanceof Error ? error.message : String(error)}` - ); - } + log.info(`✓ Successfully checked out PR #${prNumber}`); } diff --git a/utils/shell.ts b/utils/shell.ts index 2490215..ff1019e 100644 --- a/utils/shell.ts +++ b/utils/shell.ts @@ -14,6 +14,7 @@ interface ShellOptions { | "ucs2" | "utf16le"; log?: boolean; + env?: Record; onError?: (result: { status: number; stdout: string; stderr: string }) => void; } @@ -36,6 +37,7 @@ export function $(cmd: string, args: string[], options?: ShellOptions): string { stdio: ["ignore", "pipe", "pipe"], encoding, cwd: options?.cwd, + env: options?.env ? { ...process.env, ...options.env } : undefined, }); const stdout = result.stdout ?? "";