Skip to content

Commit fae909f

Browse files
f
1 parent 3f51c81 commit fae909f

File tree

1 file changed

+94
-78
lines changed

1 file changed

+94
-78
lines changed

pull_requests_parser.py

Lines changed: 94 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,50 @@
1-
from utils import logger
1+
from dataclasses import dataclass, asdict
2+
from time import sleep
3+
24
import pytz
35
import requests
46
import json
5-
from time import sleep
7+
68
from git_logger import get_assignee_story
79
from interface_wrapper import IRepositoryAPI, Repository
10+
from utils import logger
811

912
EMPTY_FIELD = 'Empty field'
1013
TIMEDELTA = 0.05
1114
TIMEZONE = 'Europe/Moscow'
12-
FIELDNAMES = (
13-
'repository name',
14-
'title',
15-
'id',
16-
'state',
17-
'commit into',
18-
'commit from',
19-
'created at',
20-
'creator name',
21-
'creator login',
22-
'creator email',
23-
'changed files',
24-
'comment body',
25-
'comment created at',
26-
'comment author name',
27-
'comment author login',
28-
'comment author email',
29-
'merger name',
30-
'merger login',
31-
'merger email',
32-
'source branch',
33-
'target branch',
34-
'assignee story',
35-
'related issues',
36-
'labels',
37-
'milestone',
38-
)
15+
16+
17+
@dataclass(kw_only=True, frozen=True)
18+
class PullRequestData:
19+
repository_name: str = ''
20+
title: str = ''
21+
id: int = 0
22+
state: str = ''
23+
commit_into: str = ''
24+
commit_from: str = ''
25+
created_at: str = ''
26+
creator_name: str = ''
27+
creator_login: str = ''
28+
creator_email: str = ''
29+
changed_files: str = ''
30+
merger_name: str | None = None
31+
merger_login: str | None = None
32+
merger_email: str | None = None
33+
source_branch: str = ''
34+
target_branch: str = ''
35+
assignee_story: str = ''
36+
related_issues: str = ''
37+
labels: str = ''
38+
milestone: str = ''
39+
40+
41+
@dataclass(kw_only=True, frozen=True)
42+
class PullRequestDataWithComment(PullRequestData):
43+
body: str = ''
44+
created_at: str = ''
45+
author_name: str = ''
46+
author_login: str = ''
47+
author_email: str = ''
3948

4049

4150
def get_related_issues(pull_request_number, repo_owner, repo_name, token):
@@ -94,6 +103,14 @@ def get_related_issues(pull_request_number, repo_owner, repo_name, token):
94103
return ';'.join(list_issues_url)
95104

96105

106+
def nvl(val):
107+
return val or EMPTY_FIELD
108+
109+
110+
def get_info(obj, attr):
111+
return EMPTY_FIELD if obj is None else getattr(obj, attr)
112+
113+
97114
def log_repositories_pr(
98115
client: IRepositoryAPI,
99116
repository: Repository,
@@ -117,60 +134,58 @@ def get_info(obj, attr):
117134
):
118135
continue
119136

120-
info_tmp = {
121-
'repository name': repository.name,
122-
'title': pull.title,
123-
'id': pull._id,
124-
'state': pull.state,
125-
'commit into': pull.base_label,
126-
'commit from': pull.head_label,
127-
'created at': pull.created_at,
128-
'creator name': nvl(pull.author.username),
129-
'creator login': pull.author.login,
130-
'creator email': pull.author.email,
131-
'changed files': '; '.join([file for file in pull.files]),
132-
'comment body': EMPTY_FIELD,
133-
'comment created at': EMPTY_FIELD,
134-
'comment author name': EMPTY_FIELD,
135-
'comment author login': EMPTY_FIELD,
136-
'comment author email': EMPTY_FIELD,
137-
'merger name': pull.merged_by.username if pull.merged_by else None,
138-
'merger login': pull.merged_by.login if pull.merged_by else None,
139-
'merger email': pull.merged_by.email if pull.merged_by else None,
140-
'source branch': pull.head_ref,
141-
'target branch': pull.base_ref,
142-
'assignee story': get_assignee_story(pull),
143-
'related issues': (
144-
EMPTY_FIELD
145-
if pull.issue_url is None
146-
else get_related_issues(
147-
pull._id, repository.owner, repository.name, token
148-
)
137+
pr_data = PullRequestData(
138+
repository_name=repository.name,
139+
title=pull.title,
140+
id=pull._id,
141+
state=pull.state,
142+
commit_into=pull.base_label,
143+
commit_from=pull.head_label,
144+
created_at=str(pull.created_at),
145+
creator_name=nvl(pull.author.username),
146+
creator_login=pull.author.login,
147+
creator_email=pull.author.email,
148+
changed_files='; '.join(pull.files),
149+
merger_name=pull.merged_by.username if pull.merged_by else None,
150+
merger_login=pull.merged_by.login if pull.merged_by else None,
151+
merger_email=pull.merged_by.email if pull.merged_by else None,
152+
source_branch=pull.head_ref,
153+
target_branch=pull.base_ref,
154+
assignee_story=get_assignee_story(pull),
155+
related_issues=(
156+
get_related_issues(pull._id, repository.owner, repository.name, token)
157+
if pull.issue_url is not None
158+
else EMPTY_FIELD
149159
),
150-
'labels': (
151-
EMPTY_FIELD
152-
if pull.labels is None
153-
else ';'.join([label for label in pull.labels])
154-
),
155-
'milestone': get_info(pull.milestone, 'title'),
156-
}
160+
labels=';'.join(pull.labels) if pull.labels else EMPTY_FIELD,
161+
milestone=get_info(pull.milestone, 'title'),
162+
)
157163

158164
if log_comments:
159165
comments = client.get_comments(repository, pull)
160-
if len(comments) > 0:
166+
if comments:
161167
for comment in comments:
162-
info = info_tmp
163-
info['comment body'] = comment.body
164-
info['comment created at'] = comment.created_at
165-
info['comment author name'] = comment.author.name
166-
info['comment author login'] = comment.author.login
167-
info['comment author email'] = nvl(comment.author.email)
168-
169-
logger.log_to_csv(csv_name, FIELDNAMES, info)
170-
logger.log_to_stdout(info)
168+
comment_data = PullRequestDataWithComment(
169+
**pr_data,
170+
body=comment.body,
171+
created_at=str(comment.created_at),
172+
author_name=comment.author.name,
173+
author_login=comment.author.login,
174+
author_email=nvl(comment.author.email),
175+
)
176+
comment_data = asdict(comment_data)
177+
178+
logger.log_to_csv(csv_name, list(comment_data.keys()), comment_data)
179+
logger.log_to_stdout(comment_data)
180+
else:
181+
base_pr_info = asdict(pr_data)
182+
logger.log_to_csv(csv_name, list(base_pr_info.keys()), base_pr_info)
183+
logger.log_to_stdout(base_pr_info)
171184
else:
172-
logger.log_to_csv(csv_name, FIELDNAMES, info_tmp)
173-
logger.log_to_stdout(info_tmp)
185+
base_pr_info = asdict(pr_data)
186+
logger.log_to_csv(csv_name, list(base_pr_info.keys()), base_pr_info)
187+
logger.log_to_stdout(base_pr_info)
188+
174189
sleep(TIMEDELTA)
175190

176191

@@ -183,7 +198,8 @@ def log_pull_requests(
183198
fork_flag,
184199
log_comments=False,
185200
):
186-
logger.log_to_csv(csv_name, FIELDNAMES)
201+
info = asdict(PullRequestDataWithComment())
202+
logger.log_to_csv(csv_name, list(info.keys()))
187203

188204
for repo, token in working_repos:
189205
try:

0 commit comments

Comments
 (0)