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