From f069e0f0f9fa2ed2e48c2901b2376d18ba1fd802 Mon Sep 17 00:00:00 2001 From: Marcin Lulek Date: Mon, 25 Nov 2019 19:10:53 +0100 Subject: [PATCH] pull-requests: add merge check that detects WIP marker in title. This will prevent merges in such case. Usually WIP in title means unfinished task that needs still some work. This pattern is present in Gitlab/Github and is already quite common. --- rhodecode/model/db.py | 8 ++++++++ rhodecode/model/pull_request.py | 10 ++++++++++ rhodecode/tests/models/test_pullrequest.py | 16 +++++++++++++++- 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/rhodecode/model/db.py b/rhodecode/model/db.py index f1938eee..d1a6004c 100644 --- a/rhodecode/model/db.py +++ b/rhodecode/model/db.py @@ -4002,6 +4002,14 @@ class _PullRequestBase(BaseModel): def reviewer_data_json(self): return json.dumps(self.reviewer_data) + @property + def work_in_progress(self): + """checks if pull request is work in progress by checking the title""" + title = self.title.upper() + if re.match(r'^(\[WIP\]\s*|WIP:\s*|WIP\s+)', title): + return True + return False + @hybrid_property def description_safe(self): from rhodecode.lib import helpers as h diff --git a/rhodecode/model/pull_request.py b/rhodecode/model/pull_request.py index 23933455..b7dd3614 100644 --- a/rhodecode/model/pull_request.py +++ b/rhodecode/model/pull_request.py @@ -1614,6 +1614,7 @@ class MergeCheck(object): PERM_CHECK = 'perm' REVIEW_CHECK = 'review' MERGE_CHECK = 'merge' + WIP_CHECK = 'wip' def __init__(self): self.review_status = None @@ -1638,6 +1639,15 @@ class MergeCheck(object): _ = translator merge_check = cls() + # title has WIP: + if pull_request.work_in_progress: + log.debug("MergeCheck: cannot merge, title has wip: marker.") + + msg = _('WIP marker in title prevents from accidental merge.') + merge_check.push_error('error', msg, cls.WIP_CHECK, pull_request.title) + if fail_early: + return merge_check + # permissions to merge user_allowed_to_merge = PullRequestModel().check_user_merge( pull_request, auth_user) diff --git a/rhodecode/tests/models/test_pullrequest.py b/rhodecode/tests/models/test_pullrequest.py index 0dc4bd18..fcdf0fed 100644 --- a/rhodecode/tests/models/test_pullrequest.py +++ b/rhodecode/tests/models/test_pullrequest.py @@ -402,7 +402,7 @@ class TestPullRequestModel(object): assert pull_request.merge_rev is None def test_get_commit_ids(self, pull_request): - # The PR has been not merget yet, so expect an exception + # The PR has been not merged yet, so expect an exception with pytest.raises(ValueError): PullRequestModel()._get_commit_ids(pull_request) @@ -433,6 +433,20 @@ class TestPullRequestModel(object): ) assert type(title) == unicode + @pytest.mark.parametrize('title, has_wip', [ + ('hello', False), + ('hello wip', False), + ('hello wip: xxx', False), + ('[wip] hello', True), + ('[wip] hello', True), + ('wip: hello', True), + ('wip hello', True), + + ]) + def test_wip_title_marker(self, pull_request, title, has_wip): + pull_request.title = title + assert pull_request.work_in_progress == has_wip + @pytest.mark.usefixtures('config_stub') class TestIntegrationMerge(object):