Skip to content

Commit ac7592f

Browse files
committed
Support updating PR branches
1 parent f42cf77 commit ac7592f

File tree

3 files changed

+55
-0
lines changed

3 files changed

+55
-0
lines changed

src/github/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,15 @@ MCP Server for the GitHub API, enabling file operations, repository management,
252252
- `pull_number` (number): Pull request number
253253
- Returns: Combined status check results and individual check details
254254

255+
24. `update_pull_request_branch`
256+
- Update a pull request branch with the latest changes from the base branch (equivalent to GitHub's "Update branch" button)
257+
- Inputs:
258+
- `owner` (string): Repository owner
259+
- `repo` (string): Repository name
260+
- `pull_number` (number): Pull request number
261+
- `expected_head_sha` (optional string): The expected SHA of the pull request's HEAD ref
262+
- Returns: Success message when branch is updated
263+
255264
## Search Query Syntax
256265

257266
### Code Search

src/github/index.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ import {
4848
SearchCodeResponseSchema,
4949
SearchCodeSchema,
5050
SearchIssuesResponseSchema,
51+
UpdatePullRequestBranchSchema,
5152
SearchIssuesSchema,
5253
SearchRepositoriesSchema,
5354
SearchUsersResponseSchema,
@@ -853,6 +854,31 @@ async function getPullRequestFiles(
853854
return z.array(PullRequestFileSchema).parse(await response.json());
854855
}
855856

857+
async function updatePullRequestBranch(
858+
owner: string,
859+
repo: string,
860+
pullNumber: number,
861+
expectedHeadSha?: string
862+
): Promise<void> {
863+
const response = await fetch(
864+
`https://api.github.com/repos/${owner}/${repo}/pulls/${pullNumber}/update-branch`,
865+
{
866+
method: "PUT",
867+
headers: {
868+
Authorization: `token ${GITHUB_PERSONAL_ACCESS_TOKEN}`,
869+
Accept: "application/vnd.github.v3+json",
870+
"User-Agent": "github-mcp-server",
871+
"Content-Type": "application/json",
872+
},
873+
body: expectedHeadSha ? JSON.stringify({ expected_head_sha: expectedHeadSha }) : undefined,
874+
}
875+
);
876+
877+
if (!response.ok) {
878+
throw new Error(`GitHub API error: ${response.statusText}`);
879+
}
880+
}
881+
856882
async function getPullRequestStatus(
857883
owner: string,
858884
repo: string,
@@ -1017,6 +1043,11 @@ server.setRequestHandler(ListToolsRequestSchema, async () => {
10171043
name: "get_pull_request_status",
10181044
description: "Get the combined status of all status checks for a pull request",
10191045
inputSchema: zodToJsonSchema(GetPullRequestStatusSchema)
1046+
},
1047+
{
1048+
name: "update_pull_request_branch",
1049+
description: "Update a pull request branch with the latest changes from the base branch",
1050+
inputSchema: zodToJsonSchema(UpdatePullRequestBranchSchema)
10201051
}
10211052
],
10221053
};
@@ -1261,6 +1292,12 @@ server.setRequestHandler(CallToolRequestSchema, async (request) => {
12611292
return { content: [{ type: "text", text: JSON.stringify(status, null, 2) }] };
12621293
}
12631294

1295+
case "update_pull_request_branch": {
1296+
const args = UpdatePullRequestBranchSchema.parse(request.params.arguments);
1297+
await updatePullRequestBranch(args.owner, args.repo, args.pull_number, args.expected_head_sha);
1298+
return { content: [{ type: "text", text: "Pull request branch updated successfully" }] };
1299+
}
1300+
12641301
default:
12651302
throw new Error(`Unknown tool: ${request.params.name}`);
12661303
}

src/github/schemas.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,4 +812,13 @@ export type GetPullRequestFiles = z.infer<typeof GetPullRequestFilesSchema>;
812812
export type PullRequestFile = z.infer<typeof PullRequestFileSchema>;
813813
export type GetPullRequestStatus = z.infer<typeof GetPullRequestStatusSchema>;
814814
export type StatusCheck = z.infer<typeof StatusCheckSchema>;
815+
// Schema for updating a pull request branch
816+
export const UpdatePullRequestBranchSchema = z.object({
817+
owner: z.string().describe("Repository owner (username or organization)"),
818+
repo: z.string().describe("Repository name"),
819+
pull_number: z.number().describe("Pull request number"),
820+
expected_head_sha: z.string().optional().describe("The expected SHA of the pull request's HEAD ref")
821+
});
822+
815823
export type CombinedStatus = z.infer<typeof CombinedStatusSchema>;
824+
export type UpdatePullRequestBranch = z.infer<typeof UpdatePullRequestBranchSchema>;

0 commit comments

Comments
 (0)