Skip to content

Commit

Permalink
Add support for Draft flag on GitHub
Browse files Browse the repository at this point in the history
  • Loading branch information
hluk committed Jun 22, 2022
1 parent 3436ce9 commit b8531d8
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 10 deletions.
15 changes: 6 additions & 9 deletions reviewrot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from reviewrot.gitlabstack import GitlabService
from reviewrot.pagurestack import PagureService
from reviewrot.phabricatorstack import PhabricatorService
from reviewrot.utils import is_wip
from six import iteritems
from six.moves import input
import yaml
Expand Down Expand Up @@ -421,12 +422,8 @@ def remove_wip(results):
res (list): list of BaseReview instances with WIP
reviews removed
"""
res = []
for result in results:
match = re.match(
r"^(\[WIP\]\s*|WIP:\s*|WIP\s+|Draft:)+\s*", str(result.title), re.IGNORECASE
)
if not match:
res.append(result)

return res
return [
result
for result in results
if not is_wip(str(result.title))
]
7 changes: 6 additions & 1 deletion reviewrot/githubstack.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from github import Github
from github.GithubException import UnknownObjectException
from reviewrot.basereview import BaseReview, BaseService, LastComment
from reviewrot.utils import is_wip

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -159,9 +160,13 @@ def get_reviews(self, uname, repo_name, age=None, show_last_comment=None):
)
continue

title = pr.title
if pr.draft and not is_wip(title):
title = f"WIP: {title}"

res = GithubReview(
user=pr.user.login,
title=pr.title,
title=title,
url=pr.html_url,
time=pr.created_at,
updated_time=pr.updated_at,
Expand Down
9 changes: 9 additions & 0 deletions reviewrot/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
"""Simple helper functions"""
import re

WIP_REGEX_PATTERN = r"^(\[WIP\]\s*|WIP:\s*|WIP\s+|Draft:)+\s*"


def is_wip(title):
match = re.match(WIP_REGEX_PATTERN, title, re.IGNORECASE)
return bool(match)
1 change: 1 addition & 0 deletions test/github_tests/mock_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class MockPull:
updated_at = "dummy_update"
review_comments = "dummy_comments"
comments = "dummy_comments"
draft = False


class MockRepo:
Expand Down
47 changes: 47 additions & 0 deletions test/github_tests/test_github.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,3 +370,50 @@ def test_request_reviews_without_repo(self, mock_get_reviews, mock_github_patch)
mock_github_instance.get_user.assert_called_with("dummy_user")
mock_github_patch.assert_called_with("dummy_token")
self.assertEqual(["1"], response)

@patch(PATH + "GithubService.check_request_state")
@patch(PATH + "GithubService.get_last_comment")
def test_get_reviews_with_draft_status(
self,
mock_last_comment,
mock_check_request_state,
):
"""
Tests get_reviews() function.
Where getting the pull requests returns single PR flagged as draft.
"""
# Set up mock return values and side effects
mock_uname = MagicMock()
mock_repo = MagicMock()
mock_pr = mock_github.MockPull()
mock_pr.draft = True
mock_repo.get_pulls.return_value = [mock_pr]
mock_uname.get_repo.return_value = mock_repo
mock_check_request_state.return_value = True

for title, expected_title in [
("dummy_title", "WIP: dummy_title"),
("WIP: dummy_title", "WIP: dummy_title"),
("Draft: dummy_title", "Draft: dummy_title")
]:
with self.subTest():
mock_pr.title = title

# Call function
response = GithubService().get_reviews(
uname=mock_uname,
repo_name="dummy_repo",
age=None,
show_last_comment=None
)

# Validate function calls and response
mock_uname.get_repo.assert_called_with("dummy_repo")
mock_check_request_state.assert_called_with(
"dummy_createdAt",
None,
)
self.assertEqual(1, len(response))
actual_review = response[0]
self.assertEqual(expected_title, actual_review.title)

0 comments on commit b8531d8

Please sign in to comment.