Skip to content

Commit

Permalink
Support updating PR branches
Browse files Browse the repository at this point in the history
  • Loading branch information
jspahrsummers committed Jan 10, 2025
1 parent f42cf77 commit ac7592f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/github/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,15 @@ MCP Server for the GitHub API, enabling file operations, repository management,
- `pull_number` (number): Pull request number
- Returns: Combined status check results and individual check details

24. `update_pull_request_branch`
- Update a pull request branch with the latest changes from the base branch (equivalent to GitHub's "Update branch" button)
- Inputs:
- `owner` (string): Repository owner
- `repo` (string): Repository name
- `pull_number` (number): Pull request number
- `expected_head_sha` (optional string): The expected SHA of the pull request's HEAD ref
- Returns: Success message when branch is updated

## Search Query Syntax

### Code Search
Expand Down
37 changes: 37 additions & 0 deletions src/github/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import {
SearchCodeResponseSchema,
SearchCodeSchema,
SearchIssuesResponseSchema,
UpdatePullRequestBranchSchema,
SearchIssuesSchema,
SearchRepositoriesSchema,
SearchUsersResponseSchema,
Expand Down Expand Up @@ -853,6 +854,31 @@ async function getPullRequestFiles(
return z.array(PullRequestFileSchema).parse(await response.json());
}

async function updatePullRequestBranch(
owner: string,
repo: string,
pullNumber: number,
expectedHeadSha?: string
): Promise<void> {
const response = await fetch(
`https://api.github.com/repos/${owner}/${repo}/pulls/${pullNumber}/update-branch`,
{
method: "PUT",
headers: {
Authorization: `token ${GITHUB_PERSONAL_ACCESS_TOKEN}`,
Accept: "application/vnd.github.v3+json",
"User-Agent": "github-mcp-server",
"Content-Type": "application/json",
},
body: expectedHeadSha ? JSON.stringify({ expected_head_sha: expectedHeadSha }) : undefined,
}
);

if (!response.ok) {
throw new Error(`GitHub API error: ${response.statusText}`);
}
}

async function getPullRequestStatus(
owner: string,
repo: string,
Expand Down Expand Up @@ -1017,6 +1043,11 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
name: "get_pull_request_status",
description: "Get the combined status of all status checks for a pull request",
inputSchema: zodToJsonSchema(GetPullRequestStatusSchema)
},
{
name: "update_pull_request_branch",
description: "Update a pull request branch with the latest changes from the base branch",
inputSchema: zodToJsonSchema(UpdatePullRequestBranchSchema)
}
],
};
Expand Down Expand Up @@ -1261,6 +1292,12 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
return { content: [{ type: "text", text: JSON.stringify(status, null, 2) }] };
}

case "update_pull_request_branch": {
const args = UpdatePullRequestBranchSchema.parse(request.params.arguments);
await updatePullRequestBranch(args.owner, args.repo, args.pull_number, args.expected_head_sha);
return { content: [{ type: "text", text: "Pull request branch updated successfully" }] };
}

default:
throw new Error(`Unknown tool: ${request.params.name}`);
}
Expand Down
9 changes: 9 additions & 0 deletions src/github/schemas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -812,4 +812,13 @@ export type GetPullRequestFiles = z.infer<typeof GetPullRequestFilesSchema>;
export type PullRequestFile = z.infer<typeof PullRequestFileSchema>;
export type GetPullRequestStatus = z.infer<typeof GetPullRequestStatusSchema>;
export type StatusCheck = z.infer<typeof StatusCheckSchema>;
// Schema for updating a pull request branch
export const UpdatePullRequestBranchSchema = z.object({
owner: z.string().describe("Repository owner (username or organization)"),
repo: z.string().describe("Repository name"),
pull_number: z.number().describe("Pull request number"),
expected_head_sha: z.string().optional().describe("The expected SHA of the pull request's HEAD ref")
});

export type CombinedStatus = z.infer<typeof CombinedStatusSchema>;
export type UpdatePullRequestBranch = z.infer<typeof UpdatePullRequestBranchSchema>;

0 comments on commit ac7592f

Please sign in to comment.