| import requests |
| from functools import lru_cache |
| from packaging.version import parse |
| import os |
|
|
| TOKEN = os.getenv("GITHUB_TOKEN", None) |
| HEADERS = {"Authorization": f"token {TOKEN}"} if TOKEN else {} |
|
|
|
|
| @lru_cache() |
| def fetch_all_branches(user, repo): |
| branches = [] |
| page = 1 |
| while True: |
| |
| response = requests.get( |
| f"https://api.github.com/repos/{user}/{repo}/branches", params={"page": page}, headers=HEADERS |
| ) |
|
|
| |
| if response.status_code == 200: |
| |
| branches.extend([branch["name"] for branch in response.json()]) |
|
|
| |
| if "next" in response.links: |
| page += 1 |
| else: |
| break |
| else: |
| print("Failed to retrieve branches:", response.status_code) |
| break |
|
|
| return branches |
|
|
|
|
| def fetch_github_info(repo_url): |
| |
| repo_parts = repo_url.rstrip("/").split("/") |
| owner, repo = repo_parts[-2], repo_parts[-1] |
| print(f"{owner=}, {repo=}") |
|
|
| |
| api_url = f"https://api.github.com/repos/{owner}/{repo}" |
|
|
| try: |
| |
| tags_response = requests.get(f"{api_url}/tags", headers=HEADERS) |
| tags_response.raise_for_status() |
| tags = tags_response.json() |
|
|
| if len(tags) < 2: |
| raise ValueError("Not enough tags to fetch the second last tag.") |
|
|
| second_last_tag = tags[1]["name"] |
|
|
| |
| branches = fetch_all_branches(user=owner, repo=repo) |
| if not branches: |
| raise ValueError("No branches found.") |
|
|
| filtered_branches = [] |
| for branch in branches: |
| if branch.startswith("v") and ("-release" in branch or "-patch" in branch): |
| filtered_branches.append(branch) |
| if not filtered_branches: |
| raise ValueError("No release branches found.") |
|
|
| sorted_branches = sorted(filtered_branches, key=lambda x: parse(x.split("-")[0][1:]), reverse=True) |
| latest_release_branch = sorted_branches[0] |
|
|
| return {"second_last_tag": second_last_tag, "latest_release_branch": latest_release_branch} |
|
|
| except requests.exceptions.RequestException as e: |
| print(f"Error fetching data from GitHub: {e}") |
| return None |
| except ValueError as e: |
| print(e) |
| return None |
|
|
|
|
| |
| if __name__ == "__main__": |
| repo_url = "https://github.com/huggingface/diffusers" |
| info = fetch_github_info(repo_url) |
|
|
| if info: |
| print("Second Last Tag:", info["second_last_tag"]) |
| print("Latest Release Branch:", info["latest_release_branch"]) |
|
|