Skip to content

Commit eddbe61

Browse files
authored
Merge pull request #7 from shllwrld/dev
fixed bug when count repos for --nickname invalid
2 parents 3abb5f7 + 761b254 commit eddbe61

File tree

1 file changed

+30
-6
lines changed

1 file changed

+30
-6
lines changed

gitcolombo.py

+30-6
Original file line numberDiff line numberDiff line change
@@ -18,17 +18,38 @@
1818
GIT_EXTRACT_CMD = "git log --pretty='{}' --all".format(LOG_FORMAT)
1919
GIT_CLONE_CMD = "git clone {}"
2020

21-
GITHUB_USER_REPOS = 'https://api.github.com/users/{}/repos'
22-
GITHUB_ORGS_REPOS = 'https://api.github.com/orgs/{}/repos'
21+
GITHUB_USER_STATS = 'https://api.github.com/users/{}'
22+
GITHUB_USER_REPOS = 'https://api.github.com/users/{}/repos?per_page=100&page={}'
23+
GITHUB_PER_PAGE_LIMIT = 100
2324

2425
SYSTEM_EMAILS = [
2526
2627
]
2728

28-
def get_github_repos(nickname, only_forks=True):
29+
30+
def get_public_repos_count(nickname):
31+
url = GITHUB_USER_STATS
32+
req_url = url.format(nickname)
33+
req = urllib.request.Request(req_url)
34+
try:
35+
response = urllib.request.urlopen(req)
36+
except Exception as e:
37+
logging.debug(e)
38+
else:
39+
stats = json.loads((response.read().decode('utf8')))
40+
repos_count = stats["public_repos"]
41+
if repos_count:
42+
return repos_count
43+
44+
45+
def get_github_repos(nickname, only_forks=True, repos_count=GITHUB_PER_PAGE_LIMIT):
2946
repos_links = set()
30-
for url in [GITHUB_ORGS_REPOS, GITHUB_USER_REPOS]:
31-
req_url = url.format(nickname)
47+
if not repos_count:
48+
return repos_links
49+
url = GITHUB_USER_REPOS
50+
last_page = int(repos_count / GITHUB_PER_PAGE_LIMIT) + (repos_count % GITHUB_PER_PAGE_LIMIT > 0)
51+
for page_num in range(1, last_page + 1):
52+
req_url = url.format(nickname, page_num)
3253
req = urllib.request.Request(req_url)
3354
try:
3455
response = urllib.request.urlopen(req)
@@ -312,7 +333,10 @@ def main():
312333
repos += dirs
313334

314335
if args.nickname:
315-
repos += get_github_repos(args.nickname)
336+
repos_count = get_public_repos_count(args.nickname)
337+
if repos_count:
338+
print('found', repos_count, 'repos')
339+
repos += get_github_repos(args.nickname, repos_count=repos_count)
316340

317341
for repo in repos:
318342
analyst.append(source=repo)

0 commit comments

Comments
 (0)