Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add organization support to repos command #76

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions github_to_sqlite/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
6 changes: 5 additions & 1 deletion github_to_sqlite/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down