# HG changeset patch # User Serhii Ilin # Date 2024-10-28 10:35:44 # Node ID 027566d56f7591c74ac923c85c16729652bfef09 # Parent 06e00555ff2b84f8b693fb36cbc787784cad179e feat(task-for-automatic-pr-merge): added f-ty to support pull request auto merge for EE. Fixes: RCCE-67 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 @@ -48,6 +48,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': '' @@ -59,7 +60,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 @@ -392,6 +392,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 @@ -2125,6 +2125,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 @@
${_('Note: when this feature is enabled, it only runs hooks defined in the rcextension package. Custom hooks added on the Admin -> Settings -> Hooks page will not be run when pull requests are automatically merged from the web interface.')}
+ %if c.rhodecode_edition_id != 'EE': +
+ + +
+
+ ${_('This feature is available in RhodeCode EE edition only. Contact {sales_email} to obtain a trial license.').format(sales_email='sales@rhodecode.com')|n} +
+ %else: +
+ ${h.checkbox('rhodecode_auto_merge_enabled' + suffix, 'True', **kwargs)} + +
+
+ ${_('When this is enabled, the pull request will be merged once it has at least one reviewer and is approved.')} +
+ %endif
${h.checkbox('rhodecode_use_outdated_comments' + suffix, 'True', **kwargs)} diff --git a/rhodecode/tests/models/settings/test_vcs_settings.py b/rhodecode/tests/models/settings/test_vcs_settings.py --- a/rhodecode/tests/models/settings/test_vcs_settings.py +++ b/rhodecode/tests/models/settings/test_vcs_settings.py @@ -38,6 +38,7 @@ SVN_FORM_DATA = { GENERAL_FORM_DATA = { 'rhodecode_pr_merge_enabled': True, + 'rhodecode_auto_merge_enabled': True, 'rhodecode_use_outdated_comments': True, 'rhodecode_hg_use_rebase_for_merging': True, 'rhodecode_hg_close_branch_before_merging': True, @@ -989,6 +990,7 @@ class TestCreateOrUpdateRepoSettings(obj 'vcs_git_lfs_enabled': 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': ''