Download src/hf_release/core/github_client.py from MikeChandler/hf-release: direct link, hf CLI and curl.
- Browser
- Download file 4.66 kB
-
https://huggingface.co/datasets/MikeChandler/hf-release/resolve/main/src/hf_release/core/github_client.py
- Command line
-
hf download hf://datasets/MikeChandler/hf-release/src/hf_release/core/github_client.py
-
curl -L -o github_client.py https://huggingface.co/datasets/MikeChandler/hf-release/resolve/main/src/hf_release/core/github_client.py
4.66 kB
| """GitHub operations via PyGithub.""" | |
| from __future__ import annotations | |
| import github | |
| from github import Github, GithubException | |
| from github.Repository import Repository | |
| from ..utils.exceptions import AuthenticationError, ReleaseCreationError, RepoNotFoundError, TagConflictError | |
| class GHClient: | |
| def __init__(self, token: str) -> None: | |
| self._gh = Github(token) | |
| def get_repo(self, repo_id: str) -> Repository: | |
| try: | |
| return self._gh.get_repo(repo_id) | |
| except GithubException as exc: | |
| if exc.status == 401: | |
| raise AuthenticationError( | |
| "GitHub token rejected.", | |
| suggestion="Check that GITHUB_TOKEN has 'repo' scope.", | |
| ) from exc | |
| if exc.status == 404: | |
| raise RepoNotFoundError( | |
| f"GitHub repo '{repo_id}' not found.", | |
| suggestion="Check the repo name and token permissions.", | |
| ) from exc | |
| raise | |
| # ------------------------------------------------------------------ | |
| # Tagging | |
| # ------------------------------------------------------------------ | |
| def ensure_tag(self, repo: Repository, tag_name: str, message: str | None = None) -> None: | |
| """Create an annotated tag pointing to HEAD. Idempotent if same SHA.""" | |
| try: | |
| head_sha = repo.get_branch(repo.default_branch).commit.sha | |
| tag_obj = repo.create_git_tag( | |
| tag=tag_name, | |
| message=message or tag_name, | |
| object=head_sha, | |
| type="commit", | |
| ) | |
| repo.create_git_ref(ref=f"refs/tags/{tag_name}", sha=tag_obj.sha) | |
| except GithubException as exc: | |
| if exc.status == 422: | |
| # Already exists — check if it points to the same commit | |
| try: | |
| existing = repo.get_git_ref(f"tags/{tag_name}") | |
| # If it exists and we can read it, treat as idempotent success | |
| _ = existing | |
| return | |
| except GithubException: | |
| pass | |
| raise TagConflictError( | |
| f"GitHub tag '{tag_name}' already exists.", | |
| suggestion="Use a different version or delete the existing tag first.", | |
| ) from exc | |
| raise TagConflictError(f"Failed to create GitHub tag '{tag_name}': {exc}") from exc | |
| def delete_tag(self, repo: Repository, tag_name: str) -> None: | |
| try: | |
| ref = repo.get_git_ref(f"tags/{tag_name}") | |
| ref.delete() | |
| except GithubException as exc: | |
| if exc.status == 404: | |
| return # Already gone | |
| raise | |
| # ------------------------------------------------------------------ | |
| # Releases | |
| # ------------------------------------------------------------------ | |
| def _get_previous_tag(self, repo: Repository) -> str | None: | |
| try: | |
| return repo.get_latest_release().tag_name | |
| except GithubException: | |
| return None | |
| def generate_notes( | |
| self, | |
| repo: Repository, | |
| tag_name: str, | |
| previous_tag: str | None = None, | |
| target_commitish: str | None = None, | |
| ) -> str: | |
| """Call GitHub's native release-notes generation endpoint.""" | |
| try: | |
| prev = previous_tag or self._get_previous_tag(repo) | |
| kwargs: dict = {"tag_name": tag_name} | |
| if prev: | |
| kwargs["previous_tag_name"] = prev | |
| if target_commitish: | |
| kwargs["target_commitish"] = target_commitish | |
| notes = repo.generate_release_notes(**kwargs) | |
| return notes.body | |
| except Exception: | |
| return "" | |
| def create_release( | |
| self, | |
| repo: Repository, | |
| tag_name: str, | |
| name: str | None = None, | |
| body: str | None = None, | |
| draft: bool = False, | |
| prerelease: bool = False, | |
| ) -> str: | |
| """Create a GitHub release. Returns the HTML URL.""" | |
| try: | |
| release = repo.create_git_release( | |
| tag=tag_name, | |
| name=name or tag_name, | |
| message=body or "", | |
| draft=draft, | |
| prerelease=prerelease, | |
| ) | |
| return release.html_url | |
| except GithubException as exc: | |
| raise ReleaseCreationError( | |
| f"Failed to create GitHub release: {exc.data.get('message', exc)}", | |
| suggestion="Check that the tag exists and the token has 'repo' write access.", | |
| ) from exc | |