diff --git a/rhodecode/apps/repository/tests/test_repo_vcs_settings.py b/rhodecode/apps/repository/tests/test_repo_vcs_settings.py --- a/rhodecode/apps/repository/tests/test_repo_vcs_settings.py +++ b/rhodecode/apps/repository/tests/test_repo_vcs_settings.py @@ -47,6 +47,7 @@ class TestVcsSettings(object): 'extensions_evolve': False, 'phases_publish': 'False', 'rhodecode_pr_merge_enabled': False, + 'rhodecode_auto_merge_enabled': False, 'rhodecode_use_outdated_comments': False, 'new_svn_branch': '', 'new_svn_tag': '' @@ -58,7 +59,7 @@ class TestVcsSettings(object): response = self.app.get(route_path('edit_repo_vcs', repo_name=repo_name)) expected_settings = ( - 'rhodecode_use_outdated_comments', 'rhodecode_pr_merge_enabled', + 'rhodecode_use_outdated_comments', 'rhodecode_pr_merge_enabled', 'rhodecode_auto_merge_enabled', 'hooks_changegroup_repo_size', 'hooks_changegroup_push_logger', 'hooks_outgoing_pull_logger' ) diff --git a/rhodecode/config/middleware.py b/rhodecode/config/middleware.py --- a/rhodecode/config/middleware.py +++ b/rhodecode/config/middleware.py @@ -49,7 +49,7 @@ from rhodecode.lib.plugins.utils import from rhodecode.lib.utils2 import AttributeDict from rhodecode.lib.exc_tracking import store_exception, format_exc from rhodecode.subscribers import ( - scan_repositories_if_enabled, write_js_routes_if_enabled, + auto_merge_pr_if_needed, scan_repositories_if_enabled, write_js_routes_if_enabled, write_metadata_if_needed, write_usage_data, import_license_if_present) from rhodecode.lib.statsd_client import StatsdClient @@ -395,6 +395,7 @@ def includeme(config, auth_resources=Non # Add subscribers. if load_all: log.debug('Adding subscribers...') + config.add_subscriber(auto_merge_pr_if_needed, rhodecode.events.PullRequestReviewEvent) config.add_subscriber(scan_repositories_if_enabled, pyramid.events.ApplicationCreated) config.add_subscriber(write_metadata_if_needed, diff --git a/rhodecode/model/forms.py b/rhodecode/model/forms.py --- a/rhodecode/model/forms.py +++ b/rhodecode/model/forms.py @@ -445,6 +445,7 @@ class _BaseVcsSettingsForm(formencode.Sc # PR/Code-review rhodecode_pr_merge_enabled = v.StringBoolean(if_missing=False) + rhodecode_auto_merge_enabled = v.StringBoolean(if_missing=False) rhodecode_use_outdated_comments = v.StringBoolean(if_missing=False) # hg diff --git a/rhodecode/model/pull_request.py b/rhodecode/model/pull_request.py --- a/rhodecode/model/pull_request.py +++ b/rhodecode/model/pull_request.py @@ -2124,6 +2124,10 @@ class PullRequestModel(BaseModel): return self._get_general_setting( pull_request, 'rhodecode_pr_merge_enabled') + def is_automatic_merge_enabled(self, pull_request): + return self._get_general_setting( + pull_request, 'rhodecode_auto_merge_enabled') + def _use_rebase_for_merging(self, pull_request): repo_type = pull_request.target_repo.repo_type if repo_type == 'hg': diff --git a/rhodecode/model/settings.py b/rhodecode/model/settings.py --- a/rhodecode/model/settings.py +++ b/rhodecode/model/settings.py @@ -461,6 +461,7 @@ class VcsSettingsModel(object): GENERAL_SETTINGS = ( 'use_outdated_comments', 'pr_merge_enabled', + 'auto_merge_enabled', 'hg_use_rebase_for_merging', 'hg_close_branch_before_merging', 'git_use_rebase_for_merging', diff --git a/rhodecode/subscribers.py b/rhodecode/subscribers.py --- a/rhodecode/subscribers.py +++ b/rhodecode/subscribers.py @@ -57,6 +57,34 @@ def add_renderer_globals(event): event['h'] = helpers +def auto_merge_pr_if_needed(event): + from rhodecode.model.db import PullRequest + from rhodecode.model.pull_request import ( + PullRequestModel, ChangesetStatus, MergeCheck + ) + + pr_event_data = event.as_dict()['pullrequest'] + pull_request = PullRequest.get(pr_event_data['pull_request_id']) + calculated_status = pr_event_data['status'] + if (calculated_status == ChangesetStatus.STATUS_APPROVED + and PullRequestModel().is_automatic_merge_enabled(pull_request)): + user = pull_request.author.AuthUser() + + merge_check = MergeCheck.validate( + pull_request, user, translator=lambda x: x, fail_early=True + ) + if merge_check.merge_possible: + from rhodecode.lib.base import vcs_operation_context + extras = vcs_operation_context( + event.request.environ, repo_name=pull_request.target_repo.repo_name, + username=user.username, action='push', + scm=pull_request.target_repo.repo_type) + from rc_ee.lib.celerylib.tasks import auto_merge_repo + auto_merge_repo.apply_async( + args=(pull_request.pull_request_id, extras) + ) + + def set_user_lang(event): request = event.request cur_user = getattr(request, 'user', None) diff --git a/rhodecode/templates/base/vcs_settings.mako b/rhodecode/templates/base/vcs_settings.mako --- a/rhodecode/templates/base/vcs_settings.mako +++ b/rhodecode/templates/base/vcs_settings.mako @@ -210,6 +210,23 @@