Skip to content

Instantly share code, notes, and snippets.

@malfet
Created January 19, 2022 17:32
Show Gist options
  • Save malfet/aefe35f9f644805af476ec73cc64503b to your computer and use it in GitHub Desktop.
Save malfet/aefe35f9f644805af476ec73cc64503b to your computer and use it in GitHub Desktop.
Print default branches for public repos in pytorch org
#!/usr/bin/env python3
# Print default branch for public repos in pytorch org
# Requires github token
from typing import Any, Dict
GH_SEARCH_REPO_REQ = """
query($cursor: String) {
search(type: REPOSITORY, query: "org:pytorch", first: 100, after: $cursor) {
nodes {
... on Repository {
name
visibility
defaultBranchRef {
name
}
}
}
pageInfo {
endCursor,
hasNextPage
}
}
}
"""
def gh_graphql(query: str, **kwargs: Any) -> Dict[str, Any]:
from urllib.request import urlopen, Request
import json
import os
url = "https://api.github.com/graphql"
data = json.dumps({"query": query, "variables": kwargs}).encode()
headers={'Authorization': f'token {os.getenv("GITHUB_TOKEN")}'}
req = Request(url, headers=headers, data=data)
with urlopen(req) as conn:
rc = json.load(conn)
if "errors" in rc:
raise RuntimeError(f"GraphQL query {query} failed: {rc['errors']}")
return rc
if __name__ == "__main__":
has_next_page = True
cursor = None
while has_next_page:
rc = gh_graphql(GH_SEARCH_REPO_REQ, cursor=cursor)["data"]["search"]
for repo in rc["nodes"]:
if repo["visibility"] != "PUBLIC":
continue
print(repo["name"], repo["defaultBranchRef"]["name"])
cursor = rc["pageInfo"]["endCursor"]
has_next_page = rc["pageInfo"]["hasNextPage"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment