hooks_utils.py
264 lines
| 9.6 KiB
| text/x-python
|
PythonLexer
r1 | # -*- coding: utf-8 -*- | |||
r4306 | # Copyright (C) 2010-2020 RhodeCode GmbH | |||
r1 | # | |||
# 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/ | ||||
import webob | ||||
r2351 | from pyramid.threadlocal import get_current_request | |||
r1 | ||||
r378 | from rhodecode import events | |||
r1 | from rhodecode.lib import hooks_base | |||
from rhodecode.lib import utils2 | ||||
r4305 | def _supports_repo_type(repo_type): | |||
if repo_type in ('hg', 'git'): | ||||
return True | ||||
return False | ||||
def _get_vcs_operation_context(username, repo_name, repo_type, action): | ||||
# NOTE(dan): import loop | ||||
r1 | from rhodecode.lib.base import vcs_operation_context | |||
r4305 | ||||
r1 | check_locking = action in ('pull', 'push') | |||
r2351 | request = get_current_request() | |||
r1 | try: | |||
r4305 | environ = request.environ | |||
r1 | except TypeError: | |||
r2351 | # we might use this outside of request context | |||
r4305 | environ = {} | |||
r1 | ||||
r4305 | if not environ: | |||
environ = webob.Request.blank('').environ | ||||
extras = vcs_operation_context(environ, repo_name, username, action, repo_type, check_locking) | ||||
r1 | return utils2.AttributeDict(extras) | |||
r4305 | def trigger_post_push_hook(username, action, hook_type, repo_name, repo_type, commit_ids): | |||
r1 | """ | |||
Triggers push action hooks | ||||
:param username: username who pushes | ||||
:param action: push/push_local/push_remote | ||||
r4305 | :param hook_type: type of hook executed | |||
r1 | :param repo_name: name of repo | |||
r4305 | :param repo_type: the type of SCM repo | |||
r1 | :param commit_ids: list of commit ids that we pushed | |||
""" | ||||
r4305 | extras = _get_vcs_operation_context(username, repo_name, repo_type, action) | |||
r1 | extras.commit_ids = commit_ids | |||
r3133 | extras.hook_type = hook_type | |||
r1 | hooks_base.post_push(extras) | |||
r4305 | def trigger_comment_commit_hooks(username, repo_name, repo_type, repo, data=None): | |||
""" | ||||
Triggers when a comment is made on a commit | ||||
:param username: username who creates the comment | ||||
:param repo_name: name of target repo | ||||
:param repo_type: the type of SCM target repo | ||||
:param repo: the repo object we trigger the event for | ||||
:param data: extra data for specific events e.g {'comment': comment_obj, 'commit': commit_obj} | ||||
""" | ||||
if not _supports_repo_type(repo_type): | ||||
return | ||||
extras = _get_vcs_operation_context(username, repo_name, repo_type, 'comment_commit') | ||||
comment = data['comment'] | ||||
commit = data['commit'] | ||||
events.trigger(events.RepoCommitCommentEvent(repo, commit, comment)) | ||||
extras.update(repo.get_dict()) | ||||
extras.commit = commit.serialize() | ||||
extras.comment = comment.get_api_data() | ||||
extras.created_by = username | ||||
r4445 | hooks_base.comment_commit_repository(**extras) | |||
r4305 | ||||
r4444 | def trigger_comment_commit_edit_hooks(username, repo_name, repo_type, repo, data=None): | |||
""" | ||||
Triggers when a comment is edited on a commit | ||||
:param username: username who edits the comment | ||||
:param repo_name: name of target repo | ||||
:param repo_type: the type of SCM target repo | ||||
:param repo: the repo object we trigger the event for | ||||
:param data: extra data for specific events e.g {'comment': comment_obj, 'commit': commit_obj} | ||||
""" | ||||
if not _supports_repo_type(repo_type): | ||||
return | ||||
extras = _get_vcs_operation_context(username, repo_name, repo_type, 'comment_commit') | ||||
comment = data['comment'] | ||||
commit = data['commit'] | ||||
events.trigger(events.RepoCommitCommentEditEvent(repo, commit, comment)) | ||||
extras.update(repo.get_dict()) | ||||
extras.commit = commit.serialize() | ||||
extras.comment = comment.get_api_data() | ||||
extras.created_by = username | ||||
r4445 | hooks_base.comment_edit_commit_repository(**extras) | |||
r4444 | ||||
r4305 | def trigger_create_pull_request_hook(username, repo_name, repo_type, pull_request, data=None): | |||
r1 | """ | |||
Triggers create pull request action hooks | ||||
:param username: username who creates the pull request | ||||
:param repo_name: name of target repo | ||||
r4305 | :param repo_type: the type of SCM target repo | |||
r1 | :param pull_request: the pull request that was created | |||
r3415 | :param data: extra data for specific events e.g {'comment': comment_obj} | |||
r1 | """ | |||
r4305 | if not _supports_repo_type(repo_type): | |||
r1 | return | |||
r4305 | extras = _get_vcs_operation_context(username, repo_name, repo_type, 'create_pull_request') | |||
r378 | events.trigger(events.PullRequestCreateEvent(pull_request)) | |||
r3627 | extras.update(pull_request.get_api_data(with_merge_state=False)) | |||
r4445 | hooks_base.create_pull_request(**extras) | |||
r1 | ||||
r4305 | def trigger_merge_pull_request_hook(username, repo_name, repo_type, pull_request, data=None): | |||
r1 | """ | |||
Triggers merge pull request action hooks | ||||
:param username: username who creates the pull request | ||||
:param repo_name: name of target repo | ||||
r4305 | :param repo_type: the type of SCM target repo | |||
r1 | :param pull_request: the pull request that was merged | |||
r3415 | :param data: extra data for specific events e.g {'comment': comment_obj} | |||
r1 | """ | |||
r4305 | if not _supports_repo_type(repo_type): | |||
r1 | return | |||
r4305 | extras = _get_vcs_operation_context(username, repo_name, repo_type, 'merge_pull_request') | |||
r378 | events.trigger(events.PullRequestMergeEvent(pull_request)) | |||
r1 | extras.update(pull_request.get_api_data()) | |||
r4445 | hooks_base.merge_pull_request(**extras) | |||
r1 | ||||
r4305 | def trigger_close_pull_request_hook(username, repo_name, repo_type, pull_request, data=None): | |||
r1 | """ | |||
Triggers close pull request action hooks | ||||
:param username: username who creates the pull request | ||||
:param repo_name: name of target repo | ||||
r4305 | :param repo_type: the type of SCM target repo | |||
r1 | :param pull_request: the pull request that was closed | |||
r3415 | :param data: extra data for specific events e.g {'comment': comment_obj} | |||
r1 | """ | |||
r4305 | if not _supports_repo_type(repo_type): | |||
r1 | return | |||
r4305 | extras = _get_vcs_operation_context(username, repo_name, repo_type, 'close_pull_request') | |||
r378 | events.trigger(events.PullRequestCloseEvent(pull_request)) | |||
r1 | extras.update(pull_request.get_api_data()) | |||
r4445 | hooks_base.close_pull_request(**extras) | |||
r1 | ||||
r4305 | def trigger_review_pull_request_hook(username, repo_name, repo_type, pull_request, data=None): | |||
r1 | """ | |||
Triggers review status change pull request action hooks | ||||
:param username: username who creates the pull request | ||||
:param repo_name: name of target repo | ||||
r4305 | :param repo_type: the type of SCM target repo | |||
r1 | :param pull_request: the pull request that review status changed | |||
r3415 | :param data: extra data for specific events e.g {'comment': comment_obj} | |||
r1 | """ | |||
r4305 | if not _supports_repo_type(repo_type): | |||
r1 | return | |||
r4305 | extras = _get_vcs_operation_context(username, repo_name, repo_type, 'review_pull_request') | |||
r3415 | status = data.get('status') | |||
events.trigger(events.PullRequestReviewEvent(pull_request, status)) | ||||
r1 | extras.update(pull_request.get_api_data()) | |||
r4445 | hooks_base.review_pull_request(**extras) | |||
r1 | ||||
r4305 | def trigger_comment_pull_request_hook(username, repo_name, repo_type, pull_request, data=None): | |||
""" | ||||
Triggers when a comment is made on a pull request | ||||
:param username: username who creates the pull request | ||||
:param repo_name: name of target repo | ||||
:param repo_type: the type of SCM target repo | ||||
:param pull_request: the pull request that comment was made on | ||||
:param data: extra data for specific events e.g {'comment': comment_obj} | ||||
""" | ||||
if not _supports_repo_type(repo_type): | ||||
return | ||||
extras = _get_vcs_operation_context(username, repo_name, repo_type, 'comment_pull_request') | ||||
comment = data['comment'] | ||||
events.trigger(events.PullRequestCommentEvent(pull_request, comment)) | ||||
extras.update(pull_request.get_api_data()) | ||||
extras.comment = comment.get_api_data() | ||||
r4445 | hooks_base.comment_pull_request(**extras) | |||
r4305 | ||||
r4444 | def trigger_comment_pull_request_edit_hook(username, repo_name, repo_type, pull_request, data=None): | |||
""" | ||||
Triggers when a comment was edited on a pull request | ||||
:param username: username who made the edit | ||||
:param repo_name: name of target repo | ||||
:param repo_type: the type of SCM target repo | ||||
:param pull_request: the pull request that comment was made on | ||||
:param data: extra data for specific events e.g {'comment': comment_obj} | ||||
""" | ||||
if not _supports_repo_type(repo_type): | ||||
return | ||||
extras = _get_vcs_operation_context(username, repo_name, repo_type, 'comment_pull_request') | ||||
comment = data['comment'] | ||||
events.trigger(events.PullRequestCommentEditEvent(pull_request, comment)) | ||||
extras.update(pull_request.get_api_data()) | ||||
extras.comment = comment.get_api_data() | ||||
r4445 | hooks_base.comment_edit_pull_request(**extras) | |||
r4444 | ||||
r4305 | def trigger_update_pull_request_hook(username, repo_name, repo_type, pull_request, data=None): | |||
r1 | """ | |||
Triggers update pull request action hooks | ||||
:param username: username who creates the pull request | ||||
:param repo_name: name of target repo | ||||
r4305 | :param repo_type: the type of SCM target repo | |||
r1 | :param pull_request: the pull request that was updated | |||
r3415 | :param data: extra data for specific events e.g {'comment': comment_obj} | |||
r1 | """ | |||
r4305 | if not _supports_repo_type(repo_type): | |||
r1 | return | |||
r4305 | extras = _get_vcs_operation_context(username, repo_name, repo_type, 'update_pull_request') | |||
r378 | events.trigger(events.PullRequestUpdateEvent(pull_request)) | |||
r1 | extras.update(pull_request.get_api_data()) | |||
r4445 | hooks_base.update_pull_request(**extras) | |||