Support updating PR branches

This commit is contained in:
Justin Spahr-Summers
2025-01-10 11:53:23 +00:00
parent f42cf77d57
commit ac7592f71a
3 changed files with 55 additions and 0 deletions

View File

@@ -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

View File

@@ -48,6 +48,7 @@ import {
SearchCodeResponseSchema,
SearchCodeSchema,
SearchIssuesResponseSchema,
UpdatePullRequestBranchSchema,
SearchIssuesSchema,
SearchRepositoriesSchema,
SearchUsersResponseSchema,
@@ -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,
@@ -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)
}
],
};
@@ -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}`);
}

View File

@@ -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>;