Add github_tags auto method (#427)

Add a new method to fetch tags with their related commit date using GitHub GraphQL API.

For large repositories, such as https://github.com/gregkh/linux, it may be more performant than the git auto method.
This commit is contained in:
Marc Wrobel
2025-04-05 16:40:29 +02:00
committed by GitHub
parent bb86bc17b8
commit e56b642326
2 changed files with 67 additions and 0 deletions

View File

@@ -11,6 +11,12 @@ class Release:
self.is_prerelease: bool = is_prerelease
class Tag:
def __init__(self, name: str, commit_at: str) -> None:
self.name: str = name
self.commit_date: str = commit_at
def fetch_releases(repo_id: str) -> list[Release]:
logging.info(f"fetching {repo_id} GitHub releases")
(owner, repo) = repo_id.split('/')
@@ -48,3 +54,41 @@ query($endCursor: String) {
release_data['isPrerelease']))
return releases
def fetch_tags(repo_id: str) -> list[Tag]:
logging.info(f"fetching {repo_id} tags")
(owner, repo) = repo_id.split('/')
child = subprocess.run("""gh api graphql --paginate -f query='
query($endCursor: String) {
repository(name: "%s", owner: "%s") {
refs(refPrefix: "refs/tags/", first: 100, after: $endCursor) {
pageInfo { hasNextPage, endCursor }
edges {
node {
name
target {
... on Tag {
tagger {
date
}
}
}
}
}
}
}
}'""" % (repo, owner), capture_output=True, timeout=300, check=True, shell=True) # noqa: UP031
logging.info(f"fetched {repo_id} tags")
responses = child.stdout.decode("utf-8").strip().replace('}{', '}\n{').split("\n")
pages = [json.loads(response) for response in responses]
tags = []
for page in pages:
for edge in page['data']['repository']['refs']['edges']:
tag_name = edge['node']['name']
tags_date = edge['node']['target'].get('tagger', {}).get('date', None)
if tags_date:
tags.append(Tag(tag_name, tags_date))
return tags