-
Notifications
You must be signed in to change notification settings - Fork 3
/
commits_parser.py
68 lines (56 loc) · 2.71 KB
/
commits_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import csv
import pytz
from time import sleep
from github import Github, Repository, GithubException, PullRequest
EMPTY_FIELD = 'Empty field'
TIMEDELTA = 0.05
TIMEZONE = 'Europe/Moscow'
FIELDNAMES = ('repository name', 'author name', 'author login', 'author email', 'date and time', 'changed files', 'commit id', 'branch')
def log_commit_to_csv(info, csv_name):
with open(csv_name, 'a', newline='') as file:
writer = csv.DictWriter(file, fieldnames=FIELDNAMES)
writer.writerow(info)
def log_commit_to_stdout(info):
print(info)
def log_repository_commits(repository: Repository, csv_name, start, finish, branch):
branches = []
match branch:
case 'all':
for branch in repository.get_branches():
branches.append(branch.name)
case None:
branches.append(repository.default_branch)
case _:
branches.append(branch)
for branch in branches:
print(f'Processing branch {branch}')
# TODO add support of since and until in https://pygithub.readthedocs.io/en/stable/github_objects/Repository.html#github.Repository.Repository.get_commits
for commit in repository.get_commits(sha=branch):
if commit.commit.author.date.astimezone(
pytz.timezone(TIMEZONE)) < start or commit.commit.author.date.astimezone(
pytz.timezone(TIMEZONE)) > finish:
continue
if commit.commit is not None:
nvl = lambda val: val or EMPTY_FIELD
commit_data = [repository.full_name, commit.commit.author.name, nvl(commit.author.login if commit.author else None), nvl(commit.commit.author.email),
commit.commit.author.date, '; '.join([file.filename for file in commit.files]), commit.commit.sha, branch]
info = dict(zip(FIELDNAMES, commit_data))
log_commit_to_csv(info, csv_name)
log_commit_to_stdout(info)
sleep(TIMEDELTA)
def log_commits(client: Github, working_repos, csv_name, start, finish, branch, fork_flag):
with open(csv_name, 'w', newline='') as file:
writer = csv.writer(file)
writer.writerow(FIELDNAMES)
for repo in working_repos:
try:
print('=' * 20, repo.full_name, '=' * 20)
log_repository_commits(repo, csv_name, start, finish, branch)
if fork_flag:
for forked_repo in repo.get_forks():
print('=' * 20, "FORKED:", forked_repo.full_name, '=' * 20)
log_repository_commits(forked_repo, csv_name, start, finish, branch)
sleep(TIMEDELTA)
sleep(TIMEDELTA)
except Exception as e:
print(e)