repo.py
219 lines
| 6.9 KiB
| text/x-python
|
PythonLexer
r375 | # Copyright (C) 2016-2016 RhodeCode GmbH | ||
# | |||
# This program is free software: you can redistribute it and/or modify | |||
# it under the terms of the GNU Affero General Public License, version 3 | |||
# (only), as published by the Free Software Foundation. | |||
# | |||
# This program is distributed in the hope that it will be useful, | |||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | |||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |||
# GNU General Public License for more details. | |||
# | |||
# You should have received a copy of the GNU Affero General Public License | |||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | |||
# | |||
# This program is dual-licensed. If you wish to learn more about the | |||
# RhodeCode Enterprise Edition, including its added features, Support services, | |||
# and proprietary license terms, please see https://rhodecode.com/licenses/ | |||
r411 | import logging | ||
r379 | |||
r411 | from rhodecode.translation import lazy_ugettext | ||
r389 | from rhodecode.model.db import User, Repository, Session | ||
r379 | from rhodecode.events.base import RhodecodeEvent | ||
r411 | log = logging.getLogger() | ||
r375 | |||
class RepoEvent(RhodecodeEvent): | |||
""" | |||
Base class for events acting on a repository. | |||
:param repo: a :class:`Repository` instance | |||
""" | |||
r379 | |||
r375 | def __init__(self, repo): | ||
r379 | super(RepoEvent, self).__init__() | ||
r375 | self.repo = repo | ||
r411 | def as_dict(self): | ||
from rhodecode.model.repo import RepoModel | |||
data = super(RepoEvent, self).as_dict() | |||
data.update({ | |||
'repo': { | |||
'repo_id': self.repo.repo_id, | |||
'repo_name': self.repo.repo_name, | |||
r445 | 'repo_type': self.repo.repo_type, | ||
r411 | 'url': RepoModel().get_url(self.repo) | ||
} | |||
}) | |||
return data | |||
def _commits_as_dict(self, commit_ids): | |||
""" Helper function to serialize commit_ids """ | |||
from rhodecode.lib.utils2 import extract_mentioned_users | |||
from rhodecode.model.db import Repository | |||
from rhodecode.lib import helpers as h | |||
from rhodecode.lib.helpers import process_patterns | |||
from rhodecode.lib.helpers import urlify_commit_message | |||
if not commit_ids: | |||
return [] | |||
commits = [] | |||
reviewers = [] | |||
vcs_repo = self.repo.scm_instance(cache=False) | |||
try: | |||
for commit_id in commit_ids: | |||
cs = vcs_repo.get_changeset(commit_id) | |||
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=self.repo.repo_name, | |||
revision=cs_data['raw_id'], | |||
qualified=True | |||
) | |||
urlified_message, issues_data = process_patterns( | |||
cs_data['message'], self.repo.repo_name) | |||
cs_data['issues'] = issues_data | |||
cs_data['message_html'] = urlify_commit_message(cs_data['message'], | |||
self.repo.repo_name) | |||
commits.append(cs_data) | |||
except Exception as e: | |||
log.exception(e) | |||
# we don't send any commits when crash happens, only full list matters | |||
# we short circuit then. | |||
return [] | |||
return commits | |||
def _issues_as_dict(self, commits): | |||
""" Helper function to serialize issues from commits """ | |||
issues = {} | |||
for commit in commits: | |||
for issue in commit['issues']: | |||
issues[issue['id']] = issue | |||
return issues | |||
r375 | |||
class RepoPreCreateEvent(RepoEvent): | |||
""" | |||
An instance of this class is emitted as an :term:`event` before a repo is | |||
created. | |||
""" | |||
name = 'repo-pre-create' | |||
r411 | display_name = lazy_ugettext('repository pre create') | ||
r375 | |||
r411 | class RepoCreateEvent(RepoEvent): | ||
r375 | """ | ||
An instance of this class is emitted as an :term:`event` whenever a repo is | |||
created. | |||
""" | |||
r411 | name = 'repo-create' | ||
display_name = lazy_ugettext('repository created') | |||
r375 | |||
class RepoPreDeleteEvent(RepoEvent): | |||
""" | |||
An instance of this class is emitted as an :term:`event` whenever a repo is | |||
created. | |||
""" | |||
name = 'repo-pre-delete' | |||
r411 | display_name = lazy_ugettext('repository pre delete') | ||
r375 | |||
r411 | class RepoDeleteEvent(RepoEvent): | ||
r375 | """ | ||
An instance of this class is emitted as an :term:`event` whenever a repo is | |||
created. | |||
""" | |||
r411 | name = 'repo-delete' | ||
display_name = lazy_ugettext('repository deleted') | |||
r375 | |||
class RepoVCSEvent(RepoEvent): | |||
""" | |||
Base class for events triggered by the VCS | |||
""" | |||
def __init__(self, repo_name, extras): | |||
self.repo = Repository.get_by_repo_name(repo_name) | |||
if not self.repo: | |||
raise Exception('repo by this name %s does not exist' % repo_name) | |||
self.extras = extras | |||
super(RepoVCSEvent, self).__init__(self.repo) | |||
r379 | @property | ||
r389 | def actor(self): | ||
r379 | if self.extras.get('username'): | ||
r389 | return User.get_by_username(self.extras['username']) | ||
r379 | |||
@property | |||
r389 | def actor_ip(self): | ||
r379 | if self.extras.get('ip'): | ||
r389 | return self.extras['ip'] | ||
r379 | |||
r375 | |||
class RepoPrePullEvent(RepoVCSEvent): | |||
""" | |||
An instance of this class is emitted as an :term:`event` before commits | |||
are pulled from a repo. | |||
""" | |||
name = 'repo-pre-pull' | |||
r411 | display_name = lazy_ugettext('repository pre pull') | ||
r375 | |||
class RepoPullEvent(RepoVCSEvent): | |||
""" | |||
An instance of this class is emitted as an :term:`event` after commits | |||
are pulled from a repo. | |||
""" | |||
name = 'repo-pull' | |||
r411 | display_name = lazy_ugettext('repository pull') | ||
r375 | |||
class RepoPrePushEvent(RepoVCSEvent): | |||
""" | |||
An instance of this class is emitted as an :term:`event` before commits | |||
are pushed to a repo. | |||
""" | |||
name = 'repo-pre-push' | |||
r411 | display_name = lazy_ugettext('repository pre push') | ||
r375 | |||
class RepoPushEvent(RepoVCSEvent): | |||
""" | |||
An instance of this class is emitted as an :term:`event` after commits | |||
are pushed to a repo. | |||
:param extras: (optional) dict of data from proxied VCS actions | |||
""" | |||
name = 'repo-push' | |||
r411 | display_name = lazy_ugettext('repository push') | ||
r375 | |||
def __init__(self, repo_name, pushed_commit_ids, extras): | |||
super(RepoPushEvent, self).__init__(repo_name, extras) | |||
self.pushed_commit_ids = pushed_commit_ids | |||
r411 | def as_dict(self): | ||
data = super(RepoPushEvent, self).as_dict() | |||
branch_url = repo_url = data['repo']['url'] | |||
commits = self._commits_as_dict(self.pushed_commit_ids) | |||
issues = self._issues_as_dict(commits) | |||
branches = set( | |||
commit['branch'] for commit in commits if commit['branch']) | |||
branches = [ | |||
{ | |||
'name': branch, | |||
'url': '{}/changelog?branch={}'.format( | |||
data['repo']['url'], branch) | |||
} | |||
for branch in branches | |||
] | |||
data['push'] = { | |||
'commits': commits, | |||
'issues': issues, | |||
'branches': branches, | |||
} | |||
return data |