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.
This commit is contained in:
Marcin Lulek 2019-11-25 19:10:53 +01:00
parent aa65b092ff
commit f069e0f0f9
3 changed files with 33 additions and 1 deletions

View file

@ -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

View file

@ -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)

View file

@ -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):