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