Skip to content

Commit 3e59afb

Browse files
authored
Merge pull request #38 from OSLL/31_branch_param
added -b mode - for branch specification
2 parents f24b331 + 50097d3 commit 3e59afb

File tree

3 files changed

+48
-32
lines changed

3 files changed

+48
-32
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pip install -r requirements.txt
1212
## Запуск приложения:
1313
1. Логирование commits
1414
```commandline
15-
python3 main.py [-t, --token] token (github токен вместо token) [-l, --list] list (list - строка пути к txt файлу со списком репозиториев) [-o, --out] out (out - название csv файла, в который будут помещены все логи)
15+
python3 main.py [-t, --token] token (github токен вместо token) [-l, --list] list (list - строка пути к txt файлу со списком репозиториев) [-o, --out] out (out - название csv файла, в который будут помещены все логи) [-b, --branch] branch (branch - название конкретной ветки, откуда брать коммиты или all - логгировать все коммиты изо всех веток)
1616
```
1717
2. Логирование issues
1818
```commandline

git_logger.py

Lines changed: 45 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ def get_assignee_story(github_object):
5858

5959

6060
def log_commit_to_csv(info, csv_name):
61-
fieldnames = ['repository name', 'commit id', 'author name', 'author login', 'author email', 'date and time',
62-
'changed files']
61+
fieldnames = ['repository name', 'author name', 'author login', 'author email', 'date and time',
62+
'changed files', 'commit id', 'branch' ]
6363
with open(csv_name, 'a', newline='') as file:
6464
writer = csv.DictWriter(file, fieldnames=fieldnames)
6565
writer.writerow(info)
@@ -69,30 +69,44 @@ def log_commit_to_stdout(info):
6969
print(info)
7070

7171

72-
def log_repository_commits(repository: Repository, csv_name, start, finish):
73-
for commit in repository.get_commits():
74-
if commit.commit.author.date.astimezone(
75-
pytz.timezone(timezone)) < start or commit.commit.author.date.astimezone(
76-
pytz.timezone(timezone)) > finish:
77-
continue
78-
if commit.commit is not None:
79-
info = {'repository name': repository.full_name,
80-
'author name': commit.commit.author.name,
81-
'author login': EMPTY_FIELD,
82-
'author email': EMPTY_FIELD,
83-
'date and time': commit.commit.author.date,
84-
'changed files': '; '.join([file.filename for file in commit.files]),
85-
'commit id': commit.commit.sha}
86-
87-
if commit.author is not None:
88-
info['author login'] = commit.author.login
89-
90-
if commit.commit.author is not None:
91-
info['author email'] = commit.commit.author.email
92-
93-
log_commit_to_csv(info, csv_name)
94-
log_commit_to_stdout(info)
95-
sleep(timedelta)
72+
def log_repository_commits(repository: Repository, csv_name, start, finish, branch):
73+
74+
75+
branches = []
76+
if branch == 'all':
77+
for branch in repository.get_branches():
78+
branches.append(branch.name)
79+
else:
80+
branches.append(branch)
81+
82+
#print(branches)
83+
84+
for branch in branches:
85+
print(f'Processing branch {branch}')
86+
for commit in repository.get_commits():
87+
if commit.commit.author.date.astimezone(
88+
pytz.timezone(timezone)) < start or commit.commit.author.date.astimezone(
89+
pytz.timezone(timezone)) > finish:
90+
continue
91+
if commit.commit is not None:
92+
info = {'repository name': repository.full_name,
93+
'author name': commit.commit.author.name,
94+
'author login': EMPTY_FIELD,
95+
'author email': EMPTY_FIELD,
96+
'date and time': commit.commit.author.date,
97+
'changed files': '; '.join([file.filename for file in commit.files]),
98+
'commit id': commit.commit.sha,
99+
'branch': branch}
100+
101+
if commit.author is not None:
102+
info['author login'] = commit.author.login
103+
104+
if commit.commit.author is not None:
105+
info['author email'] = commit.commit.author.email
106+
107+
log_commit_to_csv(info, csv_name)
108+
log_commit_to_stdout(info)
109+
sleep(timedelta)
96110

97111

98112
def log_issue_to_csv(info, csv_name):
@@ -444,25 +458,26 @@ def log_invitations(client: Github, repositories, csv_name):
444458
except Exception as e:
445459
print(e)
446460

447-
def log_commits(client: Github, repositories, csv_name, start, finish):
461+
def log_commits(client: Github, repositories, csv_name, start, finish, branch):
448462
with open(csv_name, 'w', newline='') as file:
449463
writer = csv.writer(file)
450464
writer.writerow(
451465
(
452466
'repository name',
453-
'commit id',
454467
'author name',
455468
'author login',
456469
'author email',
457470
'date and time',
458-
'changed files'
471+
'changed files',
472+
'commit id',
473+
'branch'
459474
)
460475
)
461476

462477
for repo in get_next_repo(client, repositories):
463478

464479
try:
465-
log_repository_commits(repo, csv_name, start, finish)
480+
log_repository_commits(repo, csv_name, start, finish, branch)
466481
sleep(timedelta)
467482
except Exception as e:
468483
print(e)

main.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def parse_args():
1616
parser.add_argument('-o', '--out', type=str, required=True, help='output filename')
1717
parser.add_argument('-s', '--start', type=str, required=False, help='start time', default='2000/01/01-00:00:00')
1818
parser.add_argument('-f', '--finish', type=str, required=False, help='finish time', default='2400/01/01-00:00:00')
19+
parser.add_argument('-b', '--branch', type=str, required=False, help='branch to select commits, default "main", use "all" to get all commits from all branches', default='main')
1920
parser.add_argument('--google_token', type=str, required=False, help='Specify path to google token file')
2021
parser.add_argument('--table_id', type=str, required=False,
2122
help='Specify Google sheet document id (can find in url)')
@@ -59,7 +60,7 @@ def main():
5960
if args.finish:
6061
finish = parse_time(args.finish.split('-'))
6162
if not args.p and not args.i and not args.invites:
62-
git_logger.log_commits(client, repositories, csv_name, start, finish)
63+
git_logger.log_commits(client, repositories, csv_name, start, finish, args.branch)
6364
if (args.e):
6465
export_sheets.write_data_to_table(csv_name, args.google_token, args.table_id, args.sheet_id)
6566
if args.p:

0 commit comments

Comments
 (0)