Skip to content

Commit d1fed8b

Browse files
committed
Fix out of range error from totalCount
Signed-off-by: Luiz Carvalho <lucarval@redhat.com>
1 parent 3436ce9 commit d1fed8b

3 files changed

Lines changed: 15 additions & 6 deletions

File tree

reviewrot/githubstack.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,19 @@ def get_last_comment(self, pr):
193193
last_issue_comment = None
194194
last_comment = None
195195

196-
if review_comments.totalCount > 0:
196+
try:
197+
# review_comments.totalCount may return a non-zero value when
198+
# the list of comments is actually empty :(
197199
last_review_comment = review_comments.reversed[0]
200+
except IndexError:
201+
pass
198202

199-
if issue_comments.totalCount > 0:
203+
try:
204+
# issue_comments.totalCount may return a non-zero value when
205+
# the list of comments is actually empty :(
200206
last_issue_comment = issue_comments.reversed[0]
207+
except IndexError:
208+
pass
201209

202210
# check which is newer if pr has both types of comments
203211
if last_issue_comment and last_review_comment:

test/github_tests/mock_github.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,10 @@ class MockGithubCommentsOlder:
4949
class MockGithubCommentsEmpty:
5050
"""Mocks Github Comment object with no comments."""
5151

52-
totalCount = 0
53-
reversed = [MockValue]
52+
# Simulate the bug from the PyGithub package, where
53+
# totalCount is non-zero when there are no comments.
54+
totalCount = int(2)
55+
reversed = []
5456

5557

5658
class MockPull:

test/github_tests/test_github.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ def test_get_last_comment_nothing(self, mock_lastcomment):
7575
# Call function
7676
response = GithubService().get_last_comment(mock_pr)
7777

78-
# Validate function calls and response
79-
mock_lastcomment.assert_not_called()
78+
# Validate function response
8079
self.assertIsNone(response)
8180

8281
@patch(PATH + "GithubService.check_request_state")

0 commit comments

Comments
 (0)