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 = [] # List to store all branches page = 1 # Start from first page while True: # Make a request to the GitHub API for the branches response = requests.get( f"https://api.github.com/repos/{user}/{repo}/branches", params={"page": page}, headers=HEADERS ) # Check if the request was successful if response.status_code == 200: # Add the branches from the current page to the list branches.extend([branch["name"] for branch in response.json()]) # Check if there is a 'next' link for pagination if "next" in response.links: page += 1 # Move to the next page else: break # Exit loop if there is no next page else: print("Failed to retrieve branches:", response.status_code) break return branches def fetch_github_info(repo_url): # Extract owner and repo name from the URL repo_parts = repo_url.rstrip("/").split("/") owner, repo = repo_parts[-2], repo_parts[-1] print(f"{owner=}, {repo=}") # GitHub API base URL api_url = f"https://api.github.com/repos/{owner}/{repo}" try: # Fetch tags 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"] # Fetch branches 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 # Example usage 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"])