Skip to content

Commit 7a873f0

Browse files
committed
Merge branch 'release/v0.1.0' into main
2 parents 33fb0bf + 384424e commit 7a873f0

File tree

7 files changed

+154
-1
lines changed

7 files changed

+154
-1
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,7 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
131+
# git-analyzer project settings
132+
config.json
133+
data/

README.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,34 @@
11
# git-analyzer
2-
Explore the GitHub API
2+
Analyze the repositories and branches of a GitHub user
3+
4+
## Configure
5+
### config.json
6+
```
7+
{
8+
"username": "YOUR_USERNAME_HERE",
9+
"github_access_token": "YOUR_GITHUB_ACCESS_TOKEN_HERE",
10+
"branch_search_pattern": "BRANCH_NAME_REGEX_PATTERN_HERE",
11+
"max_repos_to_download": 300
12+
}
13+
```
14+
### username
15+
Your GitHub username
16+
### github_access_token
17+
The access token from GitHub with access to repositories
18+
### branch_search_pattern
19+
The search pattern for repositories containing a branch name matching the
20+
pattern
21+
### max_repos_to_download
22+
The maximum number of repositories to fetch information for (this can prevent
23+
exceeding the rate limit too quickly)
24+
25+
## Usage
26+
### Download repository and branch data
27+
```
28+
python main.py
29+
```
30+
31+
### Search for branches matching a regex pattern
32+
```
33+
python find_branches_by_regex.py
34+
```

config-sample.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"username": "YOUR_USERNAME_HERE",
3+
"github_access_token": "YOUR_GITHUB_ACCESS_TOKEN_HERE",
4+
"branch_search_pattern": "BRANCH_NAME_REGEX_PATTERN_HERE",
5+
"max_repos_to_download": 300
6+
}

find_branches_by_regex.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
from get_config_value import get_config_value
2+
import json
3+
import re
4+
5+
def main():
6+
with open('data/user_repo_branches.json', 'r') as input_file:
7+
json_data = json.load(input_file)
8+
9+
print('Data source access time:', json_data['data_source_access_time'])
10+
11+
for repo_key in json_data['data']['repos']:
12+
repo = json_data['data']['repos'][repo_key]
13+
14+
for branch_key in repo['branches']:
15+
branch = repo['branches'][branch_key]
16+
match = re.search(get_config_value('branch_search_pattern'),
17+
branch['name'])
18+
19+
if match:
20+
print('Matching branch found in ' + repo['name'] + ': ' + \
21+
branch['name'])
22+
23+
main()

get_config_value.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import json
2+
3+
def get_config_value(key):
4+
with open('config.json', 'r') as config_file:
5+
config_json = json.load(config_file)
6+
7+
return config_json[key]

main.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
from get_config_value import get_config_value
2+
from github import Github
3+
import json
4+
from ratelimiter import RateLimiter
5+
from time import gmtime, strftime
6+
7+
def get_user_repo_branches():
8+
extract_json = {
9+
'data_source_access_time': strftime("%Y%m%dT%H%M%SZ", gmtime()),
10+
'data_source_name': 'GitHub API'
11+
}
12+
g = Github(get_config_value('github_access_token'))
13+
user = g.get_user()
14+
user_json = {
15+
'login': user.login,
16+
'public_repos': user.public_repos,
17+
'repos': {}
18+
}
19+
repos = user.get_repos()
20+
rate_limiter = RateLimiter(max_calls=5000, period=3600)
21+
22+
for i, repo in enumerate(repos):
23+
with rate_limiter:
24+
if i >= get_config_value('max_repos_to_download'):
25+
pass
26+
else:
27+
repo_name = repo.name
28+
repo_json = {
29+
'name': repo_name,
30+
'private': repo.private,
31+
'forks_count': repo.forks_count,
32+
'stargazers_count': repo.stargazers_count,
33+
'watchers_count': repo.watchers_count,
34+
'size': repo.size,
35+
'open_issues_count': repo.open_issues_count,
36+
'has_issues': repo.has_issues,
37+
'has_projects': repo.has_projects,
38+
'has_wiki': repo.has_wiki,
39+
'has_downloads': repo.has_downloads,
40+
'pushed_at': repo.pushed_at,
41+
'created_at': repo.created_at,
42+
'updated_at': repo.updated_at,
43+
'branches': {}
44+
}
45+
branches = repo.get_branches()
46+
47+
for branch in branches:
48+
branch_name = branch.name
49+
branch_commit = branch.commit
50+
branch_json = {
51+
'name': branch_name,
52+
'commit': {
53+
'sha': branch_commit.sha,
54+
'url': branch_commit.url
55+
},
56+
'protected': branch.protected,
57+
}
58+
repo_json['branches'][branch_name] = branch_json
59+
60+
user_json['repos'][repo_name] = repo_json
61+
62+
extract_json['data'] = user_json
63+
64+
with open('data/user_repo_branches.json', 'w') as output_file:
65+
json.dump(extract_json, output_file, indent=4, default=str)
66+
67+
def display_rate_limit():
68+
g = Github(get_config_value('github_access_token'))
69+
print('Rate limit stats:\nCore limit: ', g.rate_limiting[0])
70+
71+
def prompt_user_to_continue():
72+
user_input = input('Do you wish to proceed? (y/n): ')
73+
return user_input.upper() == 'Y'
74+
75+
def main():
76+
display_rate_limit()
77+
78+
if prompt_user_to_continue():
79+
get_user_repo_branches()
80+
81+
main()

requirements.txt

354 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)