Show More
@@ -0,0 +1,72 b'' | |||
|
1 | # Copyright (C) 2016-2016 RhodeCode GmbH | |
|
2 | # | |
|
3 | # This program is free software: you can redistribute it and/or modify | |
|
4 | # it under the terms of the GNU Affero General Public License, version 3 | |
|
5 | # (only), as published by the Free Software Foundation. | |
|
6 | # | |
|
7 | # This program is distributed in the hope that it will be useful, | |
|
8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
|
9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
|
10 | # GNU General Public License for more details. | |
|
11 | # | |
|
12 | # You should have received a copy of the GNU Affero General Public License | |
|
13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
|
14 | # | |
|
15 | # This program is dual-licensed. If you wish to learn more about the | |
|
16 | # RhodeCode Enterprise Edition, including its added features, Support services, | |
|
17 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |
|
18 | ||
|
19 | from rhodecode.events.repo import RepoEvent | |
|
20 | ||
|
21 | ||
|
22 | class PullRequestEvent(RepoEvent): | |
|
23 | """ | |
|
24 | Base class for events acting on a repository. | |
|
25 | ||
|
26 | :param repo: a :class:`Repository` instance | |
|
27 | """ | |
|
28 | def __init__(self, pullrequest): | |
|
29 | super(PullRequestEvent, self).__init__(pullrequest.target_repo) | |
|
30 | self.pullrequest = pullrequest | |
|
31 | ||
|
32 | ||
|
33 | class PullRequestCreateEvent(PullRequestEvent): | |
|
34 | """ | |
|
35 | An instance of this class is emitted as an :term:`event` after a pull | |
|
36 | request is created. | |
|
37 | """ | |
|
38 | name = 'pullrequest-create' | |
|
39 | ||
|
40 | ||
|
41 | class PullRequestCloseEvent(PullRequestEvent): | |
|
42 | """ | |
|
43 | An instance of this class is emitted as an :term:`event` after a pull | |
|
44 | request is closed. | |
|
45 | """ | |
|
46 | name = 'pullrequest-close' | |
|
47 | ||
|
48 | ||
|
49 | class PullRequestUpdateEvent(PullRequestEvent): | |
|
50 | """ | |
|
51 | An instance of this class is emitted as an :term:`event` after a pull | |
|
52 | request is updated. | |
|
53 | """ | |
|
54 | name = 'pullrequest-update' | |
|
55 | ||
|
56 | ||
|
57 | class PullRequestMergeEvent(PullRequestEvent): | |
|
58 | """ | |
|
59 | An instance of this class is emitted as an :term:`event` after a pull | |
|
60 | request is merged. | |
|
61 | """ | |
|
62 | name = 'pullrequest-merge' | |
|
63 | ||
|
64 | ||
|
65 | class PullRequestReviewEvent(PullRequestEvent): | |
|
66 | """ | |
|
67 | An instance of this class is emitted as an :term:`event` after a pull | |
|
68 | request is reviewed. | |
|
69 | """ | |
|
70 | name = 'pullrequest-review' | |
|
71 | ||
|
72 |
@@ -0,0 +1,61 b'' | |||
|
1 | # -*- coding: utf-8 -*- | |
|
2 | ||
|
3 | # Copyright (C) 2010-2016 RhodeCode GmbH | |
|
4 | # | |
|
5 | # This program is free software: you can redistribute it and/or modify | |
|
6 | # it under the terms of the GNU Affero General Public License, version 3 | |
|
7 | # (only), as published by the Free Software Foundation. | |
|
8 | # | |
|
9 | # This program is distributed in the hope that it will be useful, | |
|
10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | |
|
11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
|
12 | # GNU General Public License for more details. | |
|
13 | # | |
|
14 | # You should have received a copy of the GNU Affero General Public License | |
|
15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
|
16 | # | |
|
17 | # This program is dual-licensed. If you wish to learn more about the | |
|
18 | # RhodeCode Enterprise Edition, including its added features, Support services, | |
|
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |
|
20 | ||
|
21 | import pytest | |
|
22 | ||
|
23 | from rhodecode.tests.events.conftest import EventCatcher | |
|
24 | ||
|
25 | from rhodecode.model.pull_request import PullRequestModel | |
|
26 | from rhodecode.events import ( | |
|
27 | PullRequestCreateEvent, | |
|
28 | PullRequestUpdateEvent, | |
|
29 | PullRequestReviewEvent, | |
|
30 | PullRequestMergeEvent, | |
|
31 | PullRequestCloseEvent, | |
|
32 | ) | |
|
33 | ||
|
34 | ||
|
35 | @pytest.mark.backends("git", "hg") | |
|
36 | def test_create_pull_request_events(pr_util): | |
|
37 | with EventCatcher() as event_catcher: | |
|
38 | pr_util.create_pull_request() | |
|
39 | ||
|
40 | assert PullRequestCreateEvent in event_catcher.events_types | |
|
41 | ||
|
42 | ||
|
43 | @pytest.mark.backends("git", "hg") | |
|
44 | def test_close_pull_request_events(pr_util, user_admin): | |
|
45 | pr = pr_util.create_pull_request() | |
|
46 | ||
|
47 | with EventCatcher() as event_catcher: | |
|
48 | PullRequestModel().close_pull_request(pr, user_admin) | |
|
49 | ||
|
50 | assert PullRequestCloseEvent in event_catcher.events_types | |
|
51 | ||
|
52 | ||
|
53 | @pytest.mark.backends("git", "hg") | |
|
54 | def test_close_pull_request_with_comment_events(pr_util, user_admin): | |
|
55 | pr = pr_util.create_pull_request() | |
|
56 | ||
|
57 | with EventCatcher() as event_catcher: | |
|
58 | PullRequestModel().close_pull_request_with_comment( | |
|
59 | pr, user_admin, pr.target_repo) | |
|
60 | ||
|
61 | assert PullRequestCloseEvent in event_catcher.events_types |
@@ -1,51 +1,59 b'' | |||
|
1 | 1 | # Copyright (C) 2016-2016 RhodeCode GmbH |
|
2 | 2 | # |
|
3 | 3 | # This program is free software: you can redistribute it and/or modify |
|
4 | 4 | # it under the terms of the GNU Affero General Public License, version 3 |
|
5 | 5 | # (only), as published by the Free Software Foundation. |
|
6 | 6 | # |
|
7 | 7 | # This program is distributed in the hope that it will be useful, |
|
8 | 8 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
9 | 9 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
10 | 10 | # GNU General Public License for more details. |
|
11 | 11 | # |
|
12 | 12 | # You should have received a copy of the GNU Affero General Public License |
|
13 | 13 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
14 | 14 | # |
|
15 | 15 | # This program is dual-licensed. If you wish to learn more about the |
|
16 | 16 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
17 | 17 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
18 | 18 | |
|
19 | 19 | from pyramid.threadlocal import get_current_registry |
|
20 | 20 | |
|
21 | 21 | |
|
22 | 22 | class RhodecodeEvent(object): |
|
23 | 23 | """ |
|
24 | 24 | Base event class for all Rhodecode events |
|
25 | 25 | """ |
|
26 | 26 | |
|
27 | 27 | |
|
28 | 28 | def trigger(event): |
|
29 | 29 | """ |
|
30 | 30 | Helper method to send an event. This wraps the pyramid logic to send an |
|
31 | 31 | event. |
|
32 | 32 | """ |
|
33 | 33 | # For the first step we are using pyramids thread locals here. If the |
|
34 | 34 | # event mechanism works out as a good solution we should think about |
|
35 | 35 | # passing the registry as an argument to get rid of it. |
|
36 | 36 | registry = get_current_registry() |
|
37 | 37 | registry.notify(event) |
|
38 | 38 | |
|
39 | 39 | |
|
40 | 40 | from rhodecode.events.user import ( |
|
41 | 41 | UserPreCreate, |
|
42 | 42 | UserPreUpdate, |
|
43 | 43 | UserRegistered |
|
44 | 44 | ) |
|
45 | 45 | |
|
46 | 46 | from rhodecode.events.repo import ( |
|
47 | 47 | RepoPreCreateEvent, RepoCreatedEvent, |
|
48 | 48 | RepoPreDeleteEvent, RepoDeletedEvent, |
|
49 | 49 | RepoPrePushEvent, RepoPushEvent, |
|
50 | 50 | RepoPrePullEvent, RepoPullEvent, |
|
51 | 51 | ) |
|
52 | ||
|
53 | from rhodecode.events.pullrequest import ( | |
|
54 | PullRequestCreateEvent, | |
|
55 | PullRequestUpdateEvent, | |
|
56 | PullRequestReviewEvent, | |
|
57 | PullRequestMergeEvent, | |
|
58 | PullRequestCloseEvent, | |
|
59 | ) No newline at end of file |
@@ -1,156 +1,162 b'' | |||
|
1 | 1 | # -*- coding: utf-8 -*- |
|
2 | 2 | |
|
3 | 3 | # Copyright (C) 2010-2016 RhodeCode GmbH |
|
4 | 4 | # |
|
5 | 5 | # This program is free software: you can redistribute it and/or modify |
|
6 | 6 | # it under the terms of the GNU Affero General Public License, version 3 |
|
7 | 7 | # (only), as published by the Free Software Foundation. |
|
8 | 8 | # |
|
9 | 9 | # This program is distributed in the hope that it will be useful, |
|
10 | 10 | # but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 | 11 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 | 12 | # GNU General Public License for more details. |
|
13 | 13 | # |
|
14 | 14 | # You should have received a copy of the GNU Affero General Public License |
|
15 | 15 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
16 | 16 | # |
|
17 | 17 | # This program is dual-licensed. If you wish to learn more about the |
|
18 | 18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
19 | 19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
20 | 20 | |
|
21 | 21 | import pylons |
|
22 | 22 | import webob |
|
23 | 23 | |
|
24 | from rhodecode import events | |
|
24 | 25 | from rhodecode.lib import hooks_base |
|
25 | 26 | from rhodecode.lib import utils2 |
|
26 | 27 | |
|
27 | 28 | |
|
28 | 29 | def _get_rc_scm_extras(username, repo_name, repo_alias, action): |
|
29 | 30 | # TODO: johbo: Replace by vcs_operation_context and remove fully |
|
30 | 31 | from rhodecode.lib.base import vcs_operation_context |
|
31 | 32 | check_locking = action in ('pull', 'push') |
|
32 | 33 | |
|
33 | 34 | try: |
|
34 | 35 | environ = pylons.request.environ |
|
35 | 36 | except TypeError: |
|
36 | 37 | # we might use this outside of request context, let's fake the |
|
37 | 38 | # environ data |
|
38 | 39 | environ = webob.Request.blank('').environ |
|
39 | 40 | |
|
40 | 41 | extras = vcs_operation_context( |
|
41 | 42 | environ, repo_name, username, action, repo_alias, check_locking) |
|
42 | 43 | return utils2.AttributeDict(extras) |
|
43 | 44 | |
|
44 | 45 | |
|
45 | 46 | def trigger_post_push_hook( |
|
46 | 47 | username, action, repo_name, repo_alias, commit_ids): |
|
47 | 48 | """ |
|
48 | 49 | Triggers push action hooks |
|
49 | 50 | |
|
50 | 51 | :param username: username who pushes |
|
51 | 52 | :param action: push/push_local/push_remote |
|
52 | 53 | :param repo_name: name of repo |
|
53 | 54 | :param repo_alias: the type of SCM repo |
|
54 | 55 | :param commit_ids: list of commit ids that we pushed |
|
55 | 56 | """ |
|
56 | 57 | if repo_alias not in ('hg', 'git'): |
|
57 | 58 | return |
|
58 | 59 | |
|
59 | 60 | extras = _get_rc_scm_extras(username, repo_name, repo_alias, action) |
|
60 | 61 | extras.commit_ids = commit_ids |
|
61 | 62 | hooks_base.post_push(extras) |
|
62 | 63 | |
|
63 | 64 | |
|
64 | 65 | def trigger_log_create_pull_request_hook(username, repo_name, repo_alias, |
|
65 | 66 | pull_request): |
|
66 | 67 | """ |
|
67 | 68 | Triggers create pull request action hooks |
|
68 | 69 | |
|
69 | 70 | :param username: username who creates the pull request |
|
70 | 71 | :param repo_name: name of target repo |
|
71 | 72 | :param repo_alias: the type of SCM target repo |
|
72 | 73 | :param pull_request: the pull request that was created |
|
73 | 74 | """ |
|
74 | 75 | if repo_alias not in ('hg', 'git'): |
|
75 | 76 | return |
|
76 | 77 | |
|
77 | 78 | extras = _get_rc_scm_extras(username, repo_name, repo_alias, |
|
78 | 79 | 'create_pull_request') |
|
80 | events.trigger(events.PullRequestCreateEvent(pull_request)) | |
|
79 | 81 | extras.update(pull_request.get_api_data()) |
|
80 | 82 | hooks_base.log_create_pull_request(**extras) |
|
81 | 83 | |
|
82 | 84 | |
|
83 | 85 | def trigger_log_merge_pull_request_hook(username, repo_name, repo_alias, |
|
84 | 86 | pull_request): |
|
85 | 87 | """ |
|
86 | 88 | Triggers merge pull request action hooks |
|
87 | 89 | |
|
88 | 90 | :param username: username who creates the pull request |
|
89 | 91 | :param repo_name: name of target repo |
|
90 | 92 | :param repo_alias: the type of SCM target repo |
|
91 | 93 | :param pull_request: the pull request that was merged |
|
92 | 94 | """ |
|
93 | 95 | if repo_alias not in ('hg', 'git'): |
|
94 | 96 | return |
|
95 | 97 | |
|
96 | 98 | extras = _get_rc_scm_extras(username, repo_name, repo_alias, |
|
97 | 99 | 'merge_pull_request') |
|
100 | events.trigger(events.PullRequestMergeEvent(pull_request)) | |
|
98 | 101 | extras.update(pull_request.get_api_data()) |
|
99 | 102 | hooks_base.log_merge_pull_request(**extras) |
|
100 | 103 | |
|
101 | 104 | |
|
102 | 105 | def trigger_log_close_pull_request_hook(username, repo_name, repo_alias, |
|
103 | 106 | pull_request): |
|
104 | 107 | """ |
|
105 | 108 | Triggers close pull request action hooks |
|
106 | 109 | |
|
107 | 110 | :param username: username who creates the pull request |
|
108 | 111 | :param repo_name: name of target repo |
|
109 | 112 | :param repo_alias: the type of SCM target repo |
|
110 | 113 | :param pull_request: the pull request that was closed |
|
111 | 114 | """ |
|
112 | 115 | if repo_alias not in ('hg', 'git'): |
|
113 | 116 | return |
|
114 | 117 | |
|
115 | 118 | extras = _get_rc_scm_extras(username, repo_name, repo_alias, |
|
116 | 119 | 'close_pull_request') |
|
120 | events.trigger(events.PullRequestCloseEvent(pull_request)) | |
|
117 | 121 | extras.update(pull_request.get_api_data()) |
|
118 | 122 | hooks_base.log_close_pull_request(**extras) |
|
119 | 123 | |
|
120 | 124 | |
|
121 | 125 | def trigger_log_review_pull_request_hook(username, repo_name, repo_alias, |
|
122 | 126 | pull_request): |
|
123 | 127 | """ |
|
124 | 128 | Triggers review status change pull request action hooks |
|
125 | 129 | |
|
126 | 130 | :param username: username who creates the pull request |
|
127 | 131 | :param repo_name: name of target repo |
|
128 | 132 | :param repo_alias: the type of SCM target repo |
|
129 | 133 | :param pull_request: the pull request that review status changed |
|
130 | 134 | """ |
|
131 | 135 | if repo_alias not in ('hg', 'git'): |
|
132 | 136 | return |
|
133 | 137 | |
|
134 | 138 | extras = _get_rc_scm_extras(username, repo_name, repo_alias, |
|
135 | 139 | 'review_pull_request') |
|
140 | events.trigger(events.PullRequestReviewEvent(pull_request)) | |
|
136 | 141 | extras.update(pull_request.get_api_data()) |
|
137 | 142 | hooks_base.log_review_pull_request(**extras) |
|
138 | 143 | |
|
139 | 144 | |
|
140 | 145 | def trigger_log_update_pull_request_hook(username, repo_name, repo_alias, |
|
141 | 146 | pull_request): |
|
142 | 147 | """ |
|
143 | 148 | Triggers update pull request action hooks |
|
144 | 149 | |
|
145 | 150 | :param username: username who creates the pull request |
|
146 | 151 | :param repo_name: name of target repo |
|
147 | 152 | :param repo_alias: the type of SCM target repo |
|
148 | 153 | :param pull_request: the pull request that was updated |
|
149 | 154 | """ |
|
150 | 155 | if repo_alias not in ('hg', 'git'): |
|
151 | 156 | return |
|
152 | 157 | |
|
153 | 158 | extras = _get_rc_scm_extras(username, repo_name, repo_alias, |
|
154 | 159 | 'update_pull_request') |
|
160 | events.trigger(events.PullRequestUpdateEvent(pull_request)) | |
|
155 | 161 | extras.update(pull_request.get_api_data()) |
|
156 | 162 | hooks_base.log_update_pull_request(**extras) |
General Comments 0
You need to be logged in to leave comments.
Login now