events: expose permalink urls for different set of object.

- permalink url should provide a non-changeable url that can be used in
external sources with guarantee of beeing always valid.
This commit is contained in:
Marcin Kuzminski 2017-06-08 16:11:33 +02:00
parent 36f3dd22fb
commit 47f9f32ad9
10 changed files with 107 additions and 38 deletions

View file

@ -32,6 +32,11 @@ def includeme(config):
name='repo_summary_commits',
pattern='/{repo_name:.*?[^/]}/summary-commits', repo_route=True)
# repo commits
config.add_route(
name='repo_commit',
pattern='/{repo_name:.*?[^/]}/changeset/{commit_id}', repo_route=True)
# refs data
config.add_route(
name='repo_refs_data',

View file

@ -52,6 +52,8 @@ class PullRequestEvent(RepoEvent):
'issues': issues,
'pull_request_id': self.pullrequest.pull_request_id,
'url': PullRequestModel().get_url(self.pullrequest),
'permalink_url': PullRequestModel().get_url(
self.pullrequest, permalink=True),
'status': self.pullrequest.calculated_review_status(),
'commits': commits,
}
@ -131,7 +133,9 @@ class PullRequestCommentEvent(PullRequestEvent):
'type': self.comment.comment_type,
'file': self.comment.f_path,
'line': self.comment.line_no,
'url': CommentsModel().get_url(self.comment)
'url': CommentsModel().get_url(self.comment),
'permalink_url': CommentsModel().get_url(
self.comment, permalink=True),
}
})
return data

View file

@ -35,9 +35,9 @@ def _commits_as_dict(commit_ids, repos):
:param repos: list of repos to check
"""
from rhodecode.lib.utils2 import extract_mentioned_users
from rhodecode.lib import helpers as h
from rhodecode.lib.helpers import (
urlify_commit_message, process_patterns, chop_at_smart)
from rhodecode.model.repo import RepoModel
if not repos:
raise Exception('no repo defined')
@ -54,7 +54,7 @@ def _commits_as_dict(commit_ids, repos):
reviewers = []
for repo in repos:
if not needed_commits:
return commits # return early if we have the commits we need
return commits # return early if we have the commits we need
vcs_repo = repo.scm_instance(cache=False)
try:
@ -63,16 +63,15 @@ def _commits_as_dict(commit_ids, repos):
try:
cs = vcs_repo.get_changeset(commit_id)
except CommitDoesNotExistError:
continue # maybe its in next repo
continue # maybe its in next repo
cs_data = cs.__json__()
cs_data['mentions'] = extract_mentioned_users(cs_data['message'])
cs_data['reviewers'] = reviewers
cs_data['url'] = h.url('changeset_home',
repo_name=repo.repo_name,
revision=cs_data['raw_id'],
qualified=True
)
cs_data['url'] = RepoModel().get_commit_url(
repo, cs_data['raw_id'])
cs_data['permalink_url'] = RepoModel().get_commit_url(
repo, cs_data['raw_id'], permalink=True)
urlified_message, issues_data = process_patterns(
cs_data['message'], repo.repo_name)
cs_data['issues'] = issues_data
@ -130,6 +129,7 @@ class RepoEvent(RhodecodeEvent):
'repo_name': self.repo.repo_name,
'repo_type': self.repo.repo_type,
'url': RepoModel().get_url(self.repo),
'permalink_url': RepoModel().get_url(self.repo, permalink=True),
'extra_fields': extra_fields
}
})
@ -242,7 +242,10 @@ class RepoPushEvent(RepoVCSEvent):
def as_dict(self):
data = super(RepoPushEvent, self).as_dict()
branch_url = repo_url = data['repo']['url']
def branch_url(branch_name):
return '{}/changelog?branch={}'.format(
data['repo']['url'], branch_name)
commits = _commits_as_dict(
commit_ids=self.pushed_commit_ids, repos=[self.repo])
@ -258,8 +261,7 @@ class RepoPushEvent(RepoVCSEvent):
branches = [
{
'name': branch,
'url': '{}/changelog?branch={}'.format(
data['repo']['url'], branch)
'url': branch_url(branch)
}
for branch in branches
]

View file

@ -29,14 +29,14 @@ import collections
from datetime import datetime
from pylons.i18n.translation import _
from pyramid.threadlocal import get_current_registry
from pyramid.threadlocal import get_current_registry, get_current_request
from sqlalchemy.sql.expression import null
from sqlalchemy.sql.functions import coalesce
from rhodecode.lib import helpers as h, diffs
from rhodecode.lib.channelstream import channelstream_request
from rhodecode.lib.utils import action_logger
from rhodecode.lib.utils2 import extract_mentioned_users
from rhodecode.lib.utils2 import extract_mentioned_users, safe_str
from rhodecode.model import BaseModel
from rhodecode.model.db import (
ChangesetComment, User, Notification, PullRequest, AttributeDict)
@ -409,22 +409,40 @@ class CommentsModel(BaseModel):
q = q.order_by(ChangesetComment.created_on)
return q.all()
def get_url(self, comment):
def get_url(self, comment, request=None, permalink=False):
if not request:
request = get_current_request()
comment = self.__get_commit_comment(comment)
if comment.pull_request:
return h.url(
'pullrequest_show',
repo_name=comment.pull_request.target_repo.repo_name,
pull_request_id=comment.pull_request.pull_request_id,
anchor='comment-%s' % comment.comment_id,
qualified=True,)
pull_request = comment.pull_request
if permalink:
return request.route_url(
'pull_requests_global',
pull_request_id=pull_request.pull_request_id,
_anchor='comment-%s' % comment.comment_id)
else:
return request.route_url(
'pullrequest_show',
repo_name=safe_str(pull_request.target_repo.repo_name),
pull_request_id=pull_request.pull_request_id,
_anchor='comment-%s' % comment.comment_id)
else:
return h.url(
'changeset_home',
repo_name=comment.repo.repo_name,
revision=comment.revision,
anchor='comment-%s' % comment.comment_id,
qualified=True,)
repo = comment.repo
commit_id = comment.revision
if permalink:
return request.route_url(
'repo_commit', repo_name=safe_str(repo.repo_id),
commit_id=commit_id,
_anchor='comment-%s' % comment.comment_id)
else:
return request.route_url(
'repo_commit', repo_name=safe_str(repo.repo_name),
commit_id=commit_id,
_anchor='comment-%s' % comment.comment_id)
def get_comments(self, repo_id, revision=None, pull_request=None):
"""

View file

@ -31,6 +31,7 @@ import urllib
from pylons.i18n.translation import _
from pylons.i18n.translation import lazy_ugettext
from pyramid.threadlocal import get_current_request
from sqlalchemy import or_
from rhodecode.lib import helpers as h, hooks_utils, diffs
@ -961,11 +962,19 @@ class PullRequestModel(BaseModel):
self.notify_reviewers(pull_request, ids_to_add)
return ids_to_add, ids_to_remove
def get_url(self, pull_request):
return h.url('pullrequest_show',
repo_name=safe_str(pull_request.target_repo.repo_name),
pull_request_id=pull_request.pull_request_id,
qualified=True)
def get_url(self, pull_request, request=None, permalink=False):
if not request:
request = get_current_request()
if permalink:
return request.route_url(
'pull_requests_global',
pull_request_id=pull_request.pull_request_id,)
else:
return request.route_url(
'pullrequest_show',
repo_name=safe_str(pull_request.target_repo.repo_name),
pull_request_id=pull_request.pull_request_id,)
def get_shadow_clone_url(self, pull_request):
"""

View file

@ -155,10 +155,30 @@ class RepoModel(BaseModel):
repos = Repository.query().filter(Repository.group == root).all()
return repos
def get_url(self, repo, request=None):
def get_url(self, repo, request=None, permalink=False):
if not request:
request = get_current_request()
return request.route_url('repo_summary', repo_name=safe_str(repo.repo_name))
if permalink:
return request.route_url(
'repo_summary', repo_name=safe_str(repo.repo_id))
else:
return request.route_url(
'repo_summary', repo_name=safe_str(repo.repo_name))
def get_commit_url(self, repo, commit_id, request=None, permalink=False):
if not request:
request = get_current_request()
if permalink:
return request.route_url(
'repo_commit', repo_name=safe_str(repo.repo_id),
commit_id=commit_id)
else:
return request.route_url(
'repo_commit', repo_name=safe_str(repo.repo_name),
commit_id=commit_id)
@classmethod
def update_repoinfo(cls, repositories=None):

View file

@ -50,6 +50,7 @@ def test_pullrequest_events_serialized(EventClass, pr_util, config_stub):
assert data['repo']['repo_name'] == pr.target_repo.repo_name
assert data['pullrequest']['pull_request_id'] == pr.pull_request_id
assert data['pullrequest']['url']
assert data['pullrequest']['permalink_url']
@pytest.mark.backends("git", "hg")
@ -71,6 +72,7 @@ def test_pullrequest_comment_events_serialized(pr_util, config_stub):
assert data['repo']['repo_name'] == pr.target_repo.repo_name
assert data['pullrequest']['pull_request_id'] == pr.pull_request_id
assert data['pullrequest']['url']
assert data['pullrequest']['permalink_url']
assert data['comment']['text'] == comment.text

View file

@ -62,6 +62,7 @@ def test_repo_events_serialized(config_stub, repo_stub, EventClass):
assert data['name'] == EventClass.name
assert data['repo']['repo_name'] == repo_stub.repo_name
assert data['repo']['url']
assert data['repo']['permalink_url']
@pytest.mark.parametrize('EventClass', [
@ -73,6 +74,7 @@ def test_vcs_repo_events_serialize(config_stub, repo_stub, scm_extras, EventClas
assert data['name'] == EventClass.name
assert data['repo']['repo_name'] == repo_stub.repo_name
assert data['repo']['url']
assert data['repo']['permalink_url']
@pytest.mark.parametrize('EventClass', [RepoPushEvent])
@ -84,6 +86,7 @@ def test_vcs_repo_push_event_serialize(config_stub, repo_stub, scm_extras, Event
assert data['name'] == EventClass.name
assert data['repo']['repo_name'] == repo_stub.repo_name
assert data['repo']['url']
assert data['repo']['permalink_url']
def test_create_delete_repo_fires_events(backend):

View file

@ -964,11 +964,9 @@ class TestPullrequestsController(object):
assert target.text.strip() == 'tag: target'
assert target.getchildren() == []
@pytest.mark.parametrize('mergeable', [True, False])
def test_shadow_repository_link(
self, mergeable, pr_util, http_host_stub):
self, mergeable, pr_util, http_host_only_stub):
"""
Check that the pull request summary page displays a link to the shadow
repository if the pull request is mergeable. If it is not mergeable
@ -979,7 +977,7 @@ class TestPullrequestsController(object):
target_repo = pull_request.target_repo.scm_instance()
pr_id = pull_request.pull_request_id
shadow_url = '{host}/{repo}/pull-request/{pr_id}/repository'.format(
host=http_host_stub, repo=target_repo.name, pr_id=pr_id)
host=http_host_only_stub, repo=target_repo.name, pr_id=pr_id)
response = self.app.get(url(
controller='pullrequests', action='show',

View file

@ -412,4 +412,12 @@ def add_test_routes(config):
"""
config.add_route(name='home', pattern='/')
config.add_route(name='repo_summary', pattern='/{repo_name}')
config.add_route(name='repo_summary_explicit', pattern='/{repo_name}/summary')
config.add_route(name='repo_group_home', pattern='/{repo_group_name}')
config.add_route(name='pullrequest_show',
pattern='/{repo_name}/pull-request/{pull_request_id}')
config.add_route(name='pull_requests_global',
pattern='/pull-request/{pull_request_id}')
config.add_route(name='repo_commit',
pattern='/{repo_name}/changeset/{commit_id}')