From bb959b46e8a7647755c14dee180fdd5209451954 Mon Sep 17 00:00:00 2001 From: Jb DOYON Date: Tue, 6 Sep 2022 14:16:25 +0100 Subject: [PATCH] Add organization support to repos command New --organization flag to signify all given "usernames" are private orgs. Adapts API URL to the organization path instead. Not the best implementation, but a first draft. --- github_to_sqlite/cli.py | 9 +++++++-- github_to_sqlite/utils.py | 6 +++++- 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/github_to_sqlite/cli.py b/github_to_sqlite/cli.py index 70aa3b5..dd44a08 100644 --- a/github_to_sqlite/cli.py +++ b/github_to_sqlite/cli.py @@ -243,7 +243,12 @@ def stargazers(db_path, repos, auth): is_flag=True, help="Fetch HTML rendered README into 'readme_html' column", ) -def repos(db_path, usernames, auth, repo, load, readme, readme_html): +@click.option( + "--organization", + is_flag=True, + help="The given users are organizations", +) +def repos(db_path, usernames, auth, repo, load, readme, readme_html, organization): "Save repos owned by the specified (or authenticated) username or organization" db = sqlite_utils.Database(db_path) token = load_token(auth) @@ -260,7 +265,7 @@ def repos(db_path, usernames, auth, repo, load, readme, readme_html): if not usernames: usernames = [None] for username in usernames: - for repo in utils.fetch_all_repos(username, token): + for repo in utils.fetch_all_repos(username, token, organization): repo_id = utils.save_repo(db, repo) _repo_readme( db, token, repo_id, repo["full_name"], readme, readme_html diff --git a/github_to_sqlite/utils.py b/github_to_sqlite/utils.py index c7d78d7..9da8811 100644 --- a/github_to_sqlite/utils.py +++ b/github_to_sqlite/utils.py @@ -444,15 +444,19 @@ def fetch_stargazers(repo, token=None): yield from stargazers -def fetch_all_repos(username=None, token=None): +def fetch_all_repos(username=None, token=None, organization=False): assert username or token, "Must provide username= or token= or both" headers = make_headers(token) # Get topics for each repo: headers["Accept"] = "application/vnd.github.mercy-preview+json" if username: url = "https://api.github.com/users/{}/repos".format(username) + if organization: + url = "https://api.github.com/orgs/{}/repos".format(username) else: url = "https://api.github.com/user/repos" + if organization: + url = "https://api.github.com/orgs/{}/repos".format(username) for repos in paginate(url, headers): yield from repos