Skip to content

Commit 9b7b200

Browse files
timrogersCopilot
andauthored
Add pull request auto-merge tool (#3986)
* Add pull request auto-merge tool Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> * Clarify when to enable pull request auto-merge Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> * Expose pull request auto-merge state Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> * Address auto-merge tool review feedback Use consistent snake_case inputs for the new tool, title-case its metadata, and clarify auto-merge defaults and repository requirements without changing the existing merge tool API. Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
1 parent be379ae commit 9b7b200

14 files changed

Lines changed: 446 additions & 11 deletions

‎docs/feature-flags.md‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,16 @@ as output formatting) won't appear here.
431431
- `pullNumber`: The pull request number (number, required)
432432
- `repo`: Repository name (string, required)
433433

434+
- **enable_pull_request_auto_merge** - Enable Pull Request Auto-Merge
435+
- **OAuth Challenge Scopes**: `repo`
436+
- `commit_message`: Extra detail for merge commit (string, optional)
437+
- `commit_title`: Title for merge commit (string, optional)
438+
- `expected_head_sha`: The expected SHA of the pull request's HEAD ref (string, optional)
439+
- `merge_method`: Merge method. Defaults to merge when omitted. (string, optional)
440+
- `owner`: Repository owner (string, required)
441+
- `pull_number`: Pull request number (number, required)
442+
- `repo`: Repository name (string, required)
443+
434444
- **hide_pull_request_review** - Hide Pull Request Review
435445
- **OAuth Challenge Scopes**: `repo`
436446
- `classifier`: The reason for hiding the comment (string, required)
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"annotations": {
3+
"idempotentHint": false,
4+
"readOnlyHint": false,
5+
"title": "Enable Pull Request Auto-Merge"
6+
},
7+
"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.",
8+
"icons": [
9+
{
10+
"mimeType": "image/png",
11+
"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",
12+
"theme": "light"
13+
},
14+
{
15+
"mimeType": "image/png",
16+
"src": "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABgAAAAYCAYAAADgdz34AAAABmJLR0QA/wD/AP+gvaeTAAABn0lEQVRIie2Vvy5EURDGvyMUkhUU/pS2Q2clwvIKqL0Cm9ArrBcQShJPoLFR2YaIhIjQ7a7E39IiCgqVn8KsPY697E3oTHPumTPf990z986M9Mfm/A0wKGlMUlnSlnPuOQQAE5LOnXOFWErAIvBK1S6BZBAzZ2fzcckHjXwVaAXSwD2wWYN8A2iKK1ABt3m+ZeDRnseIthdgDxioxd1o662tfZIO7Lnf8xcklST1StqRdORxNEualHQIDDvnTmvdIGE5vwdWgLy93bQX0w0UgSdgKMC3AdfA7ndpSgKbduUbYBoI/7Ju4BiYrYFfAl4iBbxAgOyPgV9xWYDQ3xCXKK79C/wL/KJZoeW8QpsJCy0C54AMULaGmQu7sIAW4MpaxTKwbQU3U4dAxmLzwLpxXAIJP2jKgkY8Xx4o1SFwBmx7+7RxTUnVb9Bpa9HDFiR1/SRgWH+6FT3/h2qK6sBpB0aBB7yB880NcpaWtGHXjCsVBmb5PDIvgJ46BJKW84q9AguV87Adp/Q+9O8UMfQjRBKSxiV1SNp3zp3Ug/sVewPruexhKwhGXQAAAABJRU5ErkJggg==",
17+
"theme": "dark"
18+
}
19+
],
20+
"inputSchema": {
21+
"properties": {
22+
"commit_message": {
23+
"description": "Extra detail for merge commit",
24+
"type": "string"
25+
},
26+
"commit_title": {
27+
"description": "Title for merge commit",
28+
"type": "string"
29+
},
30+
"expected_head_sha": {
31+
"description": "The expected SHA of the pull request's HEAD ref",
32+
"type": "string"
33+
},
34+
"merge_method": {
35+
"description": "Merge method. Defaults to merge when omitted.",
36+
"enum": [
37+
"merge",
38+
"squash",
39+
"rebase"
40+
],
41+
"type": "string"
42+
},
43+
"owner": {
44+
"description": "Repository owner",
45+
"type": "string",
46+
"x-mcp-header": "owner"
47+
},
48+
"pull_number": {
49+
"description": "Pull request number",
50+
"type": "number"
51+
},
52+
"repo": {
53+
"description": "Repository name",
54+
"type": "string",
55+
"x-mcp-header": "repo"
56+
}
57+
},
58+
"required": [
59+
"owner",
60+
"repo",
61+
"pull_number"
62+
],
63+
"type": "object"
64+
},
65+
"name": "enable_pull_request_auto_merge",
66+
"outputSchema": {
67+
"additionalProperties": false,
68+
"properties": {
69+
"message": {
70+
"description": "Operation result message.",
71+
"type": "string"
72+
}
73+
},
74+
"required": [
75+
"message"
76+
],
77+
"type": "object"
78+
}
79+
}

‎pkg/github/__toolsnaps__/pull_request_read.snap‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,26 @@
8080
"array"
8181
]
8282
},
83+
"auto_merge": {
84+
"anyOf": [
85+
{
86+
"type": "null"
87+
},
88+
{
89+
"additionalProperties": false,
90+
"properties": {
91+
"enabled_by": {
92+
"type": "string"
93+
},
94+
"merge_method": {
95+
"type": "string"
96+
}
97+
},
98+
"type": "object"
99+
}
100+
],
101+
"description": "Auto-merge settings for the pull request, or null when auto-merge is not enabled."
102+
},
83103
"base": {
84104
"additionalProperties": false,
85105
"properties": {
@@ -342,7 +362,8 @@
342362
"state",
343363
"draft",
344364
"merged",
345-
"html_url"
365+
"html_url",
366+
"auto_merge"
346367
],
347368
"type": "object"
348369
},

‎pkg/github/consolidated_pull_request_types.go‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ type AddReplyToPullRequestCommentInput struct {
109109
// pull_request_read has always returned as text.
110110
type PullRequestReadOutput struct {
111111
PullRequest *MinimalPullRequest
112+
AutoMerge *PullRequestAutoMergeOutput
112113
Diff *PullRequestDiffOutput
113114
Status *MinimalCombinedStatus
114115
Files *[]MinimalPRFile
@@ -119,6 +120,18 @@ type PullRequestReadOutput struct {
119120
CheckRuns *MinimalCheckRunsResult
120121
}
121122

123+
// PullRequestDetailsOutput extends the single pull request response without
124+
// changing the shared list pull requests representation.
125+
type PullRequestDetailsOutput struct {
126+
MinimalPullRequest
127+
AutoMerge *PullRequestAutoMergeOutput `json:"auto_merge"`
128+
}
129+
130+
type PullRequestAutoMergeOutput struct {
131+
MergeMethod string `json:"merge_method,omitempty"`
132+
EnabledBy string `json:"enabled_by,omitempty"`
133+
}
134+
122135
// PullRequestDiffOutput wraps the raw diff, which the text content returns verbatim.
123136
type PullRequestDiffOutput struct {
124137
Diff string `json:"diff"`
@@ -127,7 +140,7 @@ type PullRequestDiffOutput struct {
127140
func (out PullRequestReadOutput) MarshalJSON() ([]byte, error) {
128141
switch {
129142
case out.PullRequest != nil:
130-
return json.Marshal(out.PullRequest)
143+
return json.Marshal(PullRequestDetailsOutput{MinimalPullRequest: *out.PullRequest, AutoMerge: out.AutoMerge})
131144
case out.Diff != nil:
132145
return json.Marshal(out.Diff)
133146
case out.Status != nil:
@@ -170,9 +183,17 @@ func pullRequestReadOutputSchema() *jsonschema.Schema {
170183
}
171184
// An empty array is valid for every list method, so the list shapes form
172185
// one anyOf array variant instead of ambiguous oneOf branches.
186+
details := pullRequestOutputSchema[PullRequestDetailsOutput]()
187+
details.Properties["auto_merge"] = &jsonschema.Schema{
188+
Description: "Auto-merge settings for the pull request, or null when auto-merge is not enabled.",
189+
AnyOf: []*jsonschema.Schema{
190+
{Type: "null"},
191+
pullRequestOutputSchema[PullRequestAutoMergeOutput](),
192+
},
193+
}
173194
return repositoryUnionSchema(
174195
&jsonschema.Schema{Type: "null"},
175-
pullRequestOutputSchema[MinimalPullRequest](),
196+
details,
176197
pullRequestOutputSchema[PullRequestDiffOutput](),
177198
pullRequestOutputSchema[MinimalCombinedStatus](),
178199
pullRequestOutputSchema[MinimalReviewThreadsResponse](),
@@ -431,6 +452,10 @@ var pullRequestArgumentSpecs = map[string]pullRequestArgumentSpec{
431452
required: []string{"owner", "repo"}, requiredInts: []string{"pullNumber"},
432453
strings: []string{"commit_title", "commit_message", "merge_method", "expectedHeadSha"},
433454
},
455+
"auto_merge": {
456+
required: []string{"owner", "repo"}, requiredInts: []string{"pull_number"},
457+
strings: []string{"commit_title", "commit_message", "merge_method", "expected_head_sha"},
458+
},
434459
"branch": {
435460
required: []string{"owner", "repo"}, requiredInts: []string{"pullNumber"}, strings: []string{"expectedHeadSha"},
436461
},

‎pkg/github/granular_tools_test.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ func TestPullRequestsGranularToolset(t *testing.T) {
133133
"update_pull_request_body",
134134
"update_pull_request_state",
135135
"update_pull_request_draft_state",
136+
"enable_pull_request_auto_merge",
136137
"request_pull_request_reviewers",
137138
"create_pull_request_review",
138139
"submit_pending_pull_request_review",
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"math"
6+
"strings"
7+
8+
ghErrors "github.com/github/github-mcp-server/v2/pkg/errors"
9+
"github.com/github/github-mcp-server/v2/pkg/inventory"
10+
"github.com/github/github-mcp-server/v2/pkg/scopes"
11+
"github.com/github/github-mcp-server/v2/pkg/translations"
12+
"github.com/github/github-mcp-server/v2/pkg/utils"
13+
"github.com/google/jsonschema-go/jsonschema"
14+
"github.com/modelcontextprotocol/go-sdk/mcp"
15+
"github.com/shurcooL/githubv4"
16+
)
17+
18+
type EnablePullRequestAutoMergeInput struct {
19+
Owner string `json:"owner"`
20+
Repo string `json:"repo"`
21+
PullNumber int `json:"pull_number"`
22+
CommitTitle *string `json:"commit_title,omitempty"`
23+
CommitMessage *string `json:"commit_message,omitempty"`
24+
MergeMethod *string `json:"merge_method,omitempty"`
25+
ExpectedHeadSHA *string `json:"expected_head_sha,omitempty"`
26+
}
27+
28+
// EnablePullRequestAutoMerge creates a tool to enable auto-merge for a pull request.
29+
func EnablePullRequestAutoMerge(t translations.TranslationHelperFunc) inventory.ServerTool {
30+
st := NewTool[EnablePullRequestAutoMergeInput, *RepositoryMessageOutput](
31+
ToolsetMetadataPullRequests,
32+
mcp.Tool{
33+
Name: "enable_pull_request_auto_merge",
34+
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."),
35+
OutputSchema: pullRequestOutputSchema[RepositoryMessageOutput](),
36+
Annotations: &mcp.ToolAnnotations{
37+
Title: t("TOOL_ENABLE_PULL_REQUEST_AUTO_MERGE_USER_TITLE", "Enable Pull Request Auto-Merge"),
38+
ReadOnlyHint: false,
39+
},
40+
InputSchema: &jsonschema.Schema{
41+
Type: "object",
42+
Properties: map[string]*jsonschema.Schema{
43+
"owner": {
44+
Type: "string",
45+
Description: "Repository owner",
46+
},
47+
"repo": {
48+
Type: "string",
49+
Description: "Repository name",
50+
},
51+
"pull_number": {
52+
Type: "number",
53+
Description: "Pull request number",
54+
},
55+
"commit_title": {
56+
Type: "string",
57+
Description: "Title for merge commit",
58+
},
59+
"commit_message": {
60+
Type: "string",
61+
Description: "Extra detail for merge commit",
62+
},
63+
"merge_method": {
64+
Type: "string",
65+
Description: "Merge method. Defaults to merge when omitted.",
66+
Enum: []any{"merge", "squash", "rebase"},
67+
},
68+
"expected_head_sha": {
69+
Type: "string",
70+
Description: "The expected SHA of the pull request's HEAD ref",
71+
},
72+
},
73+
Required: []string{"owner", "repo", "pull_number"},
74+
},
75+
},
76+
scopes.RequireAll(scopes.Repo),
77+
func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, input EnablePullRequestAutoMergeInput) (*mcp.CallToolResult, *RepositoryMessageOutput, error) {
78+
if input.PullNumber < 1 || input.PullNumber > math.MaxInt32 {
79+
return utils.NewToolResultError("pull_number must be a positive 32-bit integer"), nil, nil
80+
}
81+
mutationInput := githubv4.EnablePullRequestAutoMergeInput{}
82+
if input.MergeMethod != nil {
83+
switch *input.MergeMethod {
84+
case "merge", "squash", "rebase":
85+
method := githubv4.PullRequestMergeMethod(strings.ToUpper(*input.MergeMethod))
86+
mutationInput.MergeMethod = &method
87+
default:
88+
return utils.NewToolResultError("merge_method must be merge, squash, or rebase"), nil, nil
89+
}
90+
}
91+
if input.CommitTitle != nil {
92+
title := githubv4.String(*input.CommitTitle)
93+
mutationInput.CommitHeadline = &title
94+
}
95+
if input.CommitMessage != nil {
96+
message := githubv4.String(*input.CommitMessage)
97+
mutationInput.CommitBody = &message
98+
}
99+
if input.ExpectedHeadSHA != nil {
100+
sha := githubv4.GitObjectID(*input.ExpectedHeadSHA)
101+
mutationInput.ExpectedHeadOid = &sha
102+
}
103+
104+
client, err := deps.GetGQLClient(ctx)
105+
if err != nil {
106+
return utils.NewToolResultErrorFromErr("failed to get GitHub GraphQL client", err), nil, nil
107+
}
108+
id, err := resolvePullRequestNodeID(ctx, client, input.Owner, input.Repo, input.PullNumber)
109+
if err != nil {
110+
return ghErrors.NewGitHubGraphQLErrorResponse(ctx, "failed to get pull request", err), nil, nil
111+
}
112+
if id == nil || id == "" {
113+
return utils.NewToolResultError("pull request not found"), nil, nil
114+
}
115+
mutationInput.PullRequestID = id
116+
var mutation struct {
117+
EnablePullRequestAutoMerge struct {
118+
PullRequest struct {
119+
ID githubv4.ID
120+
}
121+
} `graphql:"enablePullRequestAutoMerge(input: $input)"`
122+
}
123+
if err := client.Mutate(ctx, &mutation, mutationInput, nil); err != nil {
124+
return ghErrors.NewGitHubGraphQLErrorResponse(ctx, "failed to enable pull request auto-merge", err), nil, nil
125+
}
126+
if mutation.EnablePullRequestAutoMerge.PullRequest.ID == nil || mutation.EnablePullRequestAutoMerge.PullRequest.ID == "" {
127+
return utils.NewToolResultError("GitHub did not return a pull request after enabling auto-merge"), nil, nil
128+
}
129+
return pullRequestMessageResult(utils.NewToolResultText("pull request auto-merge enabled"), nil)
130+
},
131+
normalizePullRequestArguments("auto_merge"),
132+
)
133+
st.FeatureRule = pullRequestsGranularFeatureRule
134+
return st
135+
}

0 commit comments

Comments
 (0)