Skip to content

Commit f2fd9b4

Browse files
committed
contributors parser implimented
1 parent b33af45 commit f2fd9b4

File tree

5 files changed

+101
-13
lines changed

5 files changed

+101
-13
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__pycache__

contributors_parser.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import csv
2+
from time import sleep
3+
from typing import Generator
4+
from github import Github, Repository, GithubException
5+
6+
EMPTY_FIELD = 'Empty field'
7+
TIMEDELTA = 0.05
8+
TIMEZONE = 'Europe/Moscow'
9+
FIELDNAMES = ('repository name', 'login', 'name', 'email', 'url', 'permissions', 'total_commits', 'id', 'node_id', 'type', 'bio', 'site_admin')
10+
11+
12+
def log_contributors_to_csv(info: dict, csv_name: str):
13+
with open(csv_name, 'a', newline='') as file:
14+
writer = csv.DictWriter(file, fieldnames=FIELDNAMES)
15+
writer.writerow(info)
16+
17+
def log_contributors_to_stdout(info: dict):
18+
print(info)
19+
20+
def log_repository_contributors(repository: Repository, csv_name: str):
21+
contributors_stats = get_contributors_stats(repository)
22+
23+
nvl = lambda val: val or EMPTY_FIELD
24+
25+
for contributor_stat in contributors_stats.values():
26+
contributor = contributor_stat["contributor_object"]
27+
contributor_permissons = repository.get_collaborator_permission(contributor)
28+
29+
info_tmp = {
30+
'repository name': repository.full_name,
31+
'login': contributor.login,
32+
'name': nvl(contributor.name),
33+
'email': nvl(contributor_stat['email']),
34+
'url': contributor.html_url,
35+
'permissions': nvl(contributor_permissons),
36+
'total_commits': contributor_stat['total_commits'],
37+
'id': contributor.id,
38+
'node_id': contributor.node_id,
39+
'type': contributor.type,
40+
'bio': nvl(contributor.bio),
41+
'site_admin': contributor.site_admin,
42+
}
43+
44+
log_contributors_to_csv(info_tmp, csv_name)
45+
log_contributors_to_stdout(info_tmp)
46+
47+
sleep(TIMEDELTA)
48+
49+
def get_contributors_stats(repository: Repository) -> dict:
50+
contributors_stats = dict()
51+
52+
for commit in repository.get_commits():
53+
contributor = commit.author
54+
55+
if not contributor.login in contributors_stats:
56+
contributors_stats[contributor.login] = {
57+
'total_commits': 0,
58+
'email': commit.commit.author.email,
59+
'contributor_object': contributor
60+
}
61+
62+
contributors_stats[contributor.login]['total_commits'] += 1
63+
64+
sleep(TIMEDELTA)
65+
66+
return contributors_stats
67+
68+
def log_contributors(client: Github, working_repos: Generator, csv_name: str, fork_flag: bool):
69+
with open(csv_name, 'w', newline='') as file:
70+
writer = csv.writer(file)
71+
writer.writerow(FIELDNAMES)
72+
73+
for repo in working_repos:
74+
try:
75+
print('=' * 20, repo.full_name, '=' * 20)
76+
log_repository_contributors(repo, csv_name)
77+
78+
if fork_flag:
79+
for forked_repo in repo.get_forks():
80+
print('=' * 20, "FORKED:", forked_repo.full_name, '=' * 20)
81+
log_repository_contributors(forked_repo, csv_name)
82+
sleep(TIMEDELTA)
83+
84+
except GithubException as e:
85+
print(e)
86+
exit(1)

git_logger.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def login(token):
1212
except GithubException as err:
1313
print(f'Github: Connect: error {err.data}')
1414
print('Github: Connect: user could not be authenticated please try again.')
15-
raise exit(1)
15+
exit(1)
1616
else:
1717
return client
1818

main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import issues_parser
1010
import invites_parser
1111
import wikipars
12+
import contributors_parser
1213

1314
def parse_args():
1415
parser = argparse.ArgumentParser()
@@ -17,6 +18,7 @@ def parse_args():
1718
parser.add_argument("-p", "--pull_requests", help="log pull requests", action="store_true")
1819
parser.add_argument("-i", "--issues", help="log issues", action="store_true")
1920
parser.add_argument("-w", "--wikis", help="log wikis", action="store_true")
21+
parser.add_argument("--contributors", help="log contributors", action="store_true")
2022
parser.add_argument("--forks_include", help="logging data from forks", action="store_true")
2123
parser.add_argument("-e", "--export_google_sheets", help="export table to google sheets", action="store_true")
2224
parser.add_argument('-t', '--token', type=str, required=True, help='token github account')
@@ -62,7 +64,6 @@ def main():
6264
path_drepo = args.download_repos
6365
fork_flag = args.forks_include
6466
log_pr_comments = args.pr_comments
65-
start, finish = None, None
6667

6768
try:
6869
client = git_logger.login(token=token)
@@ -84,6 +85,8 @@ def main():
8485
invites_parser.log_invitations(client, working_repos, csv_name)
8586
if args.wikis:
8687
wikipars.wikiparser(client, repositories, path_drepo, csv_name)
88+
if args.contributors:
89+
contributors_parser.log_contributors(client, working_repos, csv_name, fork_flag)
8790
if args.export_google_sheets:
8891
export_sheets.write_data_to_table(csv_name, args.google_token, args.table_id, args.sheet_id)
8992

pull_requests_parser.py

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,17 +107,15 @@ def log_repositories_pr(repository: Repository, csv_name, token, start, finish,
107107
}
108108

109109
if log_comments:
110-
comments = pull.get_comments()
111-
if comments.totalCount > 0:
112-
for comment in comments:
113-
info = info_tmp
114-
info['comment body'] = comment.body
115-
info['comment created at'] = comment.created_at
116-
info['comment author name'] = comment.user.name
117-
info['comment author login'] = comment.user.login
118-
info['comment author email'] = nvl(comment.user.email)
119-
log_pr_to_csv(info, csv_name)
120-
log_pr_to_stdout(info)
110+
for comment in pull.get_comments():
111+
info = info_tmp
112+
info['comment body'] = comment.body
113+
info['comment created at'] = comment.created_at
114+
info['comment author name'] = comment.user.name
115+
info['comment author login'] = comment.user.login
116+
info['comment author email'] = nvl(comment.user.email)
117+
log_pr_to_csv(info, csv_name)
118+
log_pr_to_stdout(info)
121119
else:
122120
log_pr_to_csv(info_tmp, csv_name)
123121
log_pr_to_stdout(info_tmp)

0 commit comments

Comments
 (0)