Last active
December 23, 2024 15:50
-
-
Save tsvikas/95a38de41c36f8243c31f56f18e6001a to your computer and use it in GitHub Desktop.
check if a user is a contributor to a github repo
This file contains hidden or 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 argparse | |
import requests | |
def check_contributors(repo, username, token): | |
_owner, _repo_name = repo.split("/") | |
base_url = ( | |
"https://api.github.com/repos/{}/contributors?anon=1&per_page=100&page={}" | |
) | |
headers = { | |
"Accept": "application/vnd.github+json", | |
"X-GitHub-Api-Version": "2022-11-28", | |
} | |
if token: | |
headers["Authorization"] = f"token {token}" | |
max_pages = 30 | |
found = False | |
for page in range(1, max_pages + 1): | |
url = base_url.format(repo, page) | |
response = requests.get(url, headers=headers) | |
if response.status_code != 200: | |
print( | |
f"Error: Unable to fetch data for page {page}, status code {response.status_code}" | |
) | |
return | |
contributors = response.json() | |
for contributor in contributors: | |
if ( | |
contributor.get("login") == username | |
or contributor.get("email") == username | |
or contributor.get("email", "").split("@")[0] == username | |
): | |
print(f"{page=}", contributor) | |
found = True | |
break | |
if found: | |
break | |
if not found: | |
print(f"User {username} not found") | |
if __name__ == "__main__": | |
parser = argparse.ArgumentParser( | |
description="Check if a GitHub user is a contributor to a repository" | |
) | |
parser.add_argument( | |
"repo", type=str, help="GitHub repository in the format 'owner/repo'" | |
) | |
parser.add_argument("username", type=str, help="GitHub username to check for") | |
parser.add_argument( | |
"--token", type=str, help="GitHub Personal Access Token (PAT)", default=None | |
) | |
args = parser.parse_args() | |
check_contributors(args.repo, args.username, args.token) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment