create_tag
Create version tags in GitHub repositories to mark releases, milestones, or specific commits with descriptive messages.
Instructions
Creates a new tag in the specified GitHub repository. Args: repo_owner (str): The owner of the repository. repo_name (str): The name of the repository. tag_name (str): The name of the tag to create. message (str): The message associated with the tag. Returns: Dict[str, Any]: The response data from the GitHub API if the tag is created successfully. None: If an error occurs during the tag creation process. Error Handling: Logs errors and prints the traceback if fetching the latest commit SHA fails or if the GitHub API request fails.
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| repo_owner | Yes | ||
| repo_name | Yes | ||
| tag_name | Yes | ||
| message | Yes |
Implementation Reference
- The core handler function implementing the 'create_tag' tool logic. It fetches the latest commit SHA and creates a lightweight GitHub tag using the GitHub Refs API.def create_tag(self, repo_owner: str, repo_name: str, tag_name: str, message: str) -> Dict[str, Any]: """ Creates a new tag in the specified GitHub repository. Args: repo_owner (str): The owner of the repository. repo_name (str): The name of the repository. tag_name (str): The name of the tag to create. message (str): The message associated with the tag. Returns: Dict[str, Any]: The response data from the GitHub API if the tag is created successfully. None: If an error occurs during the tag creation process. Error Handling: Logs errors and prints the traceback if fetching the latest commit SHA fails or if the GitHub API request fails. """ logging.info(f"Creating tag {tag_name} in {repo_owner}/{repo_name}") # Construct the tags URL tags_url = f"https://api.github.com/repos/{repo_owner}/{repo_name}/git/refs" try: # Fetch the latest commit SHA latest_sha = self.get_latest_sha(repo_owner, repo_name) if not latest_sha: raise ValueError("Failed to fetch the latest commit SHA") # Create the tag response = requests.post(tags_url, headers=self._get_headers(), json={ 'ref': f'refs/tags/{tag_name}', 'sha': latest_sha, 'message': message }, timeout=TIMEOUT) response.raise_for_status() tag_data = response.json() logging.info("Tag created successfully") return tag_data except Exception as e: logging.error(f"Error creating tag: {str(e)}") traceback.print_exc() return {"status": "error", "message": str(e)}
- src/mcp_github/issues_pr_analyser.py:109-113 (registration)The registration logic that dynamically adds all public methods from the GitHubIntegration instance (including 'create_tag') to the MCP server as tools.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)
- src/mcp_github/issues_pr_analyser.py:106-107 (registration)Calls the register_tools method on the GitHubIntegration instance to register 'create_tag' and other tools.self.register_tools(self.gi) self.register_tools(self.ip)
- Helper method called by create_tag to retrieve the SHA of the latest commit for tagging.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)