diff --git a/rhodecode/lib/middleware/simplevcs.py b/rhodecode/lib/middleware/simplevcs.py --- a/rhodecode/lib/middleware/simplevcs.py +++ b/rhodecode/lib/middleware/simplevcs.py @@ -26,6 +26,7 @@ It's implemented with basic auth functio import os import logging import importlib +import re from functools import wraps from paste.httpheaders import REMOTE_USER, AUTH_TYPE @@ -104,6 +105,31 @@ class SimpleVCS(object): auth_ret_code_detection) self.ip_addr = '0.0.0.0' + def set_repo_names(self, environ): + """ + This will populate the attributes acl_repo_name, url_repo_name and + vcs_repo_name on the current instance. + """ + # TODO: martinb: Move to class or module scope. + pr_regex = re.compile( + '(?P(?:[\w-]+)(?:/[\w-]+)*)/' + '(?P[\w-]+)' + '/pull-request/(?P\d+)/repository') + + self.url_repo_name = self._get_repository_name(environ) + match = pr_regex.match(self.url_repo_name) + + if match: + match_dict = match.groupdict() + self.acl_repo_name = '{base_name}/{repo_name}'.format(**match_dict) + self.vcs_repo_name = '{base_name}/.__shadow_{repo_name}_pr-{pr_id}'.format( + **match_dict) + self.pr_id = match_dict.get('pr_id') + else: + self.acl_repo_name = self.url_repo_name + self.vcs_repo_name = self.url_repo_name + self.pr_id = None + @property def repo_name(self): # TODO: johbo: Remove, switch to correct repo name attribute diff --git a/rhodecode/lib/middleware/vcs.py b/rhodecode/lib/middleware/vcs.py --- a/rhodecode/lib/middleware/vcs.py +++ b/rhodecode/lib/middleware/vcs.py @@ -175,52 +175,37 @@ class VCSMiddleware(object): # translate the _REPO_ID into real repo NAME for usage # in middleware environ['PATH_INFO'] = vcs_handler._get_by_id(environ['PATH_INFO']) - repo_name = vcs_handler._get_repository_name(environ) - acl_repo_name = repo_name - vcs_repo_name = repo_name - url_repo_name = repo_name - pr_id = None - - pr_regex = re.compile( - '(?P(?:[\w-]+)(?:/[\w-]+)*)/' - '(?P[\w-]+)' - '/pull-request/(?P\d+)/repository') - match = pr_regex.match(repo_name) - if match: - match_dict = match.groupdict() - pr_id = match_dict.get('pr_id') - acl_repo_name = '{base_name}/{repo_name}'.format(**match_dict) - vcs_repo_name = '{base_name}/.__shadow_{repo_name}_pr-{pr_id}'.format( - **match_dict) - + # Set repo names for permission checks, vcs and web interaction. + vcs_handler.set_repo_names(environ) log.debug('repo_names %s', { - 'acl_repo_name': acl_repo_name, - 'vcs_repo_name': vcs_repo_name, - 'url_repo_name': url_repo_name, + 'acl_repo_name': vcs_handler.acl_repo_name, + 'vcs_repo_name': vcs_handler.vcs_repo_name, + 'url_repo_name': vcs_handler.url_repo_name, }) - log.debug('pull_request %s', pr_id) + log.debug('pull_request %s', vcs_handler.pr_id) # check for type, presence in database and on filesystem if not vcs_handler.is_valid_and_existing_repo( - acl_repo_name, vcs_handler.basepath, vcs_handler.SCM): + vcs_handler.acl_repo_name, + vcs_handler.basepath, + vcs_handler.SCM): return HTTPNotFound()(environ, start_response) # TODO: johbo: Needed for the Pyro4 backend and Mercurial only. # Remove once we fully switched to the HTTP backend. - environ['REPO_NAME'] = url_repo_name + environ['REPO_NAME'] = vcs_handler.url_repo_name - # register repo_name and it's config back to the handler - vcs_handler.acl_repo_name = acl_repo_name - vcs_handler.url_repo_name = url_repo_name - vcs_handler.vcs_repo_name = vcs_repo_name - vcs_handler.pr_id = pr_id - vcs_handler.repo_vcs_config = self.vcs_config(acl_repo_name) + # register repo config back to the handler + vcs_handler.repo_vcs_config = self.vcs_config( + vcs_handler.acl_repo_name) + # Wrap handler in middlewares if they are enabled. vcs_handler = self.wrap_in_gzip_if_enabled( vcs_handler, self.config) vcs_handler, _ = wrap_in_appenlight_if_enabled( vcs_handler, self.config, self.appenlight_client) + return vcs_handler(environ, start_response) return self.application(environ, start_response)