get_latest_sha
Fetch the SHA of the most recent commit in a GitHub repository to identify current code state for analysis or automation workflows.
Instructions
Fetches the SHA of the latest commit in the specified GitHub repository. Args: repo_owner (str): The owner of the GitHub repository. repo_name (str): The name of the GitHub repository. Returns: Optional[str]: The SHA string of the latest commit if found, otherwise None. Error Handling: Logs errors and warnings if the request fails, the response is invalid, or no commits are found. Returns None in case of exceptions or if the repository has no commits.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| repo_owner | Yes | ||
| repo_name | Yes |
Implementation Reference
- The core handler function for the 'get_latest_sha' tool. It queries the GitHub API to retrieve the SHA of the most recent commit in the specified repository's default branch.def get_latest_sha(self, repo_owner: str, repo_name: str) -> Optional[str]: """ Fetches the SHA of the latest commit in the specified GitHub repository. Args: repo_owner (str): The owner of the GitHub repository. repo_name (str): The name of the GitHub repository. Returns: Optional[str]: The SHA string of the latest commit if found, otherwise None. Error Handling: Logs errors and warnings if the request fails, the response is invalid, or no commits are found. Returns None in case of exceptions or if the repository has no commits. """ logging.info({"status": "info", "message": f"Fetching latest commit SHA for {repo_owner}/{repo_name}"}) # Construct the commits URL commits_url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/commits" try: # Fetch the latest commit response = requests.get(commits_url, headers=self._get_headers(), timeout=TIMEOUT) response.raise_for_status() commits_data = response.json() if commits_data: latest_sha = commits_data[0]['sha'] logging.info({"status": "info", "message": f"Latest commit SHA: {latest_sha}"}) return latest_sha else: logging.warning({"status": "warning", "message": "No commits found in the repository"}) return "No commits found in the repository" except Exception as e: logging.error(f"Error fetching latest commit SHA: {str(e)}") traceback.print_exc() return str(e)
- src/mcp_github/issues_pr_analyser.py:105-114 (registration)Registers all public methods (non-private, i.e., not starting with '_') from the GitHubIntegration instance as MCP tools, including 'get_latest_sha'.def _register_tools(self): self.register_tools(self.gi) self.register_tools(self.ip) 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) def run(self):
- Usage of get_latest_sha as a helper function within the create_tag method to obtain the latest commit SHA before creating a tag.# Fetch the latest commit SHA latest_sha = self.get_latest_sha(repo_owner, repo_name) if not latest_sha:
- src/mcp_github/issues_pr_analyser.py:67-68 (registration)Instantiation of the GitHubIntegration class instance (self.gi) whose methods, including get_latest_sha, are later registered as tools.self.gi = GI() self.ip = IP()