File size: 2,974 Bytes
f30c6d8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
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"])