Skip to main content
Glama

merge_pr

Merge GitHub pull requests using specified methods (merge, squash, or rebase) with customizable commit messages for repository integration.

Instructions

Merges a specific pull request in a GitHub repository using the specified merge method. 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

TableJSON Schema
NameRequiredDescriptionDefault
repo_ownerYes
repo_nameYes
pr_numberYes
commit_titleNo
commit_messageNo
merge_methodNosquash

Implementation Reference

  • The `merge_pr` method in the `GitHubIntegration` class implements the core logic for merging a GitHub pull request. It constructs the API URL, sends a PUT request with merge parameters, handles the response, and returns success or error details.
    def merge_pr(self, repo_owner: str, repo_name: str, pr_number: int, commit_title: Optional[str] = None, commit_message: Optional[str] = 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. 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. """ logging.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: response = requests.put(merge_url, headers=self._get_headers(), json={ 'commit_title': commit_title, 'commit_message': commit_message, 'merge_method': merge_method }, timeout=TIMEOUT) response.raise_for_status() merge_data = response.json() logging.info("PR merged successfully") return merge_data except Exception as e: logging.error({"status": "error", "message": str(e)}) traceback.print_exc() return {"status": "error", "message": str(e)}
  • The `register_tools` method dynamically registers all non-private methods of the `GitHubIntegration` instance (including `merge_pr`) as MCP tools by calling `self.mcp.add_tool(method)` in a loop over inspect.getmembers.
    def register_tools(self, methods: Any = None) -> None: for name, method in inspect.getmembers(methods): if (inspect.isfunction(method) or inspect.ismethod(method)) and not name.startswith("_"): self.mcp.add_tool(method)
  • The `_register_tools` method calls `register_tools` on the `GitHubIntegration` instance (`self.gi`), which registers the `merge_pr` tool.
    self.register_tools(self.gi) self.register_tools(self.ip)
  • Instantiation of the `GitHubIntegration` class instance (`self.gi = GI()`), providing the `merge_pr` handler to the MCP server.
    self.gi = GI()

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/saidsef/mcp-github-pr-issue-analyser'

If you have feedback or need assistance with the MCP directory API, please join our Discord server