Ben Chuanlong Du's Blog

It is never too late to learn.

Querying Branches of GitHub Repositories Using Rest API

Things on this page are fragmentary and immature notes/thoughts of the author. Please read with your own judgement!

In [ ]:
import requests

token = "ghp_xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"


def get_repos(token: str) -> requests.Response:
    resp = requests.get(
        url="https://api.github.com/orgs/legendu-net/repos?per_page=500",
        headers={
            "Accept": "application/vnd.github.v3+json",
            "Authorization": f"token {token}",
        },
    )
    return [repo["name"] for repo in resp.json()]


def get_branches(repo: str, token: str) -> requests.Response:
    headers = {
        "Accept": "application/vnd.github+json",
        "Authorization": f"Bearer {token}",
        "X-GitHub-Api-Version": "2022-11-28",
    }
    resp = requests.get(
        f"https://api.github.com/repos/legendu-net/{repo}/branches", headers=headers
    )
    return [branch["name"] for branch in resp.json()]
In [2]:
repo_branches = {repo: get_branches(repo, token) for repo in get_repos(token)}
repo_branches
Out[2]:
{'docker-rstudio': ['main', 'text_mining'],
 'docker-ubuntu_cn': ['dev', 'main'],
 'docker-jupyterlab': ['blog', 'dev', 'main'],
 'docker-jupyterhub-rb': ['dev', 'main'],
 'docker-r-base': ['dev', 'main'],
 'docker-r-pop': ['main', 'text_mining'],
 'docker-jupyterhub-rp': ['dev', 'main'],
 'docker-jupyterhub-toree': ['main'],
 'docker-python': ['dev', 'main'],
 'docker-jdk': ['dev', 'main'],
 'docker-scala': ['main'],
 'docker-dryscrape': ['main'],
 'docker-nodejs': ['dev', 'main'],
 'docker-typescript': ['main'],
 'docker-jupyterhub-ts': ['dev', 'main'],
 'docker-jupyterhub-ds': ['dev', 'main'],
 'docker-base': ['centos7', 'debian', 'dev', 'main'],
 'docker-xubuntu-py': ['dev', 'main'],
 'docker-samba': ['dev', 'main'],
 'docker-conda': ['dev', 'main'],
 'docker-conda-yarn': ['main'],
 'docker-jupyter': ['dev', 'main'],
 'docker-jupyterhub': ['dev', 'main'],
 'docker-jupyterhub-jdk': ['dev', 'main'],
 'docker-jupyterhub-antlr4': ['main'],
 'docker-python-jdk': ['dev', 'main'],
 'docker-jupyterhub-beakerx': ['main'],
 'docker-nfs': ['dev', 'main'],
 'docker-conda-build': ['dev', 'main'],
 'docker-mlflow': ['main'],
 'docker-python-nodejs': ['dev', 'main'],
 'docker-jupyterhub-sagemath': ['dev', 'main'],
 'docker-lubuntu': ['dev', 'main'],
 'docker-lubuntu-jdk': ['dev', 'main'],
 'docker-jupyterhub-julia': ['dev', 'main'],
 'docker-jupyterhub-almond': ['dev', 'main'],
 'docker-jupyterlab-quickopen': ['main'],
 'blog': ['dependabot/npm_and_yarn/themes/elegant/decode-uri-component-0.2.2',
  'dependabot/npm_and_yarn/themes/elegant/minimatch-3.1.2',
  'dependabot/npm_and_yarn/themes/elegant/socket.io-parser-3.3.3',
  'dependabot/npm_and_yarn/themes/elegant/terser-5.14.2',
  'master'],
 'xinstall': ['dev', 'icon', 'main'],
 'docker-deepin': ['main'],
 'docker-deepin_b': ['dev', 'main'],
 'docker-deepin_cn': ['main'],
 'docker-ldeepin': ['main'],
 'docker-lubuntu-pyside2': ['main'],
 'docker-jupyterhub-selenium-firefox': ['main'],
 'docker-jupyterhub-selenium-chrome': ['main'],
 'aiutil': ['dependabot/pip/certifi-2022.12.7',
  'dependabot/pip/pillow-9.3.0',
  'dev',
  'dulwich',
  'main'],
 'docker-vscode-server': ['dev', 'main'],
 'docker-gitpod': ['blog', 'dev', 'main'],
 'docker-jupyterhub-cuda': ['cuda11', 'dev', 'main'],
 'docker-jupyterhub-cuda-dev': ['cuda11', 'dev', 'main'],
 'docker-jupyterhub-pytorch': ['cuda11', 'dev', 'main'],
 'docker-jupyterhub-more': ['dev', 'main'],
 'docker-rustpython': ['dev', 'main'],
 'docker-rust': ['centos7', 'dev', 'main'],
 'docker-python-portable': ['3.7.9', '3.8.6', 'dev', 'main'],
 'python-portable': ['main'],
 'schema': ['main'],
 'docker_image_builder': ['dev', 'main'],
 'docker-pypy': ['dev', 'main'],
 'arrow_fat_jar': ['main'],
 'docker-rust-utils': ['dev', 'main'],
 'docker-tensorboard': ['dev', 'main'],
 'docker-evcxr_jupyter': ['dev', 'main'],
 'pysparker': ['dependabot/pip/pyspark-3.2.2', 'dev', 'main'],
 'dockeree': ['dependabot/pip/certifi-2022.12.7', 'dev', 'main'],
 'docker-jupyterhub-go': ['dev', 'main'],
 'docker-jupyterhub-kotlin': ['dev', 'main'],
 'docker-gophernotes': ['dev', 'main'],
 'docker-golang': ['dev', 'main'],
 'icon': ['dev', 'main', 'refactor'],
 'arrowdb': ['dev', 'main'],
 'docker-jupyterhub-ganymede': ['dev', 'main']}
In [3]:
for repo, branches in repo_branches.items():
    if len(branches) > 2:
        print(repo + ":", branches)
docker-jupyterlab: ['blog', 'dev', 'main']
docker-base: ['centos7', 'debian', 'dev', 'main']
blog: ['dependabot/npm_and_yarn/themes/elegant/decode-uri-component-0.2.2', 'dependabot/npm_and_yarn/themes/elegant/minimatch-3.1.2', 'dependabot/npm_and_yarn/themes/elegant/socket.io-parser-3.3.3', 'dependabot/npm_and_yarn/themes/elegant/terser-5.14.2', 'master']
xinstall: ['dev', 'icon', 'main']
aiutil: ['dependabot/pip/certifi-2022.12.7', 'dependabot/pip/pillow-9.3.0', 'dev', 'dulwich', 'main']
docker-gitpod: ['blog', 'dev', 'main']
docker-jupyterhub-cuda: ['cuda11', 'dev', 'main']
docker-jupyterhub-cuda-dev: ['cuda11', 'dev', 'main']
docker-jupyterhub-pytorch: ['cuda11', 'dev', 'main']
docker-rust: ['centos7', 'dev', 'main']
docker-python-portable: ['3.7.9', '3.8.6', 'dev', 'main']
pysparker: ['dependabot/pip/pyspark-3.2.2', 'dev', 'main']
dockeree: ['dependabot/pip/certifi-2022.12.7', 'dev', 'main']
icon: ['dev', 'main', 'refactor']

Comments