Skip to content

Commit 3d0e5e2

Browse files
committed
Fixed the 404 error with Gitee PR commit and comment
Signed-off-by: Willem Jiang <willem.jiang@gmail.com>
1 parent e507109 commit 3d0e5e2

2 files changed

Lines changed: 91 additions & 31 deletions

File tree

perceval/backends/core/gitee.py

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -370,41 +370,53 @@ def __get_pull_commits(self, pr_number):
370370
"""Get pull request commit hashes"""
371371

372372
hashes = []
373-
group_pull_commits = self.client.pull_commits(pr_number)
373+
try:
374+
group_pull_commits = self.client.pull_commits(pr_number)
374375

375-
for raw_pull_commits in group_pull_commits:
376+
for raw_pull_commits in group_pull_commits:
376377

377-
for commit in json.loads(raw_pull_commits):
378-
commit_hash = commit['sha']
379-
hashes.append(commit_hash)
378+
for commit in json.loads(raw_pull_commits):
379+
commit_hash = commit['sha']
380+
hashes.append(commit_hash)
380381

382+
except requests.exceptions.HTTPError as error:
383+
# 404 not found is wrongly received from gitee API service
384+
if error.response.status_code == 404:
385+
logger.error("Can't get gitee pull request commits with PR number %s", pr_number)
386+
else:
387+
raise error
381388
return hashes
382389

383390
def __get_pull_review_comments(self, pr_number):
384391
"""Get pull request review comments"""
385392

386393
comments = []
387-
group_comments = self.client.pull_review_comments(pr_number)
388-
389-
for raw_comments in group_comments:
390-
391-
for comment in json.loads(raw_comments):
392-
comment_id = comment.get('id')
393-
394-
user = comment.get('user', None)
395-
if not user:
396-
logger.warning("Missing user info for %s", comment['url'])
397-
comment['user_data'] = None
398-
else:
399-
comment['user_data'] = self.__get_user(user['login'])
400-
comments.append(comment)
394+
try:
395+
group_comments = self.client.pull_review_comments(pr_number)
396+
for raw_comments in group_comments:
397+
398+
for comment in json.loads(raw_comments):
399+
comment_id = comment.get('id')
400+
401+
user = comment.get('user', None)
402+
if not user:
403+
logger.warning("Missing user info for %s", comment['url'])
404+
comment['user_data'] = None
405+
else:
406+
comment['user_data'] = self.__get_user(user['login'])
407+
comments.append(comment)
408+
except requests.exceptions.HTTPError as error:
409+
# 404 not found is wrongly received from gitee API service
410+
if error.response.status_code == 404:
411+
logger.error("Can't get gitee pull request comments with PR number %s", pr_number)
412+
else:
413+
raise error
401414

402415
return comments
403416

404417
# TODO need to check the Gitee API for the pull reviews
405418
def __get_pull_reviews(self, pr_number):
406419
"""Get pull request reviews"""
407-
408420
reviews = []
409421
group_reviews = self.client.pull_reviews(pr_number)
410422

@@ -419,6 +431,7 @@ def __get_pull_reviews(self, pr_number):
419431
review['user_data'] = self.__get_user(user['login'])
420432

421433
reviews.append(review)
434+
422435
return reviews
423436

424437
def __get_users(self, items):
@@ -598,24 +611,15 @@ def pull_commits(self, pr_number):
598611
}
599612

600613
commit_url = urijoin("pulls", str(pr_number), "commits")
601-
try:
602-
result = self.fetch_items(commit_url, payload)
603-
except requests.exceptions.HTTPError as error:
604-
# 404 not found is wrongly received sometimes
605-
if error.response.status_code == 404:
606-
logger.error("Can't get gitee pull commits with : %s", commit_url)
607-
result = '[]'
608-
else:
609-
raise error
610-
return result
614+
return self.fetch_items(commit_url, payload)
611615

612616
def pull_review_comments(self, pr_number):
613617
"""Get pull request review comments"""
614618

615619
payload = {
616620
'per_page': PER_PAGE,
617621
'direction': 'asc',
618-
# doesn't suppor sort parameter
622+
# doesn't support sort parameter
619623
# 'sort': 'updated'
620624
}
621625

tests/test_gitee.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,15 @@ def setup_gitee_issue_services():
6262
def setup_gitee_pull_request_services():
6363
setup_gitee_basic_services()
6464
__setup_gitee_pull_request_services()
65+
__setup_gitee_pull_request_services_with_commits_and_comments()
66+
__setup_gitee_pull_request_service_action_logs()
67+
68+
69+
def setup_gitee_pull_request_services_with_empty_commits_and_comments():
70+
setup_gitee_basic_services()
71+
__setup_gitee_pull_request_services()
72+
__setup_gitee_pull_request_services_with_empty_commits_and_comments()
73+
__setup_gitee_pull_request_service_action_logs()
6574

6675

6776
def setup_gitee_basic_services():
@@ -142,6 +151,8 @@ def __setup_gitee_pull_request_services():
142151
body=pull_request_2, status=200,
143152
forcing_headers=pagination_pull_header_2)
144153

154+
155+
def __setup_gitee_pull_request_services_with_commits_and_comments():
145156
pull_request_1_comments = read_file('data/gitee/gitee_pull_request1_comments')
146157
httpretty.register_uri(httpretty.GET, GITEE_PULL_REQUEST_1_COMMENTS_URL,
147158
body=pull_request_1_comments, status=200,
@@ -178,6 +189,26 @@ def __setup_gitee_pull_request_services():
178189
body=pull_request_2_action_logs, status=200)
179190

180191

192+
def __setup_gitee_pull_request_services_with_empty_commits_and_comments():
193+
httpretty.register_uri(httpretty.GET, GITEE_PULL_REQUEST_1_COMMENTS_URL,
194+
body="", status=404)
195+
httpretty.register_uri(httpretty.GET, GITEE_PULL_REQUEST_1_COMMITS_URL,
196+
body="", status=404)
197+
httpretty.register_uri(httpretty.GET, GITEE_PULL_REQUEST_2_COMMENTS_URL,
198+
body="", status=404)
199+
httpretty.register_uri(httpretty.GET, GITEE_PULL_REQUEST_2_COMMITS_URL,
200+
body="", status=404)
201+
202+
203+
def __setup_gitee_pull_request_service_action_logs():
204+
pull_request_1_action_logs = read_file('data/gitee/gitee_pull_request1_action_logs')
205+
httpretty.register_uri(httpretty.GET, GITEE_PULL_REQUEST_1_OPERATE_LOGS_URL,
206+
body=pull_request_1_action_logs, status=200)
207+
pull_request_2_action_logs = read_file('data/gitee/gitee_pull_request2_action_logs')
208+
httpretty.register_uri(httpretty.GET, GITEE_PULL_REQUEST_2_OPERATE_LOGS_URL,
209+
body=pull_request_2_action_logs, status=200)
210+
211+
181212
def setup_refresh_access_token_service():
182213
httpretty.register_uri(httpretty.POST, GITEE_REFRESH_TOKEN_URL, body="", status=200)
183214

@@ -320,6 +351,31 @@ def test_fetch_pulls(self):
320351
self.assertEqual(pull['data']['merged_by'], None)
321352
self.assertEqual(pull['data']['merged_by_data'], [])
322353

354+
@httpretty.activate
355+
def test_fetch_pulls_with_empty_commits_and_comments(self):
356+
setup_gitee_pull_request_services_with_empty_commits_and_comments()
357+
from_date = datetime.datetime(2019, 1, 1)
358+
gitee = Gitee("gitee_example", "repo", "[aaa]")
359+
pulls = [pr for pr in gitee.fetch(category=CATEGORY_PULL_REQUEST, from_date=from_date)]
360+
361+
self.assertEqual(len(pulls), 2)
362+
pull = pulls[0]
363+
self.assertEqual(len(pull['data']['review_comments_data']), 0)
364+
# check if the testers_data there
365+
self.assertTrue('tester_data' not in pull['data'])
366+
self.assertEqual(pull['data']['commits_data'], [])
367+
self.assertEqual(pull['data']['merged_by'], "willemjiang")
368+
self.assertEqual(pull['data']['merged_by_data']['login'], "willemjiang")
369+
370+
pull = pulls[1]
371+
self.assertEqual(len(pull['data']['review_comments_data']), 0)
372+
self.assertEqual(len(pull['data']['review_comments_data']), 0)
373+
# check if the testers_data there
374+
self.assertTrue('tester_data' not in pull['data'])
375+
self.assertEqual(pull['data']['commits_data'], [])
376+
self.assertEqual(pull['data']['merged_by'], None)
377+
self.assertEqual(pull['data']['merged_by_data'], [])
378+
323379
def test_has_resuming(self):
324380
"""Test if it returns True when has_resuming is called"""
325381

0 commit comments

Comments
 (0)