Skip to main content

Overview

The mergePullRequest method allows you to programmatically merge an existing pull request on GitHub. It supports different merge methods (merge, squash, rebase) and allows customization of the commit message.

Method Signature

Parameters

MergePullRequestOptions

Merge Methods

  • merge: Creates a merge commit with all commits from the feature branch
  • squash: Squashes all commits into a single commit before merging
  • rebase: Rebases the commits onto the base branch

Return Value

MergePullRequestResult

Prerequisites

Before using this method, ensure:
  1. GitHub Configuration: You only need to configure GitHub credentials using secrets:
    Note: Unlike other methods, mergePullRequest does NOT require agent or sandbox configuration.
  2. Pull Request State: The pull request must be:
    • Open and not already merged
    • Free of merge conflicts
    • Passing all required status checks
    • Approved if required by branch protection rules

Usage Examples

Basic Merge

Squash and Merge

Complete Workflow Example

Error Handling

The method will throw an error in the following cases:

Configuration Errors

  • Missing GitHub token or repository configuration
  • Invalid repository URL format

Pull Request Errors

  • 404: Pull request not found
  • 405: Pull request is not mergeable (conflicts or failed checks)
  • 422: Invalid merge parameters or validation failed

Example Error Handling

Notes

  • No agent or sandbox required: This method only needs GitHub configuration
  • The method requires appropriate GitHub permissions (write access to the repository)
  • Branch protection rules will be enforced
  • The merge will respect all repository settings and requirements
  • After a successful merge, the source branch may be automatically deleted depending on repository settings
  • This is a direct GitHub API call - no code execution or AI processing is involved