Last active
July 8, 2021 21:19
-
-
Save dangunter/8567232e24257ca1919c62e040e91db2 to your computer and use it in GitHub Desktop.
Get contributors to a Github repo using the Github API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from typing import List, Dict, Union | |
def get_contributors(owner: str, repo: str, extra_fields: List[str] = None, | |
simplify=True) -> Union[List[str], List[Dict[str, str]]]: | |
"""Get a list of contributors to a Github repo. | |
Args: | |
owner: Repository owner (organization) | |
repo: Repository name | |
extra_fields: List of fields besides login name to fetch from Github API result | |
simplify: If True, and no extra fields, just return a list of strings | |
Returns: | |
A list of dictionaries with field name and value for each contributor, or | |
for 'simplify' mode, a list of login names (strings) for the contributors. | |
""" | |
url = f"https://api.github.com/repos/{owner}/{repo}/contributors" | |
resp = requests.get(url) | |
result = [] | |
fields = ("login",) if extra_fields is None else ["login"] + list(extra_fields) | |
for item in resp.json(): | |
if extra_fields is None and simplify: | |
result.append(item.get(fields[0], "-")) | |
else: | |
result.append({f: item.get(f, "-") for f in fields}) | |
return result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment