merge_pr
Merges a pull request in a GitHub repository using your choice of merge, squash, or rebase methods.
Instructions
Merges a specific pull request in a GitHub repository using the specified merge method. If merge pr is fails use update_pr_branch to update the branch with the latest changes from the base branch and try merging again after CI finishes. Args: repo_owner (str): The owner of the repository. repo_name (str): The name of the repository. pr_number (int): The pull request number to merge. commit_title (str, optional): The title for the merge commit. Defaults to None. commit_message (str, optional): The message for the merge commit. Defaults to None. merge_method (Literal['merge', 'squash', 'rebase'], optional): The merge method to use ('merge', 'squash', or 'rebase'). Defaults to 'squash'. Returns: Dict[str, Any]: The JSON response from the GitHub API containing merge information if successful. Error Handling: Logs errors and prints the traceback if the merge fails, returning None.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| repo_owner | Yes | ||
| repo_name | Yes | ||
| pr_number | Yes | ||
| commit_title | No | ||
| commit_message | No | ||
| merge_method | No | squash |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- The merge_pr method on GitHubIntegration class - the core implementation that merges a pull request via PUT to GitHub's /pulls/{pr_number}/merge endpoint. Supports merge, squash, and rebase methods. Handles GitHubAPIError and httpx.HTTPError with error logging.
def merge_pr( self, repo_owner: str, repo_name: str, pr_number: int, commit_title: str | None = None, commit_message: str | None = None, merge_method: Literal["merge", "squash", "rebase"] = "squash", ) -> dict[str, Any]: """ Merges a specific pull request in a GitHub repository using the specified merge method. If merge pr is fails use update_pr_branch to update the branch with the latest changes from the base branch and try merging again after CI finishes. Args: repo_owner (str): The owner of the repository. repo_name (str): The name of the repository. pr_number (int): The pull request number to merge. commit_title (str, optional): The title for the merge commit. Defaults to None. commit_message (str, optional): The message for the merge commit. Defaults to None. merge_method (Literal['merge', 'squash', 'rebase'], optional): The merge method to use ('merge', 'squash', or 'rebase'). Defaults to 'squash'. Returns: Dict[str, Any]: The JSON response from the GitHub API containing merge information if successful. Error Handling: Logs errors and prints the traceback if the merge fails, returning None. """ logger.info(f"Merging PR {repo_owner}/{repo_name}#{pr_number}") # Construct the merge URL merge_url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/pulls/{pr_number}/merge" try: payload: dict[str, Any] = {"merge_method": merge_method} if commit_title is not None: payload["commit_title"] = commit_title if commit_message is not None: payload["commit_message"] = commit_message response = httpx.put( merge_url, headers=self._get_headers(), json=payload, timeout=TIMEOUT, ) if not response.is_success: self._handle_response_error( response, f"PR #{pr_number} merge in {repo_owner}/{repo_name}", ) merge_data = response.json() logger.info("PR merged successfully") return merge_data except GitHubAPIError as e: if isinstance(e, GitHubAuthError): detail = e.message else: github_msg = (e.response_body or {}).get("message", "") if e.response_body else "" detail = github_msg or e.message logger.error(f"Error merging PR: {detail}") return {"status": "error", "message": detail, "details": e.response_body} except httpx.HTTPError as e: logger.error(f"Error merging PR: {str(e)}") traceback.print_exc() return {"status": "error", "message": str(e)} - src/mcp_github/issues_pr_analyser.py:137-148 (registration)Dynamic registration of all public methods of GitHubIntegration as MCP tools via introspection. register_tools iterates over the object's public methods (including merge_pr) and calls self.mcp.add_tool(method) on each.
def _register_tools(self): self.register_tools(self.gi) self.register_tools(self.ip) self.mcp.add_provider(SkillsDirectoryProvider(Path(__file__).parent / "skills")) def register_tools(self, methods: Any = None) -> None: for name in dir(methods): if name.startswith("_"): continue method = getattr(methods, name) if inspect.isroutine(method): self.mcp.add_tool(method)