Skip to content

feat(pull_requests): add pull request auto-merge tools - #3926

Open
plthomasva wants to merge 4 commits into
github:mainfrom
plthomasva:feat/pull-request-auto-merge-tools
Open

plthomasva wants to merge 4 commits into
github:mainfrom
plthomasva:feat/pull-request-auto-merge-tools

Conversation

@plthomasva

Copy link
Copy Markdown

Summary

Adds enable_pull_request_auto_merge and disable_pull_request_auto_merge tools to the pull requests toolset, wrapping the GraphQL enablePullRequestAutoMerge / disablePullRequestAutoMerge mutations so a PR can be set to merge automatically once its requirements are met — the MCP equivalent of gh pr merge --auto.

Why

Fixes #2411

Real-world use case (the one that prompted this PR): an agent running a series of verified, test-only PRs through this MCP server needed "merge as they land and are checked." The only available tool was merge_pull_request — an immediate merge that bypasses checks still running — because auto-merge was unreachable through the server. With these tools, the agent (or a user) can set auto-merge and let GitHub land the PR when it is green.

On shape: #2411 proposes an enable_auto_merge method "or similar" on the pull request write tools. This PR implements standalone tools mirroring the existing merge_pull_request tool (which is itself standalone rather than a method of a consolidated write tool), and the GraphQL plumbing mirrors the hide/unhide comment tools, which exist for the same reason — a GraphQL-only capability. Happy to restructure if maintainers prefer the method form.

What changed

  • New pkg/github/pullrequest_auto_merge.go: both tools share a factory and handler. Each resolves the pull request's node ID via the REST API (PullRequests.Get), then runs the mutation through the existing GraphQL client.
    • enable_pull_request_auto_merge: owner, repo, pullNumber (required); optional merge_method (merge/squash/rebase), commit_title, commit_message.
    • disable_pull_request_auto_merge: owner, repo, pullNumber.
    • Output: node_id, auto_merge_enabled, and (on enable) the merge_method GitHub recorded.
  • Registered both tools in pkg/github/tools.go, added an auto_merge argument spec in consolidated_pull_request_types.go, and added both tools to the pull-request-authoring skill's optional tools in pkg/skills/catalog/tools.go.
  • Unit tests (pullrequest_auto_merge_test.go) with mocked REST + GraphQL: enable with method/commit text, enable with defaults, disable, PR-not-found, and mutation-failure paths.
  • Tool snapshots generated; README/docs catalogs regenerated with script/generate-docs.

MCP impact

  • No tool or API changes
  • Tool schema or behavior changed
  • New tool added — two tools in the existing pull requests toolset; no existing tool changes behavior.

Prompts tested (tool changes only)

  • "Enable auto-merge on PR How to refer PR as a resource? Meaning of `path` parameter #652 in wishboards/wishboard with squash" — maps to enable_pull_request_auto_merge with merge_method: "squash".
  • "Once CI passes, auto-merge this PR" — maps to enable_pull_request_auto_merge with defaults.
  • "Turn off auto-merge for PR [docker] build arm64 #42" — maps to disable_pull_request_auto_merge.
  • Handler behavior for each path is covered by the unit tests listed above (mocked REST + GraphQL).

Security / limits

  • No security or limits impact
  • Auth / permissions considered — both tools require the repo scope (scopes.RequireAll(scopes.Repo)), like merge_pull_request. GitHub enforces the repository's auto-merge setting and the caller's write access server-side; failures surface as tool errors.
  • Data exposure, filtering, or token/size limits considered

Tool renaming

  • I am renaming tools as part of this PR
  • I am not renaming tools as part of this PR

Lint & tests

  • Linted locally with ./script/lint — golangci-lint reports 0 issues.
  • Tested locally with ./script/test — full ./pkg/... ./internal/... suite passes, including the skills catalog coverage tests.

Docs

  • Not needed
  • Updated (README / docs / examples) — regenerated with script/generate-docs.

Skills

  • Updated relevant skills or confirmed no bundled skill applies — both tools added to the pull-request-authoring skill's optional tool list in the catalog; TestCatalogToolRequirementsAreRegistered and TestToolsetInstructionsAreCoveredBySkills pass.

Disclosure: this change was developed with AI (model) assistance, using the workflow described in #2411's own context — fittingly, the feature exists because an agent needed it. All code was built, tested, and linted locally per CONTRIBUTING.

Implements the enablePullRequestAutoMerge and disablePullRequestAutoMerge GraphQL mutations as two new tools in the pull requests toolset, following the standalone shape of merge_pull_request (upstream issue github#2411 proposes an enable_auto_merge method 'or similar').

Each tool resolves the pull request's node ID through the REST API, then runs the mutation through the existing GraphQL client, mirroring the hide/unhide comment tools. Enable accepts an optional merge_method (merge/squash/rebase) and merge commit title/message.

Includes unit tests (mocked REST + GraphQL), tool snapshots, skills catalog registration, and regenerated README/docs catalogs. Full pkg/internal test suite passes; golangci-lint reports 0 issues.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Auto-merge enablement is incorrectly marked non-destructive despite authorizing potentially destructive merges, and both mutation failure paths misclassify GraphQL errors as REST errors.

2 open issues
What changed in this PR

Adds pull request auto-merge support to the GitHub MCP Server, addressing #2411 so agents can schedule merges without falling back to the GitHub CLI.

Changes:

  • Adds enable and disable tools backed by GraphQL mutations, with optional merge settings.
  • Registers both tools in the pull requests toolset and authoring skill catalog.
  • Adds mocked behavioral tests, tool snapshots, and README documentation.
File Description
README.md Documents both tools and their parameters.
pkg/​skills/​catalog/​tools.go Adds auto-merge tools to the authoring skill’s optional tools.
pkg/​github/​tools.go Registers both tools.
pkg/​github/​pullrequest_auto_merge.go Implements tool schemas, PR lookup, and GraphQL mutations.
pkg/​github/​pullrequest_auto_merge_test.go Tests tool schemas, successful operations, and error paths.
pkg/​github/​consolidated_pull_request_types.go Adds auto-merge argument normalization.
pkg/​github/​__toolsnaps__/​enable_pull_request_auto_merge.snap Records the enable tool’s schema and metadata.
pkg/​github/​__toolsnaps__/​disable_pull_request_auto_merge.snap Records the disable tool’s schema and metadata.

Review effort: Balanced (adaptive)

Comment thread pkg/github/pullrequest_auto_merge.go Resolved
Comment thread pkg/github/pullrequest_auto_merge.go Resolved
- Enable tool now sets destructiveHint: true (enabling auto-merge authorizes a later merge of the base branch); disable stays false. Enable snapshot regenerated.

- Mutation failures now use NewGitHubGraphQLErrorResponse instead of the REST error helper, matching comment_minimize.go, so GetGitHubGraphQLErrors consumers see enable/disable failures.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

When no merge method is specified, auto-merge uses GraphQL’s MERGE default rather than a repository-permitted method, causing the parameterless workflow to fail on squash- or rebase-only repositories.

No open issues 🎉

1 previously missed issue

In code that hasn't changed since last review

Medium severity Use the repository's merge method when none is specified

pkg/​github/​pullrequest_auto_merge.go:176

Issue #2411 specifies that an omitted merge method should use the repository setting, but this path leaves mergeMethod unset, which uses GraphQL's MERGE default. On a repository that permits only squash or rebase, the advertised parameterless workflow ("Once CI passes, auto-merge this PR") therefore fails despite an allowed merge method being available. Resolve a repository-permitted default when the argument is omitted, and update the default-path test and documentation accordingly.

2 resolved since last review

Review effort: Balanced (adaptive)

…nable tool

Per the GraphQL schema, an omitted mergeMethod defaults to MERGE; on repositories that disallow merge commits the mutation fails with GitHub's error. Say so in the parameter description so callers pass a permitted method. Verified empirically on a squash-only repository (see PR discussion).
@plthomasva

Copy link
Copy Markdown
Author

On the "previously missed" finding — use the repository's merge method when none is specified — we checked this against the schema docs and against a live repository before deciding. Summary: the finding's premise is correct, and the failure it predicts is real, but it surfaces as a loud, enable-time error from GitHub itself rather than a silent wrong-method merge, so we have documented the behavior instead of adding client-side method resolution. Details:

What the documentation says

  • The GraphQL reference for EnablePullRequestAutoMergeInput.mergeMethod says: "The merge method to use. If omitted, defaults to MERGE." There is no repository-aware default at the API layer; the "default to repo setting" phrasing in Add auto-merge support to pull request tools #2411 describes the web UI's behavior (it only offers permitted methods), not the mutation's.
  • Repository settings expose three independent booleans (allow_merge_commit, allow_squash_merge, allow_rebase_merge) — there is no single "repository merge method" value to resolve to — and branch rulesets can narrow the permitted methods further per branch, so a client-side resolver cannot reliably reproduce GitHub's own decision.

What actually happens (empirical)

We configured a scratch repository as squash-only (allow_merge_commit: false, allow_rebase_merge: false, auto-merge on, base branch held by a required check) and called the mutation with no mergeMethod. GitHub rejected it at enable time:

{"type": "UNPROCESSABLE", "path": ["enablePullRequestAutoMerge"],
 "message": "Merge method merge commits are not allowed on this repository"}

So the disallowed-method case is validated by GitHub, immediately, with a specific message — and this tool returns that GraphQL error to the caller verbatim. No auto-merge is armed with a wrong method, and nothing fails later or silently. This is also the same contract as the sibling merge_pull_request tool, which leaves merge_method optional and lets GitHub refuse a disallowed method (e.g. 405 Merge commits are not allowed on this repository).

What we changed

Commit 9b621655 updates the merge_method parameter description on the enable tool to state the ground truth: if omitted, GitHub applies MERGE, which fails on repositories that do not allow merge commits, so callers should pass the method the repository permits.

If maintainers would still prefer client-side resolution of a permitted method when the argument is omitted, we are happy to implement it — it would need a policy for choosing when several methods are permitted (e.g. both squash and rebase), which is why we did not pick one unilaterally here.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The README description is out of sync with the updated merge_method schema, so the docs-check workflow will fail until the documentation is regenerated and committed.

1 open issue

Review effort: Balanced (adaptive)

Comment thread README.md Resolved
The README's generated tool documentation still carried the pre-9b621655
merge_method wording for enable_pull_request_auto_merge. Regenerated
with generate-docs so the docs check passes.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔵 Needs a closer look

The new tools authorize future base-branch merges and depend on GitHub repository rules and merge-queue semantics, exceeding the limited-consequence automated review scope and warranting human review.

No open issues 🎉

1 resolved since last review

Review effort: Balanced (adaptive)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add auto-merge support to pull request tools

2 participants