Skip to content

Commit 50fb8dd

Browse files
Arg for on/off PR comments export (#62)
* add --comments args * log PR comments only if flag is set * Update commits_parser.py (fix author.login error) * update name of pr_comments flag & add info to readme * fix "Possibly using variable 'start' / 'finish' before assignment"
1 parent 3c5290c commit 50fb8dd

File tree

4 files changed

+21
-16
lines changed

4 files changed

+21
-16
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ python3 main.py [-i, --issues] [-t, --token] token (github токен вмест
3232
```
3333
3. Логирование pull requests
3434
```commandline
35-
python3 main.py [-p, --pull_requests] [-t, --token] token (github токен вместо token) [-l, --list] list (list - строка пути к txt файлу со списком репозиториев) [-o, --out] out (out - название csv файла, в который будут помещены все логи)
35+
python3 main.py [-p, --pull_requests] [-t, --token] token (github токен вместо token) [-l, --list] list (list - строка пути к txt файлу со списком репозиториев) [-o, --out] out (out - название csv файла, в который будут помещены все логи) [--pr_comments] (если установлен - также выгружаются комментарии к PR)
3636
```
3737
4. Логирование непринятых приглашений в репо
3838
```commandline

commits_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def log_repository_commits(repository: Repository, csv_name, start, finish, bran
3939
continue
4040
if commit.commit is not None:
4141
nvl = lambda val: val or EMPTY_FIELD
42-
commit_data = [repository.full_name, commit.commit.author.name, nvl(commit.author.login), nvl(commit.commit.author.email),
42+
commit_data = [repository.full_name, commit.commit.author.name, nvl(commit.author.login if commit.author else None), nvl(commit.commit.author.email),
4343
commit.commit.author.date, '; '.join([file.filename for file in commit.files]), commit.commit.sha, branch]
4444
info = dict(zip(FIELDNAMES, commit_data))
4545

main.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ def parse_args():
2323
parser.add_argument('-l', '--list', type=str, required=True, help='Path to the file containing the list of repositories. Repositories should be separated by a line break. Names should be in the format <organization or owner>/<name> ')
2424
parser.add_argument("--download_repos", type=str, help="path to downloaded repositories", default='./')
2525
parser.add_argument('-o', '--out', type=str, required=True, help='output filename')
26+
parser.add_argument("--pr_comments", help="log comments for PR", action="store_true")
2627
parser.add_argument('-s', '--start', type=str, required=False, help='start time', default='2000/01/01-00:00:00')
2728
parser.add_argument('-f', '--finish', type=str, required=False, help='finish time', default='2400/01/01-00:00:00')
2829
parser.add_argument('-b', '--branch', type=str, required=False, help='branch to select commits, by default use "default" repository branch, use "all" to get all commits from all branches', default=None)
@@ -60,6 +61,8 @@ def main():
6061
csv_name = args.out
6162
path_drepo = args.download_repos
6263
fork_flag = args.forks_include
64+
log_pr_comments = args.pr_comments
65+
start, finish = None, None
6366

6467
try:
6568
client = git_logger.login(token=token)
@@ -74,7 +77,7 @@ def main():
7477
if args.commits:
7578
commits_parser.log_commits(client, working_repos, csv_name, start, finish, args.branch, fork_flag)
7679
if args.pull_requests:
77-
pull_requests_parser.log_pull_requests(client, working_repos, csv_name, token, start, finish, fork_flag)
80+
pull_requests_parser.log_pull_requests(client, working_repos, csv_name, token, start, finish, fork_flag, log_pr_comments)
7881
if args.issues:
7982
issues_parser.log_issues(client, working_repos, csv_name, token, start, finish, fork_flag)
8083
if args.invites:

pull_requests_parser.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def get_related_issues(pull_request_number, repo_owner, repo_name, token):
7171
return ';'.join(list_issues_url)
7272

7373

74-
def log_repositories_pr(repository: Repository, csv_name, token, start, finish):
74+
def log_repositories_pr(repository: Repository, csv_name, token, start, finish, log_comments=False):
7575
for pull in repository.get_pulls(state='all'):
7676
if pull.created_at.astimezone(pytz.timezone(TIMEZONE)) < start or pull.created_at.astimezone(
7777
pytz.timezone(TIMEZONE)) > finish:
@@ -106,23 +106,25 @@ def log_repositories_pr(repository: Repository, csv_name, token, start, finish):
106106
'milestone': get_info(pull.milestone, 'title')
107107
}
108108

109-
if pull.get_comments().totalCount > 0:
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)
109+
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)
119121
else:
120122
log_pr_to_csv(info_tmp, csv_name)
121123
log_pr_to_stdout(info_tmp)
122124
sleep(TIMEDELTA)
123125

124126

125-
def log_pull_requests(client: Github, working_repos, csv_name, token, start, finish, fork_flag):
127+
def log_pull_requests(client: Github, working_repos, csv_name, token, start, finish, fork_flag, log_comments=False):
126128
with open(csv_name, 'w', newline='') as file:
127129
writer = csv.writer(file)
128130
writer.writerow(FIELDNAMES)
@@ -134,7 +136,7 @@ def log_pull_requests(client: Github, working_repos, csv_name, token, start, fin
134136
if fork_flag:
135137
for forked_repo in repo.get_forks():
136138
print('=' * 20, "FORKED:", forked_repo.full_name, '=' * 20)
137-
log_repositories_pr(forked_repo, csv_name, token, start, finish)
139+
log_repositories_pr(forked_repo, csv_name, token, start, finish, log_comments)
138140
sleep(TIMEDELTA)
139141
sleep(TIMEDELTA)
140142
except Exception as e:

0 commit comments

Comments
 (0)