Improve repo setup with gh cli

This commit is contained in:
Colin McDonnell
2025-12-15 20:21:56 -08:00
parent dc93c89c24
commit 2f16d2ef0e
6 changed files with 268 additions and 209 deletions
+1 -1
View File
@@ -111,7 +111,7 @@ see individual files for documentation on other tools:
## usage in agents
agents should never use the `gh` cli. instead, they should use the mcp tools provided by this server.
agents should prefer using the mcp tools provided by this server. the `gh` cli is available as a fallback if needed, but mcp tools handle authentication and provide better integration.
the agent instructions automatically include guidance on using these tools.
+24 -10
View File
@@ -9,8 +9,7 @@ export const PullRequestInfo = type({
export const PullRequestInfoTool = tool({
name: "get_pull_request",
description:
"Retrieve PR information and automatically prepare the repository for review by fetching and checking out the PR branch.",
description: "Retrieve PR information. Automatically fetches and checks out the PR branch.",
parameters: PullRequestInfo,
execute: contextualize(async ({ pull_number }, ctx) => {
const pr = await ctx.octokit.rest.pulls.get({
@@ -28,18 +27,31 @@ export const PullRequestInfoTool = tool({
throw new Error(`Base branch not found for PR #${pull_number}`);
}
// Automatically fetch and checkout branches for review
// 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"]);
// use GitHub's PR ref which works for both fork and non-fork PRs
// refs/pull/{number}/head always points to the PR head commit
log.info(`Fetching PR #${pull_number} using refs/pull/${pull_number}/head`);
$("git", ["fetch", "origin", `refs/pull/${pull_number}/head`]);
// 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();
log.info(`Checking out PR branch: ${headBranch}`);
// check out a local branch from FETCH_HEAD (the PR ref we just fetched)
$("git", ["checkout", "-B", headBranch, "FETCH_HEAD"]);
// 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\``;
return {
number: data.number,
@@ -50,6 +62,8 @@ export const PullRequestInfoTool = tool({
merged: data.merged,
base: baseBranch,
head: headBranch,
isFork,
summary,
};
}),
});