Repository navigation
Add pull request auto-merge tool #3986
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
+446
−11
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
79 changes: 79 additions & 0 deletions
79
pkg/github/__toolsnaps__/enable_pull_request_auto_merge.snap
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| { | ||
| "annotations": { | ||
| "idempotentHint": false, | ||
| "readOnlyHint": false, | ||
| "title": "Enable Pull Request Auto-Merge" | ||
| }, | ||
| "description": "Enable auto-merge for a pull request that cannot be merged immediately because merge requirements are pending. GitHub merges it once those requirements are met. Returns an error if auto-merge is not enabled for the repository.", | ||
| "icons": [ | ||
| { | ||
| "mimeType": "image/png", | ||
| "src": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABmJLR0QA/wD/AP+gvaeTAAACwUlEQVRIie2Vz28UZRjHP993pi0QIC3YahNjirtmd3bS3Q1eUHvQEPUiEv8A4kXjwRJ78MCFBLjBBRKCHowHE38cNCbGGx6IUoKiodtNpoNmTJp4oSJNQ3pw29l5POxus2wo3QTwxPc0887zfD7zvu9kXnjEUfdNrjj5vJOmMP4e9JrfR1G02tuQD8tvgpck0dxCPwK30ViqnJTcr4bOmfRlI/PrhUJ5313woDpDpu8ss7f6nYHrvDnGcYlPsoY/bKaXwHY3HWfvgmNnMX0zvMM7069A3c3pkEYWa7UVgFxQPSfs7SSeH3k2rEy5jMubMBoG1yQ+SBbm53of+gCybMkk/H8VAFdbZisZLAFsJ11oyL+BUURcwrjWAZixXeIwxs/5UuVAr0QAYRjubGR+HWy3mb6QCIBXQe8nce0jgIkwfMo3/xLG085x8I9ofkMyUa0O+w2rgS0mcf3lboEDiKJo1cvsIDDr4D1DhTb8407hYhTdTJW+AvrdMnuhG9Je1m9BBzbfjXbyQcXypeqJLQt7+0rVE/mgYr3j7l7FDzOPBY8FDx6vc1EolPeNjI5/Jpgw7Lm9o+Pry//c/K0PhnLFyrSMDxE79jwxvn9079gvt28vrUD7V1EoFHalbltd2C7DfS4sAF4DTSdx7cL96LliZVriPPADuL+geRh0Z8il5SiKVn2ATENvCCYw78U/b8xdBcgHlYuGHQXuK5A4ClxM4vnXW8Lqp5JdWWt6h4CvWnsgxgDSbRZ3Gg0tCJ7sY4nGwDZOt/WBZtzN9FswLgM2sGann5mcPDaw5pWEHQH7cUu86SdkR3LF6tfrA814MNVpwNrM1leUxPXrSKfMeHcwdctyNotY8c3NbMX3LJsB3ZHsymDqlkHvYHYyievXWxPpSj4o75eYIuPWZof+vRKG4c61pncIx6gZsx34/5L/ACy3ElqUYhuvAAAAAElFTkSuQmCC", | ||
| "theme": "light" | ||
| }, | ||
| { | ||
| "mimeType": "image/png", | ||
| "src": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABmJLR0QA/wD/AP+gvaeTAAABn0lEQVRIie2Vvy5EURDGvyMUkhUU/pS2Q2clwvIKqL0Cm9ArrBcQShJPoLFR2YaIhIjQ7a7E39IiCgqVn8KsPY697E3oTHPumTPf990z986M9Mfm/A0wKGlMUlnSlnPuOQQAE5LOnXOFWErAIvBK1S6BZBAzZ2fzcckHjXwVaAXSwD2wWYN8A2iKK1ABt3m+ZeDRnseIthdgDxioxd1o662tfZIO7Lnf8xcklST1StqRdORxNEualHQIDDvnTmvdIGE5vwdWgLy93bQX0w0UgSdgKMC3AdfA7ndpSgKbduUbYBoI/7Ju4BiYrYFfAl4iBbxAgOyPgV9xWYDQ3xCXKK79C/wL/KJZoeW8QpsJCy0C54AMULaGmQu7sIAW4MpaxTKwbQU3U4dAxmLzwLpxXAIJP2jKgkY8Xx4o1SFwBmx7+7RxTUnVb9Bpa9HDFiR1/SRgWH+6FT3/h2qK6sBpB0aBB7yB880NcpaWtGHXjCsVBmb5PDIvgJ46BJKW84q9AguV87Adp/Q+9O8UMfQjRBKSxiV1SNp3zp3Ug/sVewPruexhKwhGXQAAAABJRU5ErkJggg==", | ||
| "theme": "dark" | ||
| } | ||
| ], | ||
| "inputSchema": { | ||
| "properties": { | ||
| "commit_message": { | ||
| "description": "Extra detail for merge commit", | ||
| "type": "string" | ||
| }, | ||
| "commit_title": { | ||
| "description": "Title for merge commit", | ||
| "type": "string" | ||
| }, | ||
| "expected_head_sha": { | ||
| "description": "The expected SHA of the pull request's HEAD ref", | ||
| "type": "string" | ||
| }, | ||
| "merge_method": { | ||
|
timrogers marked this conversation as resolved.
Resolved
|
||
| "description": "Merge method. Defaults to merge when omitted.", | ||
| "enum": [ | ||
| "merge", | ||
| "squash", | ||
| "rebase" | ||
| ], | ||
| "type": "string" | ||
| }, | ||
| "owner": { | ||
| "description": "Repository owner", | ||
| "type": "string", | ||
| "x-mcp-header": "owner" | ||
| }, | ||
| "pull_number": { | ||
| "description": "Pull request number", | ||
| "type": "number" | ||
| }, | ||
| "repo": { | ||
| "description": "Repository name", | ||
| "type": "string", | ||
| "x-mcp-header": "repo" | ||
| } | ||
| }, | ||
| "required": [ | ||
| "owner", | ||
| "repo", | ||
| "pull_number" | ||
| ], | ||
| "type": "object" | ||
| }, | ||
| "name": "enable_pull_request_auto_merge", | ||
| "outputSchema": { | ||
| "additionalProperties": false, | ||
| "properties": { | ||
| "message": { | ||
| "description": "Operation result message.", | ||
| "type": "string" | ||
| } | ||
| }, | ||
| "required": [ | ||
| "message" | ||
| ], | ||
| "type": "object" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,135 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "context" | ||
| "math" | ||
| "strings" | ||
|
|
||
| ghErrors "github.com/github/github-mcp-server/v2/pkg/errors" | ||
| "github.com/github/github-mcp-server/v2/pkg/inventory" | ||
| "github.com/github/github-mcp-server/v2/pkg/scopes" | ||
| "github.com/github/github-mcp-server/v2/pkg/translations" | ||
| "github.com/github/github-mcp-server/v2/pkg/utils" | ||
| "github.com/google/jsonschema-go/jsonschema" | ||
| "github.com/modelcontextprotocol/go-sdk/mcp" | ||
| "github.com/shurcooL/githubv4" | ||
| ) | ||
|
|
||
| type EnablePullRequestAutoMergeInput struct { | ||
| Owner string `json:"owner"` | ||
| Repo string `json:"repo"` | ||
| PullNumber int `json:"pull_number"` | ||
| CommitTitle *string `json:"commit_title,omitempty"` | ||
| CommitMessage *string `json:"commit_message,omitempty"` | ||
| MergeMethod *string `json:"merge_method,omitempty"` | ||
| ExpectedHeadSHA *string `json:"expected_head_sha,omitempty"` | ||
| } | ||
|
|
||
| // EnablePullRequestAutoMerge creates a tool to enable auto-merge for a pull request. | ||
| func EnablePullRequestAutoMerge(t translations.TranslationHelperFunc) inventory.ServerTool { | ||
|
timrogers marked this conversation as resolved.
Resolved
|
||
| st := NewTool[EnablePullRequestAutoMergeInput, *RepositoryMessageOutput]( | ||
| ToolsetMetadataPullRequests, | ||
| mcp.Tool{ | ||
| Name: "enable_pull_request_auto_merge", | ||
| Description: t("TOOL_ENABLE_PULL_REQUEST_AUTO_MERGE_DESCRIPTION", "Enable auto-merge for a pull request that cannot be merged immediately because merge requirements are pending. GitHub merges it once those requirements are met. Returns an error if auto-merge is not enabled for the repository."), | ||
| OutputSchema: pullRequestOutputSchema[RepositoryMessageOutput](), | ||
| Annotations: &mcp.ToolAnnotations{ | ||
| Title: t("TOOL_ENABLE_PULL_REQUEST_AUTO_MERGE_USER_TITLE", "Enable Pull Request Auto-Merge"), | ||
| ReadOnlyHint: false, | ||
| }, | ||
| InputSchema: &jsonschema.Schema{ | ||
| Type: "object", | ||
| Properties: map[string]*jsonschema.Schema{ | ||
| "owner": { | ||
| Type: "string", | ||
| Description: "Repository owner", | ||
| }, | ||
| "repo": { | ||
| Type: "string", | ||
| Description: "Repository name", | ||
| }, | ||
| "pull_number": { | ||
| Type: "number", | ||
| Description: "Pull request number", | ||
| }, | ||
| "commit_title": { | ||
| Type: "string", | ||
| Description: "Title for merge commit", | ||
| }, | ||
| "commit_message": { | ||
| Type: "string", | ||
| Description: "Extra detail for merge commit", | ||
| }, | ||
| "merge_method": { | ||
| Type: "string", | ||
| Description: "Merge method. Defaults to merge when omitted.", | ||
| Enum: []any{"merge", "squash", "rebase"}, | ||
| }, | ||
| "expected_head_sha": { | ||
| Type: "string", | ||
| Description: "The expected SHA of the pull request's HEAD ref", | ||
| }, | ||
| }, | ||
| Required: []string{"owner", "repo", "pull_number"}, | ||
| }, | ||
| }, | ||
| scopes.RequireAll(scopes.Repo), | ||
| func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, input EnablePullRequestAutoMergeInput) (*mcp.CallToolResult, *RepositoryMessageOutput, error) { | ||
| if input.PullNumber < 1 || input.PullNumber > math.MaxInt32 { | ||
| return utils.NewToolResultError("pull_number must be a positive 32-bit integer"), nil, nil | ||
| } | ||
| mutationInput := githubv4.EnablePullRequestAutoMergeInput{} | ||
| if input.MergeMethod != nil { | ||
| switch *input.MergeMethod { | ||
| case "merge", "squash", "rebase": | ||
| method := githubv4.PullRequestMergeMethod(strings.ToUpper(*input.MergeMethod)) | ||
| mutationInput.MergeMethod = &method | ||
| default: | ||
| return utils.NewToolResultError("merge_method must be merge, squash, or rebase"), nil, nil | ||
| } | ||
| } | ||
| if input.CommitTitle != nil { | ||
| title := githubv4.String(*input.CommitTitle) | ||
| mutationInput.CommitHeadline = &title | ||
| } | ||
| if input.CommitMessage != nil { | ||
| message := githubv4.String(*input.CommitMessage) | ||
| mutationInput.CommitBody = &message | ||
| } | ||
| if input.ExpectedHeadSHA != nil { | ||
| sha := githubv4.GitObjectID(*input.ExpectedHeadSHA) | ||
| mutationInput.ExpectedHeadOid = &sha | ||
| } | ||
|
|
||
| client, err := deps.GetGQLClient(ctx) | ||
| if err != nil { | ||
| return utils.NewToolResultErrorFromErr("failed to get GitHub GraphQL client", err), nil, nil | ||
| } | ||
| id, err := resolvePullRequestNodeID(ctx, client, input.Owner, input.Repo, input.PullNumber) | ||
| if err != nil { | ||
| return ghErrors.NewGitHubGraphQLErrorResponse(ctx, "failed to get pull request", err), nil, nil | ||
| } | ||
| if id == nil || id == "" { | ||
| return utils.NewToolResultError("pull request not found"), nil, nil | ||
| } | ||
| mutationInput.PullRequestID = id | ||
| var mutation struct { | ||
| EnablePullRequestAutoMerge struct { | ||
| PullRequest struct { | ||
| ID githubv4.ID | ||
| } | ||
| } `graphql:"enablePullRequestAutoMerge(input: $input)"` | ||
| } | ||
| if err := client.Mutate(ctx, &mutation, mutationInput, nil); err != nil { | ||
|
timrogers marked this conversation as resolved.
Resolved
|
||
| return ghErrors.NewGitHubGraphQLErrorResponse(ctx, "failed to enable pull request auto-merge", err), nil, nil | ||
| } | ||
| if mutation.EnablePullRequestAutoMerge.PullRequest.ID == nil || mutation.EnablePullRequestAutoMerge.PullRequest.ID == "" { | ||
| return utils.NewToolResultError("GitHub did not return a pull request after enabling auto-merge"), nil, nil | ||
| } | ||
| return pullRequestMessageResult(utils.NewToolResultText("pull request auto-merge enabled"), nil) | ||
| }, | ||
| normalizePullRequestArguments("auto_merge"), | ||
| ) | ||
| st.FeatureRule = pullRequestsGranularFeatureRule | ||
| return st | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.