Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions docs/feature-flags.md
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,16 @@ as output formatting) won't appear here.
- `pullNumber`: The pull request number (number, required)
- `repo`: Repository name (string, required)

- **enable_pull_request_auto_merge** - Enable pull request auto-merge
- **OAuth Challenge Scopes**: `repo`
- `commit_message`: Extra detail for merge commit (string, optional)
- `commit_title`: Title for merge commit (string, optional)
- `expectedHeadSha`: The expected SHA of the pull request's HEAD ref (string, optional)
- `merge_method`: Merge method (string, optional)
- `owner`: Repository owner (string, required)
- `pullNumber`: Pull request number (number, required)
- `repo`: Repository name (string, required)

- **hide_pull_request_review** - Hide Pull Request Review
- **OAuth Challenge Scopes**: `repo`
- `classifier`: The reason for hiding the comment (string, required)
Expand Down
79 changes: 79 additions & 0 deletions pkg/github/__toolsnaps__/enable_pull_request_auto_merge.snap
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. If merge_method is omitted, GitHub defaults to merge.",
"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"
},
"expectedHeadSha": {
"description": "The expected SHA of the pull request's HEAD ref",
"type": "string"
},
"merge_method": {
"description": "Merge method",
"enum": [
"merge",
"squash",
"rebase"
],
"type": "string"
},
"owner": {
"description": "Repository owner",
"type": "string",
"x-mcp-header": "owner"
},
"pullNumber": {
"description": "Pull request number",
"type": "number"
},
"repo": {
"description": "Repository name",
"type": "string",
"x-mcp-header": "repo"
}
},
"required": [
"owner",
"repo",
"pullNumber"
],
"type": "object"
},
"name": "enable_pull_request_auto_merge",
"outputSchema": {
"additionalProperties": false,
"properties": {
"message": {
"description": "Operation result message.",
"type": "string"
}
},
"required": [
"message"
],
"type": "object"
}
}
23 changes: 22 additions & 1 deletion pkg/github/__toolsnaps__/pull_request_read.snap
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,26 @@
"array"
]
},
"auto_merge": {
"anyOf": [
{
"type": "null"
},
{
"additionalProperties": false,
"properties": {
"enabled_by": {
"type": "string"
},
"merge_method": {
"type": "string"
}
},
"type": "object"
}
],
"description": "Auto-merge settings, or null when auto-merge is not enabled."
},
"base": {
"additionalProperties": false,
"properties": {
Expand Down Expand Up @@ -342,7 +362,8 @@
"state",
"draft",
"merged",
"html_url"
"html_url",
"auto_merge"
],
"type": "object"
},
Expand Down
25 changes: 23 additions & 2 deletions pkg/github/consolidated_pull_request_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ type AddReplyToPullRequestCommentInput struct {
// pull_request_read has always returned as text.
type PullRequestReadOutput struct {
PullRequest *MinimalPullRequest
AutoMerge *PullRequestAutoMergeOutput
Diff *PullRequestDiffOutput
Status *MinimalCombinedStatus
Files *[]MinimalPRFile
Expand All @@ -119,6 +120,18 @@ type PullRequestReadOutput struct {
CheckRuns *MinimalCheckRunsResult
}

// PullRequestDetailsOutput extends the single pull request response without
// changing the shared list pull requests representation.
type PullRequestDetailsOutput struct {
MinimalPullRequest
AutoMerge *PullRequestAutoMergeOutput `json:"auto_merge"`
}

type PullRequestAutoMergeOutput struct {
MergeMethod string `json:"merge_method,omitempty"`
EnabledBy string `json:"enabled_by,omitempty"`
}

// PullRequestDiffOutput wraps the raw diff, which the text content returns verbatim.
type PullRequestDiffOutput struct {
Diff string `json:"diff"`
Expand All @@ -127,7 +140,7 @@ type PullRequestDiffOutput struct {
func (out PullRequestReadOutput) MarshalJSON() ([]byte, error) {
switch {
case out.PullRequest != nil:
return json.Marshal(out.PullRequest)
return json.Marshal(PullRequestDetailsOutput{MinimalPullRequest: *out.PullRequest, AutoMerge: out.AutoMerge})
case out.Diff != nil:
return json.Marshal(out.Diff)
case out.Status != nil:
Expand Down Expand Up @@ -170,9 +183,17 @@ func pullRequestReadOutputSchema() *jsonschema.Schema {
}
// An empty array is valid for every list method, so the list shapes form
// one anyOf array variant instead of ambiguous oneOf branches.
details := pullRequestOutputSchema[PullRequestDetailsOutput]()
details.Properties["auto_merge"] = &jsonschema.Schema{
Description: "Auto-merge settings, or null when auto-merge is not enabled.",
AnyOf: []*jsonschema.Schema{
{Type: "null"},
pullRequestOutputSchema[PullRequestAutoMergeOutput](),
},
}
return repositoryUnionSchema(
&jsonschema.Schema{Type: "null"},
pullRequestOutputSchema[MinimalPullRequest](),
details,
pullRequestOutputSchema[PullRequestDiffOutput](),
pullRequestOutputSchema[MinimalCombinedStatus](),
pullRequestOutputSchema[MinimalReviewThreadsResponse](),
Expand Down
1 change: 1 addition & 0 deletions pkg/github/granular_tools_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@ func TestPullRequestsGranularToolset(t *testing.T) {
"update_pull_request_body",
"update_pull_request_state",
"update_pull_request_draft_state",
"enable_pull_request_auto_merge",
"request_pull_request_reviewers",
"create_pull_request_review",
"submit_pending_pull_request_review",
Expand Down
90 changes: 90 additions & 0 deletions pkg/github/pull_request_auto_merge.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
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/modelcontextprotocol/go-sdk/mcp"
"github.com/shurcooL/githubv4"
)

// EnablePullRequestAutoMerge creates a tool to enable auto-merge for a pull request.
func EnablePullRequestAutoMerge(t translations.TranslationHelperFunc) inventory.ServerTool {
st := NewTool[MergePullRequestInput, *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. If merge_method is omitted, GitHub defaults to merge."),
InputSchema: mergePullRequestInputSchema(),
OutputSchema: pullRequestOutputSchema[RepositoryMessageOutput](),
Annotations: &mcp.ToolAnnotations{
Title: t("TOOL_ENABLE_PULL_REQUEST_AUTO_MERGE_USER_TITLE", "Enable pull request auto-merge"),
ReadOnlyHint: false,
},
},
scopes.RequireAll(scopes.Repo),
func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, input MergePullRequestInput) (*mcp.CallToolResult, *RepositoryMessageOutput, error) {
if input.PullNumber < 1 || input.PullNumber > math.MaxInt32 {
return utils.NewToolResultError("pullNumber 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 {
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("merge"),
)
st.FeatureRule = pullRequestsGranularFeatureRule
return st
}
Loading
Loading