Skip to content

Commit

Permalink
Fix out of range error from totalCount
Browse files Browse the repository at this point in the history
Signed-off-by: Luiz Carvalho <[email protected]>
  • Loading branch information
lcarva committed Jul 20, 2022
1 parent 3436ce9 commit d1fed8b
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 6 deletions.
12 changes: 10 additions & 2 deletions reviewrot/githubstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,19 @@ def get_last_comment(self, pr):
last_issue_comment = None
last_comment = None

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

if issue_comments.totalCount > 0:
try:
# issue_comments.totalCount may return a non-zero value when
# the list of comments is actually empty :(
last_issue_comment = issue_comments.reversed[0]
except IndexError:
pass

# check which is newer if pr has both types of comments
if last_issue_comment and last_review_comment:
Expand Down
6 changes: 4 additions & 2 deletions test/github_tests/mock_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,10 @@ class MockGithubCommentsOlder:
class MockGithubCommentsEmpty:
"""Mocks Github Comment object with no comments."""

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


class MockPull:
Expand Down
3 changes: 1 addition & 2 deletions test/github_tests/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,7 @@ def test_get_last_comment_nothing(self, mock_lastcomment):
# Call function
response = GithubService().get_last_comment(mock_pr)

# Validate function calls and response
mock_lastcomment.assert_not_called()
# Validate function response
self.assertIsNone(response)

@patch(PATH + "GithubService.check_request_state")
Expand Down

0 comments on commit d1fed8b

Please sign in to comment.