diff --git a/rhodecode/apps/repository/__init__.py b/rhodecode/apps/repository/__init__.py index e3aab48c..d2d08575 100644 --- a/rhodecode/apps/repository/__init__.py +++ b/rhodecode/apps/repository/__init__.py @@ -139,6 +139,17 @@ def includeme(config): name='repo_stats', pattern='/{repo_name:.*?[^/]}/repo_stats/{commit_id}', repo_route=True) + # Changelog + config.add_route( + name='repo_changelog', + pattern='/{repo_name:.*?[^/]}/changelog', repo_route=True) + config.add_route( + name='repo_changelog_file', + pattern='/{repo_name:.*?[^/]}/changelog/{commit_id}/{f_path:.*}', repo_route=True) + config.add_route( + name='repo_changelog_elements', + pattern='/{repo_name:.*?[^/]}/changelog_elements', repo_route=True) + # Tags config.add_route( name='tags_home', diff --git a/rhodecode/tests/functional/test_changelog.py b/rhodecode/apps/repository/tests/test_repo_changelog.py similarity index 65% rename from rhodecode/tests/functional/test_changelog.py rename to rhodecode/apps/repository/tests/test_repo_changelog.py index e7d21454..c43149e5 100644 --- a/rhodecode/tests/functional/test_changelog.py +++ b/rhodecode/apps/repository/tests/test_repo_changelog.py @@ -22,20 +22,32 @@ import re import pytest -from rhodecode.controllers.changelog import DEFAULT_CHANGELOG_SIZE -from rhodecode.tests import url, TestController -from rhodecode.tests.utils import AssertResponse - +from rhodecode.apps.repository.views.repo_changelog import DEFAULT_CHANGELOG_SIZE +from rhodecode.tests import TestController MATCH_HASH = re.compile(r'r(\d+):[\da-f]+') +def route_path(name, params=None, **kwargs): + import urllib + + base_url = { + 'repo_changelog':'/{repo_name}/changelog', + 'repo_changelog_file':'/{repo_name}/changelog/{commit_id}/{f_path}', + 'repo_changelog_elements':'/{repo_name}/changelog_elements', + }[name].format(**kwargs) + + if params: + base_url = '{}?{}'.format(base_url, urllib.urlencode(params)) + return base_url + + class TestChangelogController(TestController): - def test_index(self, backend): + def test_changelog(self, backend): self.log_user() - response = self.app.get(url(controller='changelog', action='index', - repo_name=backend.repo_name)) + response = self.app.get( + route_path('repo_changelog', repo_name=backend.repo_name)) first_idx = -1 last_idx = -DEFAULT_CHANGELOG_SIZE @@ -43,39 +55,30 @@ class TestChangelogController(TestController): response, first_idx, last_idx, backend) @pytest.mark.backends("hg", "git") - def test_index_filtered_by_branch(self, backend): + def test_changelog_filtered_by_branch(self, backend): self.log_user() self.app.get( - url( - controller='changelog', - action='index', - repo_name=backend.repo_name, - branch=backend.default_branch_name), + route_path('repo_changelog', repo_name=backend.repo_name, + params=dict(branch=backend.default_branch_name)), status=200) @pytest.mark.backends("svn") - def test_index_filtered_by_branch_svn(self, autologin_user, backend): + def test_changelog_filtered_by_branch_svn(self, autologin_user, backend): repo = backend['svn-simple-layout'] response = self.app.get( - url( - controller='changelog', - action='index', - repo_name=repo.repo_name, - branch='trunk'), + route_path('repo_changelog', repo_name=repo.repo_name, + params=dict(branch='trunk')), status=200) self.assert_commits_on_page( response, indexes=[15, 12, 7, 3, 2, 1]) - def test_index_filtered_by_wrong_branch(self, backend): + def test_changelog_filtered_by_wrong_branch(self, backend): self.log_user() branch = 'wrong-branch-name' response = self.app.get( - url( - controller='changelog', - action='index', - repo_name=backend.repo_name, - branch=branch), + route_path('repo_changelog', repo_name=backend.repo_name, + params=dict(branch=branch)), status=302) expected_url = '/{repo}/changelog/{branch}'.format( repo=backend.repo_name, branch=branch) @@ -89,7 +92,7 @@ class TestChangelogController(TestController): assert found_indexes == indexes @pytest.mark.xfail_backends("svn", reason="Depends on branch support") - def test_index_filtered_by_branch_with_merges( + def test_changelog_filtered_by_branch_with_merges( self, autologin_user, backend): # Note: The changelog of branch "b" does not contain the commit "a1" @@ -104,33 +107,27 @@ class TestChangelogController(TestController): backend.create_repo(commits) self.app.get( - url('changelog_home', - controller='changelog', - action='index', - repo_name=backend.repo_name, - branch='b'), + route_path('repo_changelog', repo_name=backend.repo_name, + params=dict(branch='b')), status=200) @pytest.mark.backends("hg") - def test_index_closed_branches(self, autologin_user, backend): + def test_changelog_closed_branches(self, autologin_user, backend): repo = backend['closed_branch'] response = self.app.get( - url( - controller='changelog', - action='index', - repo_name=repo.repo_name, - branch='experimental'), + route_path('repo_changelog', repo_name=repo.repo_name, + params=dict(branch='experimental')), status=200) self.assert_commits_on_page( response, indexes=[3, 1]) - def test_index_pagination(self, backend): + def test_changelog_pagination(self, backend): self.log_user() # pagination, walk up to page 6 - changelog_url = url( - controller='changelog', action='index', - repo_name=backend.repo_name) + changelog_url = route_path( + 'repo_changelog', repo_name=backend.repo_name) + for page in range(1, 7): response = self.app.get(changelog_url, {'page': page}) @@ -166,27 +163,33 @@ class TestChangelogController(TestController): first_commit_of_next_page.idx, first_commit_of_next_page.short_id) assert first_span_of_next_page not in response - def test_index_with_filenode(self, backend): + @pytest.mark.parametrize('test_path', [ + 'vcs/exceptions.py', + '/vcs/exceptions.py', + '//vcs/exceptions.py' + ]) + def test_changelog_with_filenode(self, backend, test_path): self.log_user() - response = self.app.get(url( - controller='changelog', action='index', revision='tip', - f_path='/vcs/exceptions.py', repo_name=backend.repo_name)) + response = self.app.get( + route_path('repo_changelog_file', repo_name=backend.repo_name, + commit_id='tip', f_path=test_path), + ) # history commits messages response.mustcontain('Added exceptions module, this time for real') response.mustcontain('Added not implemented hg backend test case') response.mustcontain('Added BaseChangeset class') - def test_index_with_filenode_that_is_dirnode(self, backend): + def test_changelog_with_filenode_that_is_dirnode(self, backend): self.log_user() - response = self.app.get(url(controller='changelog', action='index', - revision='tip', f_path='/tests', - repo_name=backend.repo_name)) - assert response.status == '302 Found' + self.app.get( + route_path('repo_changelog_file', repo_name=backend.repo_name, + commit_id='tip', f_path='/tests'), + status=302) - def test_index_with_filenode_not_existing(self, backend): + def test_changelog_with_filenode_not_existing(self, backend): self.log_user() - response = self.app.get(url(controller='changelog', action='index', - revision='tip', f_path='/wrong_path', - repo_name=backend.repo_name)) - assert response.status == '302 Found' + self.app.get( + route_path('repo_changelog_file', repo_name=backend.repo_name, + commit_id='tip', f_path='wrong_path'), + status=302) diff --git a/rhodecode/controllers/changelog.py b/rhodecode/apps/repository/views/repo_changelog.py similarity index 51% rename from rhodecode/controllers/changelog.py rename to rhodecode/apps/repository/views/repo_changelog.py index cf057e6f..3897099a 100644 --- a/rhodecode/controllers/changelog.py +++ b/rhodecode/apps/repository/views/repo_changelog.py @@ -18,21 +18,19 @@ # RhodeCode Enterprise Edition, including its added features, Support services, # and proprietary license terms, please see https://rhodecode.com/licenses/ -""" -changelog controller for rhodecode -""" import logging -from pylons import request, url, session, tmpl_context as c -from pylons.controllers.util import redirect -from pylons.i18n.translation import _ -from webob.exc import HTTPNotFound, HTTPBadRequest +from pyramid.httpexceptions import HTTPNotFound, HTTPFound +from pyramid.view import view_config +from pyramid.renderers import render +from pyramid.response import Response +from rhodecode.apps._base import RepoAppView import rhodecode.lib.helpers as h from rhodecode.lib.auth import ( - LoginRequired, HasRepoPermissionAnyDecorator, XHRRequired) -from rhodecode.lib.base import BaseRepoController, render + LoginRequired, HasRepoPermissionAnyDecorator) + from rhodecode.lib.ext_json import json from rhodecode.lib.graphmod import _colored, _dagwalker from rhodecode.lib.helpers import RepoPage @@ -46,35 +44,36 @@ log = logging.getLogger(__name__) DEFAULT_CHANGELOG_SIZE = 20 -class ChangelogController(BaseRepoController): +class RepoChangelogView(RepoAppView): - def __before__(self): - super(ChangelogController, self).__before__() - c.affected_files_cut_off = 60 - - def __get_commit_or_redirect( - self, commit_id, repo, redirect_after=True, partial=False): + def _get_commit_or_redirect(self, commit_id, redirect_after=True): """ - This is a safe way to get a commit. If an error occurs it - redirects to a commit with a proper message. If partial is set - then it does not do redirect raise and throws an exception instead. + This is a safe way to get commit. If an error occurs it redirects to + tip with proper message - :param commit_id: commit to fetch - :param repo: repo instance + :param commit_id: id of commit to fetch + :param redirect_after: toggle redirection """ + _ = self.request.translate + try: - return c.rhodecode_repo.get_commit(commit_id) + return self.rhodecode_vcs_repo.get_commit(commit_id) except EmptyRepositoryError: if not redirect_after: return None - h.flash(_('There are no commits yet'), category='warning') - redirect(url('changelog_home', repo_name=repo.repo_name)) + + h.flash(h.literal( + _('There are no commits yet')), category='warning') + raise HTTPFound( + h.route_path('repo_summary', repo_name=self.db_repo_name)) + + except (CommitDoesNotExistError, LookupError): + msg = _('No such commit exists for this repository') + h.flash(msg, category='error') + raise HTTPNotFound() except RepositoryError as e: - log.exception(safe_str(e)) - h.flash(safe_str(h.escape(e)), category='warning') - if not partial: - redirect(h.url('changelog_home', repo_name=repo.repo_name)) - raise HTTPBadRequest() + h.flash(safe_str(h.escape(e)), category='error') + raise HTTPNotFound() def _graph(self, repo, commits, prev_data=None, next_data=None): """ @@ -110,131 +109,176 @@ class ChangelogController(BaseRepoController): return json.dumps(data), json.dumps(current) def _check_if_valid_branch(self, branch_name, repo_name, f_path): - if branch_name not in c.rhodecode_repo.branches_all: + if branch_name not in self.rhodecode_vcs_repo.branches_all: h.flash('Branch {} is not found.'.format(h.escape(branch_name)), category='warning') - redirect(url('changelog_file_home', repo_name=repo_name, - revision=branch_name, f_path=f_path or '')) + redirect_url = h.route_path( + 'repo_changelog_file', repo_name=repo_name, + commit_id=branch_name, f_path=f_path or '') + raise HTTPFound(redirect_url) + + def _load_changelog_data( + self, c, collection, page, chunk_size, branch_name=None, + dynamic=False): + + def url_generator(**kw): + query_params = {} + query_params.update(kw) + return h.route_path( + 'repo_changelog', + repo_name=c.rhodecode_db_repo.repo_name, _query=query_params) - def _load_changelog_data(self, collection, page, chunk_size, branch_name=None, dynamic=False): c.total_cs = len(collection) c.showing_commits = min(chunk_size, c.total_cs) c.pagination = RepoPage(collection, page=page, item_count=c.total_cs, - items_per_page=chunk_size, branch=branch_name) + items_per_page=chunk_size, branch=branch_name, + url=url_generator) c.next_page = c.pagination.next_page c.prev_page = c.pagination.previous_page if dynamic: - if request.GET.get('chunk') != 'next': + if self.request.GET.get('chunk') != 'next': c.next_page = None - if request.GET.get('chunk') != 'prev': + if self.request.GET.get('chunk') != 'prev': c.prev_page = None page_commit_ids = [x.raw_id for x in c.pagination] c.comments = c.rhodecode_db_repo.get_comments(page_commit_ids) c.statuses = c.rhodecode_db_repo.statuses(page_commit_ids) + def load_default_context(self): + c = self._get_local_tmpl_context(include_app_defaults=True) + + # TODO(marcink): remove repo_info and use c.rhodecode_db_repo instead + c.repo_info = self.db_repo + c.rhodecode_repo = self.rhodecode_vcs_repo + + self._register_global_c(c) + return c + @LoginRequired() - @HasRepoPermissionAnyDecorator('repository.read', 'repository.write', - 'repository.admin') - def index(self, repo_name, revision=None, f_path=None): - commit_id = revision + @HasRepoPermissionAnyDecorator( + 'repository.read', 'repository.write', 'repository.admin') + @view_config( + route_name='repo_changelog', request_method='GET', + renderer='rhodecode:templates/changelog/changelog.mako') + @view_config( + route_name='repo_changelog_file', request_method='GET', + renderer='rhodecode:templates/changelog/changelog.mako') + def repo_changelog(self): + c = self.load_default_context() + + commit_id = self.request.matchdict.get('commit_id') + f_path = self._get_f_path(self.request.matchdict) + chunk_size = 20 - c.branch_name = branch_name = request.GET.get('branch', None) - c.book_name = book_name = request.GET.get('bookmark', None) - hist_limit = safe_int(request.GET.get('limit')) or None + c.branch_name = branch_name = self.request.GET.get('branch') or '' + c.book_name = book_name = self.request.GET.get('bookmark') or '' + hist_limit = safe_int(self.request.GET.get('limit')) or None - p = safe_int(request.GET.get('page', 1), 1) + p = safe_int(self.request.GET.get('page', 1), 1) c.selected_name = branch_name or book_name if not commit_id and branch_name: - self._check_if_valid_branch(branch_name, repo_name, f_path) + self._check_if_valid_branch(branch_name, self.db_repo_name, f_path) c.changelog_for_path = f_path pre_load = ['author', 'branch', 'date', 'message', 'parents'] commit_ids = [] + partial_xhr = self.request.environ.get('HTTP_X_PARTIAL_XHR') + try: if f_path: log.debug('generating changelog for path %s', f_path) # get the history for the file ! - base_commit = c.rhodecode_repo.get_commit(revision) + base_commit = self.rhodecode_vcs_repo.get_commit(commit_id) try: collection = base_commit.get_file_history( f_path, limit=hist_limit, pre_load=pre_load) - if (collection - and request.environ.get('HTTP_X_PARTIAL_XHR')): + if collection and partial_xhr: # for ajax call we remove first one since we're looking # at it right now in the context of a file commit collection.pop(0) except (NodeDoesNotExistError, CommitError): # this node is not present at tip! try: - commit = self.__get_commit_or_redirect( - commit_id, repo_name) + commit = self._get_commit_or_redirect(commit_id) collection = commit.get_file_history(f_path) except RepositoryError as e: h.flash(safe_str(e), category='warning') - redirect(h.url('changelog_home', repo_name=repo_name)) + redirect_url = h.route_path( + 'repo_changelog', repo_name=self.db_repo_name) + raise HTTPFound(redirect_url) collection = list(reversed(collection)) else: - collection = c.rhodecode_repo.get_commits( + collection = self.rhodecode_vcs_repo.get_commits( branch_name=branch_name, pre_load=pre_load) self._load_changelog_data( - collection, p, chunk_size, c.branch_name, dynamic=f_path) + c, collection, p, chunk_size, c.branch_name, dynamic=f_path) except EmptyRepositoryError as e: h.flash(safe_str(h.escape(e)), category='warning') - return redirect(h.route_path('repo_summary', repo_name=repo_name)) + raise HTTPFound( + h.route_path('repo_summary', repo_name=self.db_repo_name)) except (RepositoryError, CommitDoesNotExistError, Exception) as e: log.exception(safe_str(e)) h.flash(safe_str(h.escape(e)), category='error') - return redirect(url('changelog_home', repo_name=repo_name)) + raise HTTPFound( + h.route_path('repo_changelog', repo_name=self.db_repo_name)) - if (request.environ.get('HTTP_X_PARTIAL_XHR') - or request.environ.get('HTTP_X_PJAX')): + if partial_xhr or self.request.environ.get('HTTP_X_PJAX'): # loading from ajax, we don't want the first result, it's popped - return render('changelog/changelog_file_history.mako') + # in the code above + html = render( + 'rhodecode:templates/changelog/changelog_file_history.mako', + self._get_template_context(c), self.request) + return Response(html) if not f_path: commit_ids = c.pagination c.graph_data, c.graph_commits = self._graph( - c.rhodecode_repo, commit_ids) + self.rhodecode_vcs_repo, commit_ids) - return render('changelog/changelog.mako') + return self._get_template_context(c) @LoginRequired() - @XHRRequired() @HasRepoPermissionAnyDecorator( 'repository.read', 'repository.write', 'repository.admin') - def changelog_elements(self, repo_name): - commit_id = None + @view_config( + route_name='repo_changelog_elements', request_method=('GET', 'POST'), + renderer='rhodecode:templates/changelog/changelog_elements.mako', + xhr=True) + def repo_changelog_elements(self): + c = self.load_default_context() chunk_size = 20 def wrap_for_error(err): - return '