update_pr_branch
Update a pull request branch by merging the base branch into it, ensuring it includes the latest upstream changes.
Instructions
Updates the pull request branch with the latest upstream changes by merging the base branch into the PR branch.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| repo_owner | Yes | The owner of the repository. | |
| repo_name | Yes | The name of the repository. | |
| pr_number | Yes | The pull request number. | |
| expected_head_sha | No | Optional SHA of the PR branch head. If provided, GitHub will only update the branch if the current head SHA matches this value. |
Output Schema
| Name | Required | Description | Default |
|---|---|---|---|
No arguments | |||
Implementation Reference
- The main handler for 'update_pr_branch'. This method updates a pull request branch with the latest upstream changes by calling the GitHub API's 'pulls/{pr_number}/update-branch' endpoint via an HTTP PUT request. It accepts repo_owner, repo_name, pr_number, and an optional expected_head_sha parameter.
def update_pr_branch( self, repo_owner: str, repo_name: str, pr_number: int, expected_head_sha: str | None = None, ) -> dict[str, Any]: """ Updates the pull request branch with the latest upstream changes by merging the base branch into the PR branch. Args: repo_owner: The owner of the repository. repo_name: The name of the repository. pr_number: The pull request number. expected_head_sha: Optional SHA of the PR branch head. If provided, GitHub will only update the branch if the current head SHA matches this value. Returns: dict[str, Any]: The GitHub API response for the branch update request. Raises: GitHubAuthError: If authentication fails. GitHubAPIError: If the request fails. """ logger.info(f"Updating PR branch for {repo_owner}/{repo_name}#{pr_number}") pr_url = ( f"https://api.github.com/repos/{repo_owner}/{repo_name}/pulls/{pr_number}/update-branch" ) try: payload: dict[str, Any] = {} if expected_head_sha is not None: payload["expected_head_sha"] = expected_head_sha response = httpx.put( pr_url, headers=self._get_headers(), json=payload, timeout=TIMEOUT, ) self._raise_for_status(response, f"PR #{pr_number} update branch") logger.info("PR branch update requested successfully") return response.json() except GitHubAuthError: raise except Exception as e: logger.error(f"Error updating PR branch: {str(e)}") traceback.print_exc() return {"status": "error", "message": str(e)} - src/mcp_github/issues_pr_analyser.py:137-148 (registration)Registration of tools from GitHubIntegration (which includes update_pr_branch). The register_tools method iterates over all public methods of the GitHubIntegration instance using dir() and adds them as MCP tools via self.mcp.add_tool(method). This is how update_pr_branch gets registered as an MCP tool.
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) - The merge_pr method references update_pr_branch in its docstring: 'If merge pr fails use update_pr_branch to update the branch with the latest changes from the base branch and try merging again after CI finishes.' This is the contextual reference that ties the two tools together.
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)}