# HG changeset patch # User Marcin Kuzminski # Date 2020-10-07 09:26:24 # Node ID b5299f6ded6a6201fce67f03369db52de0016fb1 # Parent 2365f5e77e4797f4af09ced7f850f6c3f40d819e pull-requests: limit the ammount of data saved in default reviewers data for better memory usage - fixes #5633 diff --git a/rhodecode/apps/repository/utils.py b/rhodecode/apps/repository/utils.py --- a/rhodecode/apps/repository/utils.py +++ b/rhodecode/apps/repository/utils.py @@ -18,7 +18,7 @@ # RhodeCode Enterprise Edition, including its added features, Support services, # and proprietary license terms, please see https://rhodecode.com/licenses/ -from rhodecode.lib import helpers as h +from rhodecode.lib import helpers as h, rc_cache from rhodecode.lib.utils2 import safe_int from rhodecode.model.pull_request import get_diff_info from rhodecode.model.db import PullRequestReviewers @@ -54,20 +54,36 @@ def reviewer_as_json(user, reasons=None, } -def get_default_reviewers_data(current_user, source_repo, source_commit, target_repo, target_commit): +def to_reviewers(e): + if isinstance(e, (tuple, list)): + return map(reviewer_as_json, e) + else: + return reviewer_as_json(e) + + +def get_default_reviewers_data(current_user, source_repo, source_ref, target_repo, target_ref, + include_diff_info=True): """ Return json for default reviewers of a repository """ - diff_info = get_diff_info( - source_repo, source_commit.raw_id, target_repo, target_commit.raw_id) + diff_info = {} + if include_diff_info: + diff_info = get_diff_info( + source_repo, source_ref.commit_id, target_repo, target_ref.commit_id) reasons = ['Default reviewer', 'Repository owner'] json_reviewers = [reviewer_as_json( user=target_repo.user, reasons=reasons, mandatory=False, rules=None, role=None)] + compute_key = rc_cache.utils.compute_key_from_params( + current_user.user_id, source_repo.repo_id, source_ref.type, source_ref.name, + source_ref.commit_id, target_repo.repo_id, target_ref.type, target_ref.name, + target_ref.commit_id) + return { 'api_ver': REVIEWER_API_VERSION, # define version for later possible schema upgrade + 'compute_key': compute_key, 'diff_info': diff_info, 'reviewers': json_reviewers, 'rules': {}, diff --git a/rhodecode/apps/repository/views/repo_pull_requests.py b/rhodecode/apps/repository/views/repo_pull_requests.py --- a/rhodecode/apps/repository/views/repo_pull_requests.py +++ b/rhodecode/apps/repository/views/repo_pull_requests.py @@ -40,7 +40,7 @@ from rhodecode.lib.auth import ( LoginRequired, HasRepoPermissionAny, HasRepoPermissionAnyDecorator, NotAnonymous, CSRFRequired) from rhodecode.lib.utils2 import str2bool, safe_str, safe_unicode, safe_int, aslist -from rhodecode.lib.vcs.backends.base import EmptyCommit, UpdateFailureReason +from rhodecode.lib.vcs.backends.base import EmptyCommit, UpdateFailureReason, Reference from rhodecode.lib.vcs.exceptions import ( CommitDoesNotExistError, RepositoryRequirementError, EmptyRepositoryError) from rhodecode.model.changeset_status import ChangesetStatusModel @@ -1150,8 +1150,9 @@ class RepoPullRequestsView(RepoAppView, ancestor = source_scm.get_common_ancestor( source_commit.raw_id, target_commit.raw_id, target_scm) + source_ref_type, source_ref_name, source_commit_id = _form['target_ref'].split(':') + target_ref_type, target_ref_name, target_commit_id = _form['source_ref'].split(':') # recalculate target ref based on ancestor - target_ref_type, target_ref_name, __ = _form['target_ref'].split(':') target_ref = ':'.join((target_ref_type, target_ref_name, ancestor)) get_default_reviewers_data, validate_default_reviewers, validate_observers = \ @@ -1159,8 +1160,12 @@ class RepoPullRequestsView(RepoAppView, # recalculate reviewers logic, to make sure we can validate this reviewer_rules = get_default_reviewers_data( - self._rhodecode_db_user, source_db_repo, - source_commit, target_db_repo, target_commit) + self._rhodecode_db_user, + source_db_repo, + Reference(source_ref_type, source_ref_name, source_commit_id), + target_db_repo, + Reference(target_ref_type, target_ref_name, target_commit_id), + include_diff_info=False) reviewers = validate_default_reviewers(_form['review_members'], reviewer_rules) observers = validate_observers(_form['observer_members'], reviewer_rules) diff --git a/rhodecode/apps/repository/views/repo_review_rules.py b/rhodecode/apps/repository/views/repo_review_rules.py --- a/rhodecode/apps/repository/views/repo_review_rules.py +++ b/rhodecode/apps/repository/views/repo_review_rules.py @@ -25,6 +25,7 @@ from pyramid.view import view_config from rhodecode.apps._base import RepoAppView from rhodecode.apps.repository.utils import get_default_reviewers_data from rhodecode.lib.auth import LoginRequired, HasRepoPermissionAnyDecorator +from rhodecode.lib.vcs.backends.base import Reference from rhodecode.model.db import Repository log = logging.getLogger(__name__) @@ -61,13 +62,19 @@ class RepoReviewRulesView(RepoAppView): target_repo_name = request.GET.get('target_repo', source_repo_name) target_repo = Repository.get_by_repo_name(target_repo_name) - source_ref = request.GET['source_ref'] - target_ref = request.GET['target_ref'] - source_commit = source_repo.get_commit(source_ref) - target_commit = target_repo.get_commit(target_ref) + current_user = request.user.get_instance() + + source_commit_id = request.GET['source_ref'] + source_type = request.GET['source_ref_type'] + source_name = request.GET['source_ref_name'] - current_user = request.user.get_instance() + target_commit_id = request.GET['target_ref'] + target_type = request.GET['target_ref_type'] + target_name = request.GET['target_ref_name'] + + source_ref = Reference(source_type, source_name, source_commit_id) + target_ref = Reference(target_type, target_name, target_commit_id) + review_data = get_default_reviewers_data( - current_user, source_repo, source_commit, target_repo, target_commit) - + current_user, source_repo, source_ref, target_repo, target_ref) return review_data diff --git a/rhodecode/lib/vcs/backends/base.py b/rhodecode/lib/vcs/backends/base.py --- a/rhodecode/lib/vcs/backends/base.py +++ b/rhodecode/lib/vcs/backends/base.py @@ -57,7 +57,20 @@ FILEMODE_DEFAULT = 0o100644 FILEMODE_EXECUTABLE = 0o100755 EMPTY_COMMIT_ID = '0' * 40 -Reference = collections.namedtuple('Reference', ('type', 'name', 'commit_id')) +_Reference = collections.namedtuple('Reference', ('type', 'name', 'commit_id')) + + +class Reference(_Reference): + + @property + def branch(self): + if self.type == 'branch': + return self.name + + @property + def bookmark(self): + if self.type == 'book': + return self.name class MergeFailureReason(object): 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 @@ -710,7 +710,7 @@ class PullRequestModel(BaseModel): MergeCheck.validate( pull_request, auth_user=auth_user, translator=translator) - self.notify_reviewers(pull_request, reviewer_ids) + self.notify_reviewers(pull_request, reviewer_ids, created_by_user) self.trigger_pull_request_hook(pull_request, created_by_user, 'create') creation_data = pull_request.get_api_data(with_merge_state=False) diff --git a/rhodecode/public/js/src/rhodecode/pullrequests.js b/rhodecode/public/js/src/rhodecode/pullrequests.js --- a/rhodecode/public/js/src/rhodecode/pullrequests.js +++ b/rhodecode/public/js/src/rhodecode/pullrequests.js @@ -313,9 +313,13 @@ window.ReviewersController = function () { 'repo_name': templateContext.repo_name, 'source_repo': sourceRepo, + 'source_ref_type': sourceRef[0], + 'source_ref_name': sourceRef[1], 'source_ref': sourceRef[2], 'target_repo': targetRepo, - 'target_ref': targetRef[2] + 'target_ref': targetRef[2], + 'target_ref_type': sourceRef[0], + 'target_ref_name': sourceRef[1] }); self.currentRequest = $.ajax({