From bf1342a3fd92584a3c99700a149138ced80b92bd Mon Sep 17 00:00:00 2001 From: Ryan Scott Brown Date: Tue, 2 Jan 2024 11:29:43 -0500 Subject: [PATCH] Better support private repos for organizations Using `https://api.github.com/users/{SOMEONE}/repos` endpoint does not seem to return private repos for an organization. In this PR, I added a `--org` option to match the one on the pull-requests command and allow collecting private repos. --- github_to_sqlite/cli.py | 28 ++++++++++++++++++++-------- 1 file changed, 20 insertions(+), 8 deletions(-) diff --git a/github_to_sqlite/cli.py b/github_to_sqlite/cli.py index e6a2d88..1ec29ef 100644 --- a/github_to_sqlite/cli.py +++ b/github_to_sqlite/cli.py @@ -268,6 +268,12 @@ def stargazers(db_path, repos, auth): type=click.Path(file_okay=True, dir_okay=False, allow_dash=True, exists=True), help="Load repos JSON from this file instead of the API", ) +@click.option( + "--org", + "orgs", + help="Fetch all pull requests from this GitHub organization", + multiple=True, +) @click.option( "--readme", is_flag=True, @@ -278,7 +284,7 @@ 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): +def repos(db_path, usernames, auth, repo, load, orgs, readme, readme_html): "Save repos owned by the specified (or authenticated) username or organization" db = sqlite_utils.Database(db_path) token = load_token(auth) @@ -292,14 +298,20 @@ def repos(db_path, usernames, auth, repo, load, readme, readme_html): repo_id = utils.save_repo(db, utils.fetch_repo(full_name, token)) _repo_readme(db, token, repo_id, full_name, readme, readme_html) else: - if not usernames: + repo_lists = [] + if orgs: + repo_lists.extend(utils.fetch_all_repos(None, token, org) for org in orgs) + elif not usernames: + # if an org was specified, don't fetch the current user's repos usernames = [None] - for username in usernames: - for repo in utils.fetch_all_repos(username, token): - repo_id = utils.save_repo(db, repo) - _repo_readme( - db, token, repo_id, repo["full_name"], readme, readme_html - ) + + repo_lists.extend(utils.fetch_all_repos(username, token) for username in usernames) + + for repo in itertools.chain(*repo_lists): + repo_id = utils.save_repo(db, repo) + _repo_readme( + db, token, repo_id, repo["full_name"], readme, readme_html + ) utils.ensure_db_shape(db)