diff --git a/rhodecode/events/__init__.py b/rhodecode/events/__init__.py --- a/rhodecode/events/__init__.py +++ b/rhodecode/events/__init__.py @@ -49,3 +49,11 @@ from rhodecode.events.repo import ( RepoPrePushEvent, RepoPushEvent, RepoPrePullEvent, RepoPullEvent, ) + +from rhodecode.events.pullrequest import ( + PullRequestCreateEvent, + PullRequestUpdateEvent, + PullRequestReviewEvent, + PullRequestMergeEvent, + PullRequestCloseEvent, +) \ No newline at end of file diff --git a/rhodecode/events/pullrequest.py b/rhodecode/events/pullrequest.py new file mode 100644 --- /dev/null +++ b/rhodecode/events/pullrequest.py @@ -0,0 +1,72 @@ +# 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 . +# +# 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/ + +from rhodecode.events.repo import RepoEvent + + +class PullRequestEvent(RepoEvent): + """ + Base class for events acting on a repository. + + :param repo: a :class:`Repository` instance + """ + def __init__(self, pullrequest): + super(PullRequestEvent, self).__init__(pullrequest.target_repo) + self.pullrequest = pullrequest + + +class PullRequestCreateEvent(PullRequestEvent): + """ + An instance of this class is emitted as an :term:`event` after a pull + request is created. + """ + name = 'pullrequest-create' + + +class PullRequestCloseEvent(PullRequestEvent): + """ + An instance of this class is emitted as an :term:`event` after a pull + request is closed. + """ + name = 'pullrequest-close' + + +class PullRequestUpdateEvent(PullRequestEvent): + """ + An instance of this class is emitted as an :term:`event` after a pull + request is updated. + """ + name = 'pullrequest-update' + + +class PullRequestMergeEvent(PullRequestEvent): + """ + An instance of this class is emitted as an :term:`event` after a pull + request is merged. + """ + name = 'pullrequest-merge' + + +class PullRequestReviewEvent(PullRequestEvent): + """ + An instance of this class is emitted as an :term:`event` after a pull + request is reviewed. + """ + name = 'pullrequest-review' + + diff --git a/rhodecode/lib/hooks_utils.py b/rhodecode/lib/hooks_utils.py --- a/rhodecode/lib/hooks_utils.py +++ b/rhodecode/lib/hooks_utils.py @@ -21,6 +21,7 @@ import pylons import webob +from rhodecode import events from rhodecode.lib import hooks_base from rhodecode.lib import utils2 @@ -76,6 +77,7 @@ def trigger_log_create_pull_request_hook extras = _get_rc_scm_extras(username, repo_name, repo_alias, 'create_pull_request') + events.trigger(events.PullRequestCreateEvent(pull_request)) extras.update(pull_request.get_api_data()) hooks_base.log_create_pull_request(**extras) @@ -95,6 +97,7 @@ def trigger_log_merge_pull_request_hook( extras = _get_rc_scm_extras(username, repo_name, repo_alias, 'merge_pull_request') + events.trigger(events.PullRequestMergeEvent(pull_request)) extras.update(pull_request.get_api_data()) hooks_base.log_merge_pull_request(**extras) @@ -114,6 +117,7 @@ def trigger_log_close_pull_request_hook( extras = _get_rc_scm_extras(username, repo_name, repo_alias, 'close_pull_request') + events.trigger(events.PullRequestCloseEvent(pull_request)) extras.update(pull_request.get_api_data()) hooks_base.log_close_pull_request(**extras) @@ -133,6 +137,7 @@ def trigger_log_review_pull_request_hook extras = _get_rc_scm_extras(username, repo_name, repo_alias, 'review_pull_request') + events.trigger(events.PullRequestReviewEvent(pull_request)) extras.update(pull_request.get_api_data()) hooks_base.log_review_pull_request(**extras) @@ -152,5 +157,6 @@ def trigger_log_update_pull_request_hook extras = _get_rc_scm_extras(username, repo_name, repo_alias, 'update_pull_request') + events.trigger(events.PullRequestUpdateEvent(pull_request)) extras.update(pull_request.get_api_data()) hooks_base.log_update_pull_request(**extras) diff --git a/rhodecode/tests/events/test_pullrequest.py b/rhodecode/tests/events/test_pullrequest.py new file mode 100644 --- /dev/null +++ b/rhodecode/tests/events/test_pullrequest.py @@ -0,0 +1,61 @@ +# -*- coding: utf-8 -*- + +# Copyright (C) 2010-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 . +# +# 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 pytest + +from rhodecode.tests.events.conftest import EventCatcher + +from rhodecode.model.pull_request import PullRequestModel +from rhodecode.events import ( + PullRequestCreateEvent, + PullRequestUpdateEvent, + PullRequestReviewEvent, + PullRequestMergeEvent, + PullRequestCloseEvent, +) + + +@pytest.mark.backends("git", "hg") +def test_create_pull_request_events(pr_util): + with EventCatcher() as event_catcher: + pr_util.create_pull_request() + + assert PullRequestCreateEvent in event_catcher.events_types + + +@pytest.mark.backends("git", "hg") +def test_close_pull_request_events(pr_util, user_admin): + pr = pr_util.create_pull_request() + + with EventCatcher() as event_catcher: + PullRequestModel().close_pull_request(pr, user_admin) + + assert PullRequestCloseEvent in event_catcher.events_types + + +@pytest.mark.backends("git", "hg") +def test_close_pull_request_with_comment_events(pr_util, user_admin): + pr = pr_util.create_pull_request() + + with EventCatcher() as event_catcher: + PullRequestModel().close_pull_request_with_comment( + pr, user_admin, pr.target_repo) + + assert PullRequestCloseEvent in event_catcher.events_types