pull-requests: limit the ammount of data saved in default reviewers data for better memory usage

- fixes #5633
This commit is contained in:
Marcin Kuzminski 2020-10-07 11:26:24 +02:00
parent d724630710
commit e78b5fc1ee
6 changed files with 63 additions and 18 deletions

View file

@ -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, role=None, mandatory=False, rules=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': {},

View file

@ -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, DataGridAppView):
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, DataGridAppView):
# 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)

View file

@ -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()
review_data = get_default_reviewers_data(
current_user, source_repo, source_commit, target_repo, target_commit)
source_commit_id = request.GET['source_ref']
source_type = request.GET['source_ref_type']
source_name = request.GET['source_ref_name']
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_ref, target_repo, target_ref)
return review_data

View file

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

View file

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

View file

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