# HG changeset patch # User RhodeCode Admin # Date 2024-11-14 19:15:11 # Node ID 2f80d511cb9531a9d8957241f42d904af073a691 # Parent 5bcb0167fe6b0f335df11ab5267edc1661331653 feat(events): added context into events so we can track additional context of calling it diff --git a/rhodecode/events/__init__.py b/rhodecode/events/__init__.py --- a/rhodecode/events/__init__.py +++ b/rhodecode/events/__init__.py @@ -18,13 +18,10 @@ import logging -from rhodecode.events.base import ( - RhodeCodeIntegrationEvent, - RhodecodeEvent -) +from rhodecode.events.base import RhodeCodeIntegrationEvent, RhodecodeEvent from rhodecode.events.base import ( # pragma: no cover - FtsBuild + FtsBuild, ) from rhodecode.events.user import ( # pragma: no cover @@ -37,11 +34,16 @@ from rhodecode.events.user import ( # p from rhodecode.events.repo import ( # pragma: no cover RepoEvent, - RepoCommitCommentEvent, RepoCommitCommentEditEvent, - RepoPreCreateEvent, RepoCreateEvent, - RepoPreDeleteEvent, RepoDeleteEvent, - RepoPrePushEvent, RepoPushEvent, - RepoPrePullEvent, RepoPullEvent, + RepoCommitCommentEvent, + RepoCommitCommentEditEvent, + RepoPreCreateEvent, + RepoCreateEvent, + RepoPreDeleteEvent, + RepoDeleteEvent, + RepoPrePushEvent, + RepoPushEvent, + RepoPrePullEvent, + RepoPullEvent, ) from rhodecode.events.repo_group import ( # pragma: no cover @@ -77,12 +79,13 @@ def trigger(event, registry=None): from pyramid.threadlocal import get_current_registry event_name = event.__class__ - log.debug('event %s sent for execution', event_name) + log.debug("event %s sent for execution", event_name) registry = registry or get_current_registry() registry.notify(event) - log.debug('event %s triggered using registry %s', event_name, registry) + log.debug("event %s triggered using registry %s", event_name, registry) # Send the events to integrations directly from rhodecode.integrations import integrations_event_handler + if isinstance(event, RhodeCodeIntegrationEvent): integrations_event_handler(event) diff --git a/rhodecode/events/base.py b/rhodecode/events/base.py --- a/rhodecode/events/base.py +++ b/rhodecode/events/base.py @@ -26,10 +26,7 @@ from rhodecode.lib.utils2 import Attribu # this is a user object to be used for events caused by the system (eg. shell) -SYSTEM_USER = AttributeDict(dict( - username='__SYSTEM__', - user_id='__SYSTEM_ID__' -)) +SYSTEM_USER = AttributeDict(dict(username="__SYSTEM__", user_id="__SYSTEM_ID__")) log = logging.getLogger(__name__) @@ -38,16 +35,18 @@ class RhodecodeEvent(object): """ Base event class for all RhodeCode events """ + name = "RhodeCodeEvent" - no_url_set = '' + no_url_set = "" - def __init__(self, request=None, actor=None): + def __init__(self, request=None, actor=None, context=None): self._request = request self._actor = actor + self._context = context self.utc_timestamp = datetime.datetime.utcnow() def __repr__(self): - return '<{}:({})>'.format(self.__class__.__name__, self.name) + return f"<{self.__class__.__name__}:(name={self.name}, context={self._context})>" def get_request(self): if self._request: @@ -63,11 +62,11 @@ class RhodecodeEvent(object): if not self.request: return - user = getattr(self.request, 'user', None) + user = getattr(self.request, "user", None) if user: return user - api_user = getattr(self.request, 'rpc_user', None) + api_user = getattr(self.request, "rpc_user", None) if api_user: return api_user @@ -80,15 +79,17 @@ class RhodecodeEvent(object): return self._actor auth_user = self.auth_user - log.debug('Got integration actor: %s', auth_user) + log.debug("Got integration actor: %s", auth_user) if isinstance(auth_user, AuthUser): instance = auth_user.get_instance() # we can't find this DB user... if not instance: - return AttributeDict(dict( - username=auth_user.username, - user_id=auth_user.user_id, - )) + return AttributeDict( + dict( + username=auth_user.username, + user_id=auth_user.user_id, + ) + ) elif auth_user: return auth_user return SYSTEM_USER @@ -98,29 +99,26 @@ class RhodecodeEvent(object): auth_user = self.auth_user if auth_user: return auth_user.ip_addr - return '' + return "" @property def server_url(self): if self.request: try: - return self.request.route_url('home') + return self.request.route_url("home") except Exception: - log.exception('Failed to fetch URL for server') + log.exception("Failed to fetch URL for server") return self.no_url_set return self.no_url_set def as_dict(self): data = { - 'name': self.name, - 'utc_timestamp': self.utc_timestamp, - 'actor_ip': self.actor_ip, - 'actor': { - 'username': self.actor.username, - 'user_id': self.actor.user_id - }, - 'server_url': self.server_url + "name": self.name, + "utc_timestamp": self.utc_timestamp, + "actor_ip": self.actor_ip, + "actor": {"username": self.actor.username, "user_id": self.actor.user_id}, + "server_url": self.server_url, } return data @@ -129,13 +127,14 @@ class RhodeCodeIntegrationEvent(Rhodecod """ Special subclass for Integration events """ - description = '' + + description = "" class FtsBuild(RhodecodeEvent): """ This event will be triggered when FTS Build is triggered """ - name = 'fts-build' - display_name = 'Start FTS Build' + name = "fts-build" + display_name = "Start FTS Build" diff --git a/rhodecode/events/interfaces.py b/rhodecode/events/interfaces.py --- a/rhodecode/events/interfaces.py +++ b/rhodecode/events/interfaces.py @@ -24,20 +24,23 @@ class IUserRegistered(Interface): An event type that is emitted whenever a new user registers a user account. """ - user = Attribute('The user object.') - session = Attribute('The session while processing the register form post.') + + user = Attribute("The user object.") + session = Attribute("The session while processing the register form post.") class IUserPreCreate(Interface): """ An event type that is emitted before a new user object is created. """ - user_data = Attribute('Data used to create the new user') + + user_data = Attribute("Data used to create the new user") class IUserPreUpdate(Interface): """ An event type that is emitted before a user object is updated. """ - user = Attribute('The not yet updated user object') - user_data = Attribute('Data used to update the user') + + user = Attribute("The not yet updated user object") + user_data = Attribute("Data used to update the user") diff --git a/rhodecode/events/pullrequest.py b/rhodecode/events/pullrequest.py --- a/rhodecode/events/pullrequest.py +++ b/rhodecode/events/pullrequest.py @@ -19,7 +19,7 @@ import logging from rhodecode.translation import lazy_ugettext -from rhodecode.events.repo import (RepoEvent, _commits_as_dict, _issues_as_dict) +from rhodecode.events.repo import RepoEvent, _commits_as_dict, _issues_as_dict log = logging.getLogger(__name__) @@ -30,45 +30,44 @@ class PullRequestEvent(RepoEvent): :param pullrequest: a :class:`PullRequest` instance """ - name = 'pullrequest-event' - display_name = lazy_ugettext('pullrequest generic event') - description = lazy_ugettext('All events within a context of a pull request') + + name = "pullrequest-event" + display_name = lazy_ugettext("pullrequest generic event") + description = lazy_ugettext("All events within a context of a pull request") - def __init__(self, pullrequest): - super().__init__(pullrequest.target_repo) + def __init__(self, pullrequest, context=None): + super().__init__(pullrequest.target_repo, context=context) self.pullrequest = pullrequest + self.context = self._context def as_dict(self): from rhodecode.lib.utils2 import md5_safe from rhodecode.model.pull_request import PullRequestModel + data = super().as_dict() - commits = _commits_as_dict( - self, - commit_ids=self.pullrequest.revisions, - repos=[self.pullrequest.source_repo] - ) + commits = _commits_as_dict(self, commit_ids=self.pullrequest.revisions, repos=[self.pullrequest.source_repo]) issues = _issues_as_dict(commits) # calculate hashes of all commits for unique identifier of commits # inside that pull request - commits_hash = md5_safe(':'.join(x.get('raw_id', '') for x in commits)) + commits_hash = md5_safe(":".join(x.get("raw_id", "") for x in commits)) - data.update({ - 'pullrequest': { - 'title': self.pullrequest.title, - 'issues': issues, - 'pull_request_id': self.pullrequest.pull_request_id, - 'url': PullRequestModel().get_url( - self.pullrequest, request=self.request), - 'permalink_url': PullRequestModel().get_url( - self.pullrequest, request=self.request, permalink=True), - 'shadow_url': PullRequestModel().get_shadow_clone_url( - self.pullrequest, request=self.request), - 'status': self.pullrequest.calculated_review_status(), - 'commits_uid': commits_hash, - 'commits': commits, + data.update( + { + "pullrequest": { + "title": self.pullrequest.title, + "issues": issues, + "pull_request_id": self.pullrequest.pull_request_id, + "url": PullRequestModel().get_url(self.pullrequest, request=self.request), + "permalink_url": PullRequestModel().get_url(self.pullrequest, request=self.request, permalink=True), + "shadow_url": PullRequestModel().get_shadow_clone_url(self.pullrequest, request=self.request), + "status": self.pullrequest.calculated_review_status(), + "commits_uid": commits_hash, + "commits": commits, + }, + "context": self.context, } - }) + ) return data @@ -77,9 +76,10 @@ class PullRequestCreateEvent(PullRequest An instance of this class is emitted as an :term:`event` after a pull request is created. """ - name = 'pullrequest-create' - display_name = lazy_ugettext('pullrequest created') - description = lazy_ugettext('Event triggered after pull request was created') + + name = "pullrequest-create" + display_name = lazy_ugettext("pullrequest created") + description = lazy_ugettext("Event triggered after pull request was created") class PullRequestCloseEvent(PullRequestEvent): @@ -87,9 +87,10 @@ class PullRequestCloseEvent(PullRequestE An instance of this class is emitted as an :term:`event` after a pull request is closed. """ - name = 'pullrequest-close' - display_name = lazy_ugettext('pullrequest closed') - description = lazy_ugettext('Event triggered after pull request was closed') + + name = "pullrequest-close" + display_name = lazy_ugettext("pullrequest closed") + description = lazy_ugettext("Event triggered after pull request was closed") class PullRequestUpdateEvent(PullRequestEvent): @@ -97,9 +98,10 @@ class PullRequestUpdateEvent(PullRequest An instance of this class is emitted as an :term:`event` after a pull request's commits have been updated. """ - name = 'pullrequest-update' - display_name = lazy_ugettext('pullrequest commits updated') - description = lazy_ugettext('Event triggered after pull requests was updated') + + name = "pullrequest-update" + display_name = lazy_ugettext("pullrequest commits updated") + description = lazy_ugettext("Event triggered after pull requests was updated") class PullRequestReviewEvent(PullRequestEvent): @@ -107,13 +109,13 @@ class PullRequestReviewEvent(PullRequest An instance of this class is emitted as an :term:`event` after a pull request review has changed. A status defines new status of review. """ - name = 'pullrequest-review' - display_name = lazy_ugettext('pullrequest review changed') - description = lazy_ugettext('Event triggered after a review status of a ' - 'pull requests has changed to other.') - def __init__(self, pullrequest, status): - super().__init__(pullrequest) + name = "pullrequest-review" + display_name = lazy_ugettext("pullrequest review changed") + description = lazy_ugettext("Event triggered after a review status of a pull requests has changed to other.") + + def __init__(self, pullrequest, status, context=None): + super().__init__(pullrequest, context=context) self.status = status @@ -122,10 +124,10 @@ class PullRequestMergeEvent(PullRequestE An instance of this class is emitted as an :term:`event` after a pull request is merged. """ - name = 'pullrequest-merge' - display_name = lazy_ugettext('pullrequest merged') - description = lazy_ugettext('Event triggered after a successful merge operation ' - 'was executed on a pull request') + + name = "pullrequest-merge" + display_name = lazy_ugettext("pullrequest merged") + description = lazy_ugettext("Event triggered after a successful merge operation was executed on a pull request") class PullRequestCommentEvent(PullRequestEvent): @@ -133,37 +135,39 @@ class PullRequestCommentEvent(PullReques An instance of this class is emitted as an :term:`event` after a pull request comment is created. """ - name = 'pullrequest-comment' - display_name = lazy_ugettext('pullrequest commented') - description = lazy_ugettext('Event triggered after a comment was made on a code ' - 'in the pull request') - def __init__(self, pullrequest, comment): - super().__init__(pullrequest) + name = "pullrequest-comment" + display_name = lazy_ugettext("pullrequest commented") + description = lazy_ugettext("Event triggered after a comment was made on a code in the pull request") + + def __init__(self, pullrequest, comment, context=None): + super().__init__(pullrequest, context=context) self.comment = comment def as_dict(self): from rhodecode.model.comment import CommentsModel + data = super().as_dict() status = None if self.comment.status_change: status = self.comment.review_status - data.update({ - 'comment': { - 'status': status, - 'text': self.comment.text, - 'type': self.comment.comment_type, - 'file': self.comment.f_path, - 'line': self.comment.line_no, - 'version': self.comment.last_version, - 'url': CommentsModel().get_url( - self.comment, request=self.request), - 'permalink_url': CommentsModel().get_url( - self.comment, request=self.request, permalink=True), + data.update( + { + "comment": { + "status": status, + "text": self.comment.text, + "type": self.comment.comment_type, + "file": self.comment.f_path, + "line": self.comment.line_no, + "version": self.comment.last_version, + "url": CommentsModel().get_url(self.comment, request=self.request), + "permalink_url": CommentsModel().get_url(self.comment, request=self.request, permalink=True), + }, + "context": self.context, } - }) + ) return data @@ -172,10 +176,10 @@ class PullRequestCommentEditEvent(PullRe An instance of this class is emitted as an :term:`event` after a pull request comment is edited. """ - name = 'pullrequest-comment-edit' - display_name = lazy_ugettext('pullrequest comment edited') - description = lazy_ugettext('Event triggered after a comment was edited on a code ' - 'in the pull request') + + name = "pullrequest-comment-edit" + display_name = lazy_ugettext("pullrequest comment edited") + description = lazy_ugettext("Event triggered after a comment was edited on a code in the pull request") def __init__(self, pullrequest, comment): super().__init__(pullrequest) @@ -183,24 +187,26 @@ class PullRequestCommentEditEvent(PullRe def as_dict(self): from rhodecode.model.comment import CommentsModel + data = super().as_dict() status = None if self.comment.status_change: status = self.comment.review_status - data.update({ - 'comment': { - 'status': status, - 'text': self.comment.text, - 'type': self.comment.comment_type, - 'file': self.comment.f_path, - 'line': self.comment.line_no, - 'version': self.comment.last_version, - 'url': CommentsModel().get_url( - self.comment, request=self.request), - 'permalink_url': CommentsModel().get_url( - self.comment, request=self.request, permalink=True), + data.update( + { + "comment": { + "status": status, + "text": self.comment.text, + "type": self.comment.comment_type, + "file": self.comment.f_path, + "line": self.comment.line_no, + "version": self.comment.last_version, + "url": CommentsModel().get_url(self.comment, request=self.request), + "permalink_url": CommentsModel().get_url(self.comment, request=self.request, permalink=True), + }, + "context": self.context } - }) + ) return data diff --git a/rhodecode/events/repo.py b/rhodecode/events/repo.py --- a/rhodecode/events/repo.py +++ b/rhodecode/events/repo.py @@ -37,12 +37,11 @@ def _commits_as_dict(event, commit_ids, :param repos: a list of repos to check """ from rhodecode.lib.utils2 import extract_mentioned_users - from rhodecode.lib.helpers import ( - urlify_commit_message, process_patterns, chop_at_smart) + 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') + raise Exception("no repo defined") if not isinstance(repos, (tuple, list)): repos = [repos] @@ -63,37 +62,31 @@ def _commits_as_dict(event, commit_ids, try: # use copy of needed_commits since we modify it while iterating for commit_id in list(needed_commits): - if commit_id.startswith('tag=>'): + if commit_id.startswith("tag=>"): raw_id = commit_id[5:] cs_data = { - 'raw_id': commit_id, 'short_id': commit_id, - 'branch': None, - 'git_ref_change': 'tag_add', - 'message': f'Added new tag {raw_id}', - 'author': event.actor.full_contact, - 'date': datetime.datetime.now(), - 'refs': { - 'branches': [], - 'bookmarks': [], - 'tags': [] - } + "raw_id": commit_id, + "short_id": commit_id, + "branch": None, + "git_ref_change": "tag_add", + "message": f"Added new tag {raw_id}", + "author": event.actor.full_contact, + "date": datetime.datetime.now(), + "refs": {"branches": [], "bookmarks": [], "tags": []}, } commits.append(cs_data) - elif commit_id.startswith('delete_branch=>'): + elif commit_id.startswith("delete_branch=>"): raw_id = commit_id[15:] cs_data = { - 'raw_id': commit_id, 'short_id': commit_id, - 'branch': None, - 'git_ref_change': 'branch_delete', - 'message': f'Deleted branch {raw_id}', - 'author': event.actor.full_contact, - 'date': datetime.datetime.now(), - 'refs': { - 'branches': [], - 'bookmarks': [], - 'tags': [] - } + "raw_id": commit_id, + "short_id": commit_id, + "branch": None, + "git_ref_change": "branch_delete", + "message": f"Deleted branch {raw_id}", + "author": event.actor.full_contact, + "date": datetime.datetime.now(), + "refs": {"branches": [], "bookmarks": [], "tags": []}, } commits.append(cs_data) @@ -104,50 +97,45 @@ def _commits_as_dict(event, commit_ids, continue # maybe its in next repo cs_data = cs.__json__() - cs_data['refs'] = cs._get_refs() + cs_data["refs"] = cs._get_refs() - cs_data['mentions'] = extract_mentioned_users(cs_data['message']) - cs_data['reviewers'] = reviewers - cs_data['url'] = RepoModel().get_commit_url( - repo, cs_data['raw_id'], request=event.request) - cs_data['permalink_url'] = RepoModel().get_commit_url( - repo, cs_data['raw_id'], request=event.request, - permalink=True) - urlified_message, issues_data, errors = process_patterns( - cs_data['message'], repo.repo_name) - cs_data['issues'] = issues_data - cs_data['message_html'] = urlify_commit_message( - cs_data['message'], repo.repo_name) - cs_data['message_html_title'] = chop_at_smart( - cs_data['message'], '\n', suffix_if_chopped='...') + cs_data["mentions"] = extract_mentioned_users(cs_data["message"]) + cs_data["reviewers"] = reviewers + cs_data["url"] = RepoModel().get_commit_url(repo, cs_data["raw_id"], request=event.request) + cs_data["permalink_url"] = RepoModel().get_commit_url( + repo, cs_data["raw_id"], request=event.request, permalink=True + ) + urlified_message, issues_data, errors = process_patterns(cs_data["message"], repo.repo_name) + cs_data["issues"] = issues_data + cs_data["message_html"] = urlify_commit_message(cs_data["message"], repo.repo_name) + cs_data["message_html_title"] = chop_at_smart(cs_data["message"], "\n", suffix_if_chopped="...") commits.append(cs_data) needed_commits.remove(commit_id) except Exception: - log.exception('Failed to extract commits data') + log.exception("Failed to extract commits data") # we don't send any commits when crash happens, only full list # matters we short circuit then. return [] # we failed to remove all needed_commits from all repositories if needed_commits: - raise ValueError(f'Unexpectedly not found {needed_commits} in all available repos {repos}') + raise ValueError(f"Unexpectedly not found {needed_commits} in all available repos {repos}") - missing_commits = set(commit_ids) - set(c['raw_id'] for c in commits) + missing_commits = set(commit_ids) - set(c["raw_id"] for c in commits) if missing_commits: - log.error('Inconsistent repository state. ' - 'Missing commits: %s', ', '.join(missing_commits)) + log.error("Inconsistent repository state. " "Missing commits: %s", ", ".join(missing_commits)) return commits def _issues_as_dict(commits): - """ Helper function to serialize issues from commits """ + """Helper function to serialize issues from commits""" issues = {} for commit in commits: - for issue in commit['issues']: - issues[issue['id']] = issue + for issue in commit["issues"]: + issues[issue["id"]] = issue return issues @@ -156,33 +144,36 @@ class RepoEvent(RhodeCodeIntegrationEven Base class for events acting on a repository. """ - def __init__(self, repo, actor=None): + def __init__(self, repo, actor=None, context=None): """ :param repo: a :class:`Repository` instance """ - super().__init__(actor=actor) + super().__init__(actor=actor, context=context) self.repo = repo + self.context = self._context def as_dict(self): from rhodecode.model.repo import RepoModel + data = super().as_dict() extra_fields = collections.OrderedDict() for field in self.repo.extra_fields: extra_fields[field.field_key] = field.field_value - data.update({ - 'repo': { - 'repo_id': self.repo.repo_id, - 'repo_name': self.repo.repo_name, - 'repo_type': self.repo.repo_type, - 'url': RepoModel().get_url( - self.repo, request=self.request), - 'permalink_url': RepoModel().get_url( - self.repo, request=self.request, permalink=True), - 'extra_fields': extra_fields + data.update( + { + "repo": { + "repo_id": self.repo.repo_id, + "repo_name": self.repo.repo_name, + "repo_type": self.repo.repo_type, + "url": RepoModel().get_url(self.repo, request=self.request), + "permalink_url": RepoModel().get_url(self.repo, request=self.request, permalink=True), + "extra_fields": extra_fields, + }, + "context": self.context, } - }) + ) return data @@ -192,32 +183,32 @@ class RepoCommitCommentEvent(RepoEvent): on repository commit. """ - name = 'repo-commit-comment' - display_name = lazy_ugettext('repository commit comment') - description = lazy_ugettext('Event triggered after a comment was made ' - 'on commit inside a repository') + name = "repo-commit-comment" + display_name = lazy_ugettext("repository commit comment") + description = lazy_ugettext("Event triggered after a comment was made on commit inside a repository") - def __init__(self, repo, commit, comment): - super().__init__(repo) + def __init__(self, repo, commit, comment, context=None): + super().__init__(repo, context=context) self.commit = commit self.comment = comment def as_dict(self): data = super().as_dict() - data['commit'] = { - 'commit_id': self.commit.raw_id, - 'commit_message': self.commit.message, - 'commit_branch': self.commit.branch, + data["commit"] = { + "commit_id": self.commit.raw_id, + "commit_message": self.commit.message, + "commit_branch": self.commit.branch, } - data['comment'] = { - 'comment_id': self.comment.comment_id, - 'comment_text': self.comment.text, - 'comment_type': self.comment.comment_type, - 'comment_f_path': self.comment.f_path, - 'comment_line_no': self.comment.line_no, - 'comment_version': self.comment.last_version, + data["comment"] = { + "comment_id": self.comment.comment_id, + "comment_text": self.comment.text, + "comment_type": self.comment.comment_type, + "comment_f_path": self.comment.f_path, + "comment_line_no": self.comment.line_no, + "comment_version": self.comment.last_version, } + data["contex"] = self.context return data @@ -227,32 +218,32 @@ class RepoCommitCommentEditEvent(RepoEve on repository commit. """ - name = 'repo-commit-edit-comment' - display_name = lazy_ugettext('repository commit edit comment') - description = lazy_ugettext('Event triggered after a comment was edited ' - 'on commit inside a repository') + name = "repo-commit-edit-comment" + display_name = lazy_ugettext("repository commit edit comment") + description = lazy_ugettext("Event triggered after a comment was edited on commit inside a repository") - def __init__(self, repo, commit, comment): - super().__init__(repo) + def __init__(self, repo, commit, comment, context=None): + super().__init__(repo, context=context) self.commit = commit self.comment = comment def as_dict(self): data = super().as_dict() - data['commit'] = { - 'commit_id': self.commit.raw_id, - 'commit_message': self.commit.message, - 'commit_branch': self.commit.branch, + data["commit"] = { + "commit_id": self.commit.raw_id, + "commit_message": self.commit.message, + "commit_branch": self.commit.branch, } - data['comment'] = { - 'comment_id': self.comment.comment_id, - 'comment_text': self.comment.text, - 'comment_type': self.comment.comment_type, - 'comment_f_path': self.comment.f_path, - 'comment_line_no': self.comment.line_no, - 'comment_version': self.comment.last_version, + data["comment"] = { + "comment_id": self.comment.comment_id, + "comment_text": self.comment.text, + "comment_type": self.comment.comment_type, + "comment_f_path": self.comment.f_path, + "comment_line_no": self.comment.line_no, + "comment_version": self.comment.last_version, } + data["context"] = "context" return data @@ -261,9 +252,10 @@ class RepoPreCreateEvent(RepoEvent): An instance of this class is emitted as an :term:`event` before a repo is created. """ - name = 'repo-pre-create' - display_name = lazy_ugettext('repository pre create') - description = lazy_ugettext('Event triggered before repository is created') + + name = "repo-pre-create" + display_name = lazy_ugettext("repository pre create") + description = lazy_ugettext("Event triggered before repository is created") class RepoCreateEvent(RepoEvent): @@ -271,9 +263,10 @@ class RepoCreateEvent(RepoEvent): An instance of this class is emitted as an :term:`event` whenever a repo is created. """ - name = 'repo-create' - display_name = lazy_ugettext('repository created') - description = lazy_ugettext('Event triggered after repository was created') + + name = "repo-create" + display_name = lazy_ugettext("repository created") + description = lazy_ugettext("Event triggered after repository was created") class RepoPreDeleteEvent(RepoEvent): @@ -281,9 +274,10 @@ class RepoPreDeleteEvent(RepoEvent): An instance of this class is emitted as an :term:`event` whenever a repo is created. """ - name = 'repo-pre-delete' - display_name = lazy_ugettext('repository pre delete') - description = lazy_ugettext('Event triggered before a repository is deleted') + + name = "repo-pre-delete" + display_name = lazy_ugettext("repository pre delete") + description = lazy_ugettext("Event triggered before a repository is deleted") class RepoDeleteEvent(RepoEvent): @@ -291,43 +285,45 @@ class RepoDeleteEvent(RepoEvent): An instance of this class is emitted as an :term:`event` whenever a repo is created. """ - name = 'repo-delete' - display_name = lazy_ugettext('repository deleted') - description = lazy_ugettext('Event triggered after repository was deleted') + + name = "repo-delete" + display_name = lazy_ugettext("repository deleted") + description = lazy_ugettext("Event triggered after repository was deleted") class RepoVCSEvent(RepoEvent): """ Base class for events triggered by the VCS """ - name = '' - display_name = 'generic_vcs_event' - def __init__(self, repo_name, extras): + name = "" + display_name = "generic_vcs_event" + + def __init__(self, repo_name, extras, context=None): self.repo = Repository.get_by_repo_name(repo_name) if not self.repo: - raise Exception(f'repo by this name {repo_name} does not exist') + raise Exception(f"repo by this name {repo_name} does not exist") self.extras = extras - super().__init__(self.repo) + super().__init__(self.repo, context=context) @property def actor(self): - if self.extras.get('username'): - return User.get_by_username(self.extras['username']) + if self.extras.get("username"): + return User.get_by_username(self.extras["username"]) @property def actor_ip(self): - if self.extras.get('ip'): - return self.extras['ip'] + if self.extras.get("ip"): + return self.extras["ip"] @property def server_url(self): - if self.extras.get('server_url'): - return self.extras['server_url'] + if self.extras.get("server_url"): + return self.extras["server_url"] @property def request(self): - return self.extras.get('request') or self.get_request() + return self.extras.get("request") or self.get_request() class RepoPrePullEvent(RepoVCSEvent): @@ -335,9 +331,10 @@ 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' - display_name = lazy_ugettext('repository pre pull') - description = lazy_ugettext('Event triggered before repository code is pulled') + + name = "repo-pre-pull" + display_name = lazy_ugettext("repository pre pull") + description = lazy_ugettext("Event triggered before repository code is pulled") class RepoPullEvent(RepoVCSEvent): @@ -345,9 +342,10 @@ class RepoPullEvent(RepoVCSEvent): An instance of this class is emitted as an :term:`event` after commits are pulled from a repo. """ - name = 'repo-pull' - display_name = lazy_ugettext('repository pull') - description = lazy_ugettext('Event triggered after repository code was pulled') + + name = "repo-pull" + display_name = lazy_ugettext("repository pull") + description = lazy_ugettext("Event triggered after repository code was pulled") class RepoPrePushEvent(RepoVCSEvent): @@ -355,10 +353,10 @@ 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' - display_name = lazy_ugettext('repository pre push') - description = lazy_ugettext('Event triggered before the code is ' - 'pushed to a repository') + + name = "repo-pre-push" + display_name = lazy_ugettext("repository pre push") + description = lazy_ugettext("Event triggered before the code is pushed to a repository") class RepoPushEvent(RepoVCSEvent): @@ -368,13 +366,13 @@ class RepoPushEvent(RepoVCSEvent): :param extras: (optional) dict of data from proxied VCS actions """ - name = 'repo-push' - display_name = lazy_ugettext('repository push') - description = lazy_ugettext('Event triggered after the code was ' - 'pushed to a repository') - def __init__(self, repo_name, pushed_commit_ids, extras): - super().__init__(repo_name, extras) + name = "repo-push" + display_name = lazy_ugettext("repository push") + description = lazy_ugettext("Event triggered after the code was pushed to a repository") + + def __init__(self, repo_name, pushed_commit_ids, extras, context=None): + super().__init__(repo_name, extras, context=context) self.pushed_commit_ids = pushed_commit_ids self.new_refs = extras.new_refs @@ -382,63 +380,48 @@ class RepoPushEvent(RepoVCSEvent): data = super().as_dict() def branch_url(branch_name): - return '{}/changelog?branch={}'.format( - data['repo']['url'], branch_name) + return "{}/changelog?branch={}".format(data["repo"]["url"], branch_name) def tag_url(tag_name): - return '{}/files/{}/'.format( - data['repo']['url'], tag_name) + return "{}/files/{}/".format(data["repo"]["url"], tag_name) - commits = _commits_as_dict( - self, commit_ids=self.pushed_commit_ids, repos=[self.repo]) + commits = _commits_as_dict(self, commit_ids=self.pushed_commit_ids, repos=[self.repo]) last_branch = None for commit in reversed(commits): - commit['branch'] = commit['branch'] or last_branch - last_branch = commit['branch'] + commit["branch"] = commit["branch"] or last_branch + last_branch = commit["branch"] issues = _issues_as_dict(commits) branches = set() tags = set() for commit in commits: - if commit['refs']['tags']: - for tag in commit['refs']['tags']: + if commit["refs"]["tags"]: + for tag in commit["refs"]["tags"]: tags.add(tag) - if commit['branch']: - branches.add(commit['branch']) + if commit["branch"]: + branches.add(commit["branch"]) # maybe we have branches in new_refs ? try: - branches = branches.union(set(self.new_refs['branches'])) + branches = branches.union(set(self.new_refs["branches"])) except Exception: pass - branches = [ - { - 'name': branch, - 'url': branch_url(branch) - } - for branch in branches - ] + branches = [{"name": branch, "url": branch_url(branch)} for branch in branches] # maybe we have branches in new_refs ? try: - tags = tags.union(set(self.new_refs['tags'])) + tags = tags.union(set(self.new_refs["tags"])) except Exception: pass - tags = [ - { - 'name': tag, - 'url': tag_url(tag) - } - for tag in tags - ] + tags = [{"name": tag, "url": tag_url(tag)} for tag in tags] - data['push'] = { - 'commits': commits, - 'issues': issues, - 'branches': branches, - 'tags': tags, + data["push"] = { + "commits": commits, + "issues": issues, + "branches": branches, + "tags": tags, } return data diff --git a/rhodecode/events/repo_group.py b/rhodecode/events/repo_group.py --- a/rhodecode/events/repo_group.py +++ b/rhodecode/events/repo_group.py @@ -29,27 +29,31 @@ class RepoGroupEvent(RhodeCodeIntegratio """ Base class for events acting on a repository group. - :param repo: a :class:`RepositoryGroup` instance + :param repo_group: a :class:`RepositoryGroup` instance """ - def __init__(self, repo_group): - super().__init__() + def __init__(self, repo_group, context=None): + super().__init__(context=context) self.repo_group = repo_group + self.context = self._context def as_dict(self): data = super().as_dict() - data.update({ - 'repo_group': { - 'group_id': self.repo_group.group_id, - 'group_name': self.repo_group.group_name, - 'group_parent_id': self.repo_group.group_parent_id, - 'group_description': self.repo_group.group_description, - 'user_id': self.repo_group.user_id, - 'created_by': self.repo_group.user.username, - 'created_on': self.repo_group.created_on, - 'enable_locking': self.repo_group.enable_locking, + data.update( + { + "repo_group": { + "group_id": self.repo_group.group_id, + "group_name": self.repo_group.group_name, + "group_parent_id": self.repo_group.group_parent_id, + "group_description": self.repo_group.group_description, + "user_id": self.repo_group.user_id, + "created_by": self.repo_group.user.username, + "created_on": self.repo_group.created_on, + "enable_locking": self.repo_group.enable_locking, + }, + "context": self.context } - }) + ) return data @@ -58,9 +62,10 @@ class RepoGroupCreateEvent(RepoGroupEven An instance of this class is emitted as an :term:`event` whenever a repository group is created. """ - name = 'repo-group-create' - display_name = lazy_ugettext('repository group created') - description = lazy_ugettext('Event triggered after a repository group was created') + + name = "repo-group-create" + display_name = lazy_ugettext("repository group created") + description = lazy_ugettext("Event triggered after a repository group was created") class RepoGroupDeleteEvent(RepoGroupEvent): @@ -68,9 +73,10 @@ class RepoGroupDeleteEvent(RepoGroupEven An instance of this class is emitted as an :term:`event` whenever a repository group is deleted. """ - name = 'repo-group-delete' - display_name = lazy_ugettext('repository group deleted') - description = lazy_ugettext('Event triggered after a repository group was deleted') + + name = "repo-group-delete" + display_name = lazy_ugettext("repository group deleted") + description = lazy_ugettext("Event triggered after a repository group was deleted") class RepoGroupUpdateEvent(RepoGroupEvent): @@ -78,6 +84,7 @@ class RepoGroupUpdateEvent(RepoGroupEven An instance of this class is emitted as an :term:`event` whenever a repository group is updated. """ - name = 'repo-group-update' - display_name = lazy_ugettext('repository group update') - description = lazy_ugettext('Event triggered after a repository group was updated') + + name = "repo-group-update" + display_name = lazy_ugettext("repository group update") + description = lazy_ugettext("Event triggered after a repository group was updated") diff --git a/rhodecode/events/user.py b/rhodecode/events/user.py --- a/rhodecode/events/user.py +++ b/rhodecode/events/user.py @@ -21,8 +21,7 @@ from zope.interface import implementer from rhodecode.translation import lazy_ugettext from rhodecode.events.base import RhodecodeEvent, RhodeCodeIntegrationEvent -from rhodecode.events.interfaces import ( - IUserRegistered, IUserPreCreate, IUserPreUpdate) +from rhodecode.events.interfaces import IUserRegistered, IUserPreCreate, IUserPreUpdate log = logging.getLogger(__name__) @@ -33,8 +32,9 @@ class UserRegistered(RhodeCodeIntegratio An instance of this class is emitted as an :term:`event` whenever a user account is registered. """ - name = 'user-register' - display_name = lazy_ugettext('user registered') + + name = "user-register" + display_name = lazy_ugettext("user registered") def __init__(self, user, session): super().__init__() @@ -48,8 +48,9 @@ class UserPreCreate(RhodeCodeIntegration An instance of this class is emitted as an :term:`event` before a new user object is created. """ - name = 'user-pre-create' - display_name = lazy_ugettext('user pre create') + + name = "user-pre-create" + display_name = lazy_ugettext("user pre create") def __init__(self, user_data): super().__init__() @@ -62,8 +63,9 @@ class UserPostCreate(RhodeCodeIntegratio An instance of this class is emitted as an :term:`event` after a new user object is created. """ - name = 'user-post-create' - display_name = lazy_ugettext('user post create') + + name = "user-post-create" + display_name = lazy_ugettext("user post create") def __init__(self, user_data): super().__init__() @@ -76,8 +78,9 @@ class UserPreUpdate(RhodeCodeIntegration An instance of this class is emitted as an :term:`event` before a user object is updated. """ - name = 'user-pre-update' - display_name = lazy_ugettext('user pre update') + + name = "user-pre-update" + display_name = lazy_ugettext("user pre update") def __init__(self, user, user_data): super().__init__() @@ -87,8 +90,8 @@ class UserPreUpdate(RhodeCodeIntegration class UserPermissionsChange(RhodecodeEvent): """ - This event should be triggered on an event that permissions of user might changed. - Currently this should be triggered on: + This event should be triggered on an event that permissions of user might be changed. + Currently, this should be triggered on: - user added/removed from user group - repo permissions changed @@ -96,9 +99,11 @@ class UserPermissionsChange(RhodecodeEve - user group permissions changed """ - name = 'user-permissions-change' - display_name = lazy_ugettext('user permissions change') + + name = "user-permissions-change" + display_name = lazy_ugettext("user permissions change") - def __init__(self, user_ids): - super().__init__() + def __init__(self, user_ids, context=None): + super().__init__(context=context) self.user_ids = user_ids + self.context = self._context