Show More
@@ -1,316 +1,350 b'' | |||||
1 | # -*- coding: utf-8 -*- |
|
1 | # -*- coding: utf-8 -*- | |
2 |
|
2 | |||
3 | # Copyright (C) 2012-2017 RhodeCode GmbH |
|
3 | # Copyright (C) 2012-2017 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 | from __future__ import unicode_literals |
|
21 | from __future__ import unicode_literals | |
22 | import string |
|
22 | import string | |
23 | from collections import OrderedDict |
|
23 | from collections import OrderedDict | |
24 |
|
24 | |||
25 | import deform |
|
25 | import deform | |
|
26 | import deform.widget | |||
26 | import logging |
|
27 | import logging | |
27 | import requests |
|
28 | import requests | |
|
29 | import requests.adapters | |||
28 | import colander |
|
30 | import colander | |
29 | from celery.task import task |
|
31 | from celery.task import task | |
30 | from requests.packages.urllib3.util.retry import Retry |
|
32 | from requests.packages.urllib3.util.retry import Retry | |
31 |
|
33 | |||
32 | import rhodecode |
|
34 | import rhodecode | |
33 | from rhodecode import events |
|
35 | from rhodecode import events | |
34 | from rhodecode.translation import _ |
|
36 | from rhodecode.translation import _ | |
35 | from rhodecode.integrations.types.base import IntegrationTypeBase |
|
37 | from rhodecode.integrations.types.base import IntegrationTypeBase | |
36 |
|
38 | |||
37 | log = logging.getLogger(__name__) |
|
39 | log = logging.getLogger(__name__) | |
38 |
|
40 | |||
39 | # updating this required to update the `common_vars` passed in url calling func |
|
41 | # updating this required to update the `common_vars` passed in url calling func | |
40 | WEBHOOK_URL_VARS = [ |
|
42 | WEBHOOK_URL_VARS = [ | |
41 | 'repo_name', |
|
43 | 'repo_name', | |
42 | 'repo_type', |
|
44 | 'repo_type', | |
43 | 'repo_id', |
|
45 | 'repo_id', | |
44 | 'repo_url', |
|
46 | 'repo_url', | |
45 | # extra repo fields |
|
47 | # extra repo fields | |
46 | 'extra:<extra_key_name>', |
|
48 | 'extra:<extra_key_name>', | |
47 |
|
49 | |||
48 | # special attrs below that we handle, using multi-call |
|
50 | # special attrs below that we handle, using multi-call | |
49 | 'branch', |
|
51 | 'branch', | |
50 | 'commit_id', |
|
52 | 'commit_id', | |
51 |
|
53 | |||
52 | # pr events vars |
|
54 | # pr events vars | |
53 | 'pull_request_id', |
|
55 | 'pull_request_id', | |
54 | 'pull_request_url', |
|
56 | 'pull_request_url', | |
55 |
|
57 | |||
56 | # user who triggers the call |
|
58 | # user who triggers the call | |
57 | 'username', |
|
59 | 'username', | |
58 | 'user_id', |
|
60 | 'user_id', | |
59 |
|
61 | |||
60 | ] |
|
62 | ] | |
61 | URL_VARS = ', '.join('${' + x + '}' for x in WEBHOOK_URL_VARS) |
|
63 | URL_VARS = ', '.join('${' + x + '}' for x in WEBHOOK_URL_VARS) | |
62 |
|
64 | |||
63 |
|
65 | |||
|
66 | def get_auth(settings): | |||
|
67 | from requests.auth import HTTPBasicAuth | |||
|
68 | username = settings.get('username') | |||
|
69 | password = settings.get('password') | |||
|
70 | if username and password: | |||
|
71 | return HTTPBasicAuth(username, password) | |||
|
72 | return None | |||
|
73 | ||||
|
74 | ||||
64 | class WebhookHandler(object): |
|
75 | class WebhookHandler(object): | |
65 | def __init__(self, template_url, secret_token, headers): |
|
76 | def __init__(self, template_url, secret_token, headers): | |
66 | self.template_url = template_url |
|
77 | self.template_url = template_url | |
67 | self.secret_token = secret_token |
|
78 | self.secret_token = secret_token | |
68 | self.headers = headers |
|
79 | self.headers = headers | |
69 |
|
80 | |||
70 | def get_base_parsed_template(self, data): |
|
81 | def get_base_parsed_template(self, data): | |
71 | """ |
|
82 | """ | |
72 | initially parses the passed in template with some common variables |
|
83 | initially parses the passed in template with some common variables | |
73 | available on ALL calls |
|
84 | available on ALL calls | |
74 | """ |
|
85 | """ | |
75 | # note: make sure to update the `WEBHOOK_URL_VARS` if this changes |
|
86 | # note: make sure to update the `WEBHOOK_URL_VARS` if this changes | |
76 | common_vars = { |
|
87 | common_vars = { | |
77 | 'repo_name': data['repo']['repo_name'], |
|
88 | 'repo_name': data['repo']['repo_name'], | |
78 | 'repo_type': data['repo']['repo_type'], |
|
89 | 'repo_type': data['repo']['repo_type'], | |
79 | 'repo_id': data['repo']['repo_id'], |
|
90 | 'repo_id': data['repo']['repo_id'], | |
80 | 'repo_url': data['repo']['url'], |
|
91 | 'repo_url': data['repo']['url'], | |
81 | 'username': data['actor']['username'], |
|
92 | 'username': data['actor']['username'], | |
82 | 'user_id': data['actor']['user_id'] |
|
93 | 'user_id': data['actor']['user_id'] | |
83 | } |
|
94 | } | |
84 | extra_vars = {} |
|
95 | extra_vars = {} | |
85 | for extra_key, extra_val in data['repo']['extra_fields'].items(): |
|
96 | for extra_key, extra_val in data['repo']['extra_fields'].items(): | |
86 | extra_vars['extra:{}'.format(extra_key)] = extra_val |
|
97 | extra_vars['extra:{}'.format(extra_key)] = extra_val | |
87 | common_vars.update(extra_vars) |
|
98 | common_vars.update(extra_vars) | |
88 |
|
99 | |||
89 | return string.Template( |
|
100 | return string.Template( | |
90 | self.template_url).safe_substitute(**common_vars) |
|
101 | self.template_url).safe_substitute(**common_vars) | |
91 |
|
102 | |||
92 | def repo_push_event_handler(self, event, data): |
|
103 | def repo_push_event_handler(self, event, data): | |
93 | url = self.get_base_parsed_template(data) |
|
104 | url = self.get_base_parsed_template(data) | |
94 | url_cals = [] |
|
105 | url_cals = [] | |
95 | branch_data = OrderedDict() |
|
106 | branch_data = OrderedDict() | |
96 | for obj in data['push']['branches']: |
|
107 | for obj in data['push']['branches']: | |
97 | branch_data[obj['name']] = obj |
|
108 | branch_data[obj['name']] = obj | |
98 |
|
109 | |||
99 | branches_commits = OrderedDict() |
|
110 | branches_commits = OrderedDict() | |
100 | for commit in data['push']['commits']: |
|
111 | for commit in data['push']['commits']: | |
101 | if commit['branch'] not in branches_commits: |
|
112 | if commit['branch'] not in branches_commits: | |
102 | branch_commits = {'branch': branch_data[commit['branch']], |
|
113 | branch_commits = {'branch': branch_data[commit['branch']], | |
103 | 'commits': []} |
|
114 | 'commits': []} | |
104 | branches_commits[commit['branch']] = branch_commits |
|
115 | branches_commits[commit['branch']] = branch_commits | |
105 |
|
116 | |||
106 | branch_commits = branches_commits[commit['branch']] |
|
117 | branch_commits = branches_commits[commit['branch']] | |
107 | branch_commits['commits'].append(commit) |
|
118 | branch_commits['commits'].append(commit) | |
108 |
|
119 | |||
109 | if '${branch}' in url: |
|
120 | if '${branch}' in url: | |
110 | # call it multiple times, for each branch if used in variables |
|
121 | # call it multiple times, for each branch if used in variables | |
111 | for branch, commit_ids in branches_commits.items(): |
|
122 | for branch, commit_ids in branches_commits.items(): | |
112 | branch_url = string.Template(url).safe_substitute(branch=branch) |
|
123 | branch_url = string.Template(url).safe_substitute(branch=branch) | |
113 | # call further down for each commit if used |
|
124 | # call further down for each commit if used | |
114 | if '${commit_id}' in branch_url: |
|
125 | if '${commit_id}' in branch_url: | |
115 | for commit_data in commit_ids['commits']: |
|
126 | for commit_data in commit_ids['commits']: | |
116 | commit_id = commit_data['raw_id'] |
|
127 | commit_id = commit_data['raw_id'] | |
117 | commit_url = string.Template(branch_url).safe_substitute( |
|
128 | commit_url = string.Template(branch_url).safe_substitute( | |
118 | commit_id=commit_id) |
|
129 | commit_id=commit_id) | |
119 | # register per-commit call |
|
130 | # register per-commit call | |
120 | log.debug( |
|
131 | log.debug( | |
121 | 'register webhook call(%s) to url %s', event, commit_url) |
|
132 | 'register webhook call(%s) to url %s', event, commit_url) | |
122 | url_cals.append((commit_url, self.secret_token, self.headers, data)) |
|
133 | url_cals.append((commit_url, self.secret_token, self.headers, data)) | |
123 |
|
134 | |||
124 | else: |
|
135 | else: | |
125 | # register per-branch call |
|
136 | # register per-branch call | |
126 | log.debug( |
|
137 | log.debug( | |
127 | 'register webhook call(%s) to url %s', event, branch_url) |
|
138 | 'register webhook call(%s) to url %s', event, branch_url) | |
128 | url_cals.append((branch_url, self.secret_token, self.headers, data)) |
|
139 | url_cals.append((branch_url, self.secret_token, self.headers, data)) | |
129 |
|
140 | |||
130 | else: |
|
141 | else: | |
131 | log.debug( |
|
142 | log.debug( | |
132 | 'register webhook call(%s) to url %s', event, url) |
|
143 | 'register webhook call(%s) to url %s', event, url) | |
133 | url_cals.append((url, self.secret_token, self.headers, data)) |
|
144 | url_cals.append((url, self.secret_token, self.headers, data)) | |
134 |
|
145 | |||
135 | return url_cals |
|
146 | return url_cals | |
136 |
|
147 | |||
137 | def repo_create_event_handler(self, event, data): |
|
148 | def repo_create_event_handler(self, event, data): | |
138 | url = self.get_base_parsed_template(data) |
|
149 | url = self.get_base_parsed_template(data) | |
139 | log.debug( |
|
150 | log.debug( | |
140 | 'register webhook call(%s) to url %s', event, url) |
|
151 | 'register webhook call(%s) to url %s', event, url) | |
141 | return [(url, self.secret_token, self.headers, data)] |
|
152 | return [(url, self.secret_token, self.headers, data)] | |
142 |
|
153 | |||
143 | def pull_request_event_handler(self, event, data): |
|
154 | def pull_request_event_handler(self, event, data): | |
144 | url = self.get_base_parsed_template(data) |
|
155 | url = self.get_base_parsed_template(data) | |
145 | log.debug( |
|
156 | log.debug( | |
146 | 'register webhook call(%s) to url %s', event, url) |
|
157 | 'register webhook call(%s) to url %s', event, url) | |
147 | url = string.Template(url).safe_substitute( |
|
158 | url = string.Template(url).safe_substitute( | |
148 | pull_request_id=data['pullrequest']['pull_request_id'], |
|
159 | pull_request_id=data['pullrequest']['pull_request_id'], | |
149 | pull_request_url=data['pullrequest']['url']) |
|
160 | pull_request_url=data['pullrequest']['url']) | |
150 | return [(url, self.secret_token, self.headers, data)] |
|
161 | return [(url, self.secret_token, self.headers, data)] | |
151 |
|
162 | |||
152 | def __call__(self, event, data): |
|
163 | def __call__(self, event, data): | |
153 | if isinstance(event, events.RepoPushEvent): |
|
164 | if isinstance(event, events.RepoPushEvent): | |
154 | return self.repo_push_event_handler(event, data) |
|
165 | return self.repo_push_event_handler(event, data) | |
155 | elif isinstance(event, events.RepoCreateEvent): |
|
166 | elif isinstance(event, events.RepoCreateEvent): | |
156 | return self.repo_create_event_handler(event, data) |
|
167 | return self.repo_create_event_handler(event, data) | |
157 | elif isinstance(event, events.PullRequestEvent): |
|
168 | elif isinstance(event, events.PullRequestEvent): | |
158 | return self.pull_request_event_handler(event, data) |
|
169 | return self.pull_request_event_handler(event, data) | |
159 | else: |
|
170 | else: | |
160 | raise ValueError('event type not supported: %s' % events) |
|
171 | raise ValueError('event type not supported: %s' % events) | |
161 |
|
172 | |||
162 |
|
173 | |||
163 | class WebhookSettingsSchema(colander.Schema): |
|
174 | class WebhookSettingsSchema(colander.Schema): | |
164 | url = colander.SchemaNode( |
|
175 | url = colander.SchemaNode( | |
165 | colander.String(), |
|
176 | colander.String(), | |
166 | title=_('Webhook URL'), |
|
177 | title=_('Webhook URL'), | |
167 | description= |
|
178 | description= | |
168 | _('URL to which Webhook should submit data. Following variables ' |
|
179 | _('URL to which Webhook should submit data. Following variables ' | |
169 | 'are allowed to be used: {vars}. Some of the variables would ' |
|
180 | 'are allowed to be used: {vars}. Some of the variables would ' | |
170 | 'trigger multiple calls, like ${{branch}} or ${{commit_id}}. ' |
|
181 | 'trigger multiple calls, like ${{branch}} or ${{commit_id}}. ' | |
171 | 'Webhook will be called as many times as unique objects in ' |
|
182 | 'Webhook will be called as many times as unique objects in ' | |
172 | 'data in such cases.').format(vars=URL_VARS), |
|
183 | 'data in such cases.').format(vars=URL_VARS), | |
173 | missing=colander.required, |
|
184 | missing=colander.required, | |
174 | required=True, |
|
185 | required=True, | |
175 | validator=colander.url, |
|
186 | validator=colander.url, | |
176 | widget=deform.widget.TextInputWidget( |
|
187 | widget=deform.widget.TextInputWidget( | |
177 | placeholder='https://www.example.com/webhook' |
|
188 | placeholder='https://www.example.com/webhook' | |
178 | ), |
|
189 | ), | |
179 | ) |
|
190 | ) | |
180 | secret_token = colander.SchemaNode( |
|
191 | secret_token = colander.SchemaNode( | |
181 | colander.String(), |
|
192 | colander.String(), | |
182 | title=_('Secret Token'), |
|
193 | title=_('Secret Token'), | |
183 | description=_('Optional string used to validate received payloads. ' |
|
194 | description=_('Optional string used to validate received payloads. ' | |
184 | 'It will be sent together with event data in JSON'), |
|
195 | 'It will be sent together with event data in JSON'), | |
185 | default='', |
|
196 | default='', | |
186 | missing='', |
|
197 | missing='', | |
187 | widget=deform.widget.TextInputWidget( |
|
198 | widget=deform.widget.TextInputWidget( | |
188 | placeholder='e.g. secret_token' |
|
199 | placeholder='e.g. secret_token' | |
189 | ), |
|
200 | ), | |
190 | ) |
|
201 | ) | |
|
202 | username = colander.SchemaNode( | |||
|
203 | colander.String(), | |||
|
204 | title=_('Username'), | |||
|
205 | description=_('Optional username to authenticate the call.'), | |||
|
206 | default='', | |||
|
207 | missing='', | |||
|
208 | widget=deform.widget.TextInputWidget( | |||
|
209 | placeholder='e.g. admin' | |||
|
210 | ), | |||
|
211 | ) | |||
|
212 | password = colander.SchemaNode( | |||
|
213 | colander.String(), | |||
|
214 | title=_('Password'), | |||
|
215 | description=_('Optional password to authenticate the call.'), | |||
|
216 | default='', | |||
|
217 | missing='', | |||
|
218 | widget=deform.widget.PasswordWidget( | |||
|
219 | placeholder='e.g. secret.', | |||
|
220 | redisplay=True, | |||
|
221 | ), | |||
|
222 | ) | |||
191 | custom_header_key = colander.SchemaNode( |
|
223 | custom_header_key = colander.SchemaNode( | |
192 | colander.String(), |
|
224 | colander.String(), | |
193 | title=_('Custom Header Key'), |
|
225 | title=_('Custom Header Key'), | |
194 | description=_('Custom Header name to be set when calling endpoint.'), |
|
226 | description=_('Custom Header name to be set when calling endpoint.'), | |
195 | default='', |
|
227 | default='', | |
196 | missing='', |
|
228 | missing='', | |
197 | widget=deform.widget.TextInputWidget( |
|
229 | widget=deform.widget.TextInputWidget( | |
198 | placeholder='e.g.Authorization' |
|
230 | placeholder='e.g.Authorization' | |
199 | ), |
|
231 | ), | |
200 | ) |
|
232 | ) | |
201 | custom_header_val = colander.SchemaNode( |
|
233 | custom_header_val = colander.SchemaNode( | |
202 | colander.String(), |
|
234 | colander.String(), | |
203 | title=_('Custom Header Value'), |
|
235 | title=_('Custom Header Value'), | |
204 | description=_('Custom Header value to be set when calling endpoint.'), |
|
236 | description=_('Custom Header value to be set when calling endpoint.'), | |
205 | default='', |
|
237 | default='', | |
206 | missing='', |
|
238 | missing='', | |
207 | widget=deform.widget.TextInputWidget( |
|
239 | widget=deform.widget.TextInputWidget( | |
208 | placeholder='e.g. RcLogin auth=xxxx' |
|
240 | placeholder='e.g. RcLogin auth=xxxx' | |
209 | ), |
|
241 | ), | |
210 | ) |
|
242 | ) | |
211 | method_type = colander.SchemaNode( |
|
243 | method_type = colander.SchemaNode( | |
212 | colander.String(), |
|
244 | colander.String(), | |
213 | title=_('Call Method'), |
|
245 | title=_('Call Method'), | |
214 | description=_('Select if the Webhook call should be made ' |
|
246 | description=_('Select if the Webhook call should be made ' | |
215 | 'with POST or GET.'), |
|
247 | 'with POST or GET.'), | |
216 | default='post', |
|
248 | default='post', | |
217 | missing='', |
|
249 | missing='', | |
218 | widget=deform.widget.RadioChoiceWidget( |
|
250 | widget=deform.widget.RadioChoiceWidget( | |
219 | values=[('get', 'GET'), ('post', 'POST')], |
|
251 | values=[('get', 'GET'), ('post', 'POST')], | |
220 | inline=True |
|
252 | inline=True | |
221 | ), |
|
253 | ), | |
222 | ) |
|
254 | ) | |
223 |
|
255 | |||
224 |
|
256 | |||
225 | class WebhookIntegrationType(IntegrationTypeBase): |
|
257 | class WebhookIntegrationType(IntegrationTypeBase): | |
226 | key = 'webhook' |
|
258 | key = 'webhook' | |
227 | display_name = _('Webhook') |
|
259 | display_name = _('Webhook') | |
228 | description = _('Post json events to a Webhook endpoint') |
|
260 | description = _('Post json events to a Webhook endpoint') | |
229 | icon = '''<?xml version="1.0" encoding="UTF-8" standalone="no"?><svg viewBox="0 0 256 239" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMid"><g><path d="M119.540432,100.502743 C108.930124,118.338815 98.7646301,135.611455 88.3876025,152.753617 C85.7226696,157.154315 84.4040417,160.738531 86.5332204,166.333309 C92.4107024,181.787152 84.1193605,196.825836 68.5350381,200.908244 C53.8383677,204.759349 39.5192953,195.099955 36.6032893,179.365384 C34.0194114,165.437749 44.8274148,151.78491 60.1824106,149.608284 C61.4694072,149.424428 62.7821041,149.402681 64.944891,149.240571 C72.469175,136.623655 80.1773157,123.700312 88.3025935,110.073173 C73.611854,95.4654658 64.8677898,78.3885437 66.803227,57.2292132 C68.1712787,42.2715849 74.0527146,29.3462646 84.8033863,18.7517722 C105.393354,-1.53572199 136.805164,-4.82141828 161.048542,10.7510424 C184.333097,25.7086706 194.996783,54.8450075 185.906752,79.7822957 C179.052655,77.9239597 172.151111,76.049808 164.563565,73.9917997 C167.418285,60.1274266 165.306899,47.6765751 155.95591,37.0109123 C149.777932,29.9690049 141.850349,26.2780332 132.835442,24.9178894 C114.764113,22.1877169 97.0209573,33.7983633 91.7563309,51.5355878 C85.7800012,71.6669027 94.8245623,88.1111998 119.540432,100.502743 L119.540432,100.502743 Z" fill="#C73A63"></path><path d="M149.841194,79.4106285 C157.316054,92.5969067 164.905578,105.982857 172.427885,119.246236 C210.44865,107.483365 239.114472,128.530009 249.398582,151.063322 C261.81978,178.282014 253.328765,210.520191 228.933162,227.312431 C203.893073,244.551464 172.226236,241.605803 150.040866,219.46195 C155.694953,214.729124 161.376716,209.974552 167.44794,204.895759 C189.360489,219.088306 208.525074,218.420096 222.753207,201.614016 C234.885769,187.277151 234.622834,165.900356 222.138374,151.863988 C207.730339,135.66681 188.431321,135.172572 165.103273,150.721309 C155.426087,133.553447 145.58086,116.521995 136.210101,99.2295848 C133.05093,93.4015266 129.561608,90.0209366 122.440622,88.7873178 C110.547271,86.7253555 102.868785,76.5124151 102.408155,65.0698097 C101.955433,53.7537294 108.621719,43.5249733 119.04224,39.5394355 C129.363912,35.5914599 141.476705,38.7783085 148.419765,47.554004 C154.093621,54.7244134 155.896602,62.7943365 152.911402,71.6372484 C152.081082,74.1025091 151.00562,76.4886916 149.841194,79.4106285 L149.841194,79.4106285 Z" fill="#4B4B4B"></path><path d="M167.706921,187.209935 L121.936499,187.209935 C117.54964,205.253587 108.074103,219.821756 91.7464461,229.085759 C79.0544063,236.285822 65.3738898,238.72736 50.8136292,236.376762 C24.0061432,232.053165 2.08568567,207.920497 0.156179306,180.745298 C-2.02835403,149.962159 19.1309765,122.599149 47.3341915,116.452801 C49.2814904,123.524363 51.2485589,130.663141 53.1958579,137.716911 C27.3195169,150.919004 18.3639187,167.553089 25.6054984,188.352614 C31.9811726,206.657224 50.0900643,216.690262 69.7528413,212.809503 C89.8327554,208.847688 99.9567329,192.160226 98.7211371,165.37844 C117.75722,165.37844 136.809118,165.180745 155.847178,165.475311 C163.280522,165.591951 169.019617,164.820939 174.620326,158.267339 C183.840836,147.48306 200.811003,148.455721 210.741239,158.640984 C220.88894,169.049642 220.402609,185.79839 209.663799,195.768166 C199.302587,205.38802 182.933414,204.874012 173.240413,194.508846 C171.247644,192.37176 169.677943,189.835329 167.706921,187.209935 L167.706921,187.209935 Z" fill="#4A4A4A"></path></g></svg>''' |
|
261 | icon = '''<?xml version="1.0" encoding="UTF-8" standalone="no"?><svg viewBox="0 0 256 239" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" preserveAspectRatio="xMidYMid"><g><path d="M119.540432,100.502743 C108.930124,118.338815 98.7646301,135.611455 88.3876025,152.753617 C85.7226696,157.154315 84.4040417,160.738531 86.5332204,166.333309 C92.4107024,181.787152 84.1193605,196.825836 68.5350381,200.908244 C53.8383677,204.759349 39.5192953,195.099955 36.6032893,179.365384 C34.0194114,165.437749 44.8274148,151.78491 60.1824106,149.608284 C61.4694072,149.424428 62.7821041,149.402681 64.944891,149.240571 C72.469175,136.623655 80.1773157,123.700312 88.3025935,110.073173 C73.611854,95.4654658 64.8677898,78.3885437 66.803227,57.2292132 C68.1712787,42.2715849 74.0527146,29.3462646 84.8033863,18.7517722 C105.393354,-1.53572199 136.805164,-4.82141828 161.048542,10.7510424 C184.333097,25.7086706 194.996783,54.8450075 185.906752,79.7822957 C179.052655,77.9239597 172.151111,76.049808 164.563565,73.9917997 C167.418285,60.1274266 165.306899,47.6765751 155.95591,37.0109123 C149.777932,29.9690049 141.850349,26.2780332 132.835442,24.9178894 C114.764113,22.1877169 97.0209573,33.7983633 91.7563309,51.5355878 C85.7800012,71.6669027 94.8245623,88.1111998 119.540432,100.502743 L119.540432,100.502743 Z" fill="#C73A63"></path><path d="M149.841194,79.4106285 C157.316054,92.5969067 164.905578,105.982857 172.427885,119.246236 C210.44865,107.483365 239.114472,128.530009 249.398582,151.063322 C261.81978,178.282014 253.328765,210.520191 228.933162,227.312431 C203.893073,244.551464 172.226236,241.605803 150.040866,219.46195 C155.694953,214.729124 161.376716,209.974552 167.44794,204.895759 C189.360489,219.088306 208.525074,218.420096 222.753207,201.614016 C234.885769,187.277151 234.622834,165.900356 222.138374,151.863988 C207.730339,135.66681 188.431321,135.172572 165.103273,150.721309 C155.426087,133.553447 145.58086,116.521995 136.210101,99.2295848 C133.05093,93.4015266 129.561608,90.0209366 122.440622,88.7873178 C110.547271,86.7253555 102.868785,76.5124151 102.408155,65.0698097 C101.955433,53.7537294 108.621719,43.5249733 119.04224,39.5394355 C129.363912,35.5914599 141.476705,38.7783085 148.419765,47.554004 C154.093621,54.7244134 155.896602,62.7943365 152.911402,71.6372484 C152.081082,74.1025091 151.00562,76.4886916 149.841194,79.4106285 L149.841194,79.4106285 Z" fill="#4B4B4B"></path><path d="M167.706921,187.209935 L121.936499,187.209935 C117.54964,205.253587 108.074103,219.821756 91.7464461,229.085759 C79.0544063,236.285822 65.3738898,238.72736 50.8136292,236.376762 C24.0061432,232.053165 2.08568567,207.920497 0.156179306,180.745298 C-2.02835403,149.962159 19.1309765,122.599149 47.3341915,116.452801 C49.2814904,123.524363 51.2485589,130.663141 53.1958579,137.716911 C27.3195169,150.919004 18.3639187,167.553089 25.6054984,188.352614 C31.9811726,206.657224 50.0900643,216.690262 69.7528413,212.809503 C89.8327554,208.847688 99.9567329,192.160226 98.7211371,165.37844 C117.75722,165.37844 136.809118,165.180745 155.847178,165.475311 C163.280522,165.591951 169.019617,164.820939 174.620326,158.267339 C183.840836,147.48306 200.811003,148.455721 210.741239,158.640984 C220.88894,169.049642 220.402609,185.79839 209.663799,195.768166 C199.302587,205.38802 182.933414,204.874012 173.240413,194.508846 C171.247644,192.37176 169.677943,189.835329 167.706921,187.209935 L167.706921,187.209935 Z" fill="#4A4A4A"></path></g></svg>''' | |
230 |
|
262 | |||
231 | valid_events = [ |
|
263 | valid_events = [ | |
232 | events.PullRequestCloseEvent, |
|
264 | events.PullRequestCloseEvent, | |
233 | events.PullRequestMergeEvent, |
|
265 | events.PullRequestMergeEvent, | |
234 | events.PullRequestUpdateEvent, |
|
266 | events.PullRequestUpdateEvent, | |
235 | events.PullRequestCommentEvent, |
|
267 | events.PullRequestCommentEvent, | |
236 | events.PullRequestReviewEvent, |
|
268 | events.PullRequestReviewEvent, | |
237 | events.PullRequestCreateEvent, |
|
269 | events.PullRequestCreateEvent, | |
238 | events.RepoPushEvent, |
|
270 | events.RepoPushEvent, | |
239 | events.RepoCreateEvent, |
|
271 | events.RepoCreateEvent, | |
240 | ] |
|
272 | ] | |
241 |
|
273 | |||
242 | def settings_schema(self): |
|
274 | def settings_schema(self): | |
243 | schema = WebhookSettingsSchema() |
|
275 | schema = WebhookSettingsSchema() | |
244 | schema.add(colander.SchemaNode( |
|
276 | schema.add(colander.SchemaNode( | |
245 | colander.Set(), |
|
277 | colander.Set(), | |
246 | widget=deform.widget.CheckboxChoiceWidget( |
|
278 | widget=deform.widget.CheckboxChoiceWidget( | |
247 | values=sorted( |
|
279 | values=sorted( | |
248 | [(e.name, e.display_name) for e in self.valid_events] |
|
280 | [(e.name, e.display_name) for e in self.valid_events] | |
249 | ) |
|
281 | ) | |
250 | ), |
|
282 | ), | |
251 | description="Events activated for this integration", |
|
283 | description="Events activated for this integration", | |
252 | name='events' |
|
284 | name='events' | |
253 | )) |
|
285 | )) | |
254 | return schema |
|
286 | return schema | |
255 |
|
287 | |||
256 | def send_event(self, event): |
|
288 | def send_event(self, event): | |
257 | log.debug('handling event %s with Webhook integration %s', |
|
289 | log.debug('handling event %s with Webhook integration %s', | |
258 | event.name, self) |
|
290 | event.name, self) | |
259 |
|
291 | |||
260 | if event.__class__ not in self.valid_events: |
|
292 | if event.__class__ not in self.valid_events: | |
261 | log.debug('event not valid: %r' % event) |
|
293 | log.debug('event not valid: %r' % event) | |
262 | return |
|
294 | return | |
263 |
|
295 | |||
264 | if event.name not in self.settings['events']: |
|
296 | if event.name not in self.settings['events']: | |
265 | log.debug('event ignored: %r' % event) |
|
297 | log.debug('event ignored: %r' % event) | |
266 | return |
|
298 | return | |
267 |
|
299 | |||
268 | data = event.as_dict() |
|
300 | data = event.as_dict() | |
269 | template_url = self.settings['url'] |
|
301 | template_url = self.settings['url'] | |
270 |
|
302 | |||
271 | headers = {} |
|
303 | headers = {} | |
272 |
head_key = self.settings |
|
304 | head_key = self.settings.get('custom_header_key') | |
273 |
head_val = self.settings |
|
305 | head_val = self.settings.get('custom_header_val') | |
274 | if head_key and head_val: |
|
306 | if head_key and head_val: | |
275 | headers = {head_key: head_val} |
|
307 | headers = {head_key: head_val} | |
276 |
|
308 | |||
277 | handler = WebhookHandler( |
|
309 | handler = WebhookHandler( | |
278 | template_url, self.settings['secret_token'], headers) |
|
310 | template_url, self.settings['secret_token'], headers) | |
279 |
|
311 | |||
280 | url_calls = handler(event, data) |
|
312 | url_calls = handler(event, data) | |
281 | log.debug('webhook: calling following urls: %s', |
|
313 | log.debug('webhook: calling following urls: %s', | |
282 | [x[0] for x in url_calls]) |
|
314 | [x[0] for x in url_calls]) | |
283 | post_to_webhook(url_calls, self.settings) |
|
315 | post_to_webhook(url_calls, self.settings) | |
284 |
|
316 | |||
285 |
|
317 | |||
286 | @task(ignore_result=True) |
|
318 | @task(ignore_result=True) | |
287 | def post_to_webhook(url_calls, settings): |
|
319 | def post_to_webhook(url_calls, settings): | |
288 | max_retries = 3 |
|
320 | max_retries = 3 | |
289 | retries = Retry( |
|
321 | retries = Retry( | |
290 | total=max_retries, |
|
322 | total=max_retries, | |
291 | backoff_factor=0.15, |
|
323 | backoff_factor=0.15, | |
292 | status_forcelist=[500, 502, 503, 504]) |
|
324 | status_forcelist=[500, 502, 503, 504]) | |
293 | call_headers = { |
|
325 | call_headers = { | |
294 | 'User-Agent': 'RhodeCode-webhook-caller/{}'.format( |
|
326 | 'User-Agent': 'RhodeCode-webhook-caller/{}'.format( | |
295 | rhodecode.__version__) |
|
327 | rhodecode.__version__) | |
296 | } # updated below with custom ones, allows override |
|
328 | } # updated below with custom ones, allows override | |
297 |
|
329 | |||
298 | for url, token, headers, data in url_calls: |
|
330 | for url, token, headers, data in url_calls: | |
299 | req_session = requests.Session() |
|
331 | req_session = requests.Session() | |
300 | req_session.mount( # retry max N times |
|
332 | req_session.mount( # retry max N times | |
301 | 'http://', requests.adapters.HTTPAdapter(max_retries=retries)) |
|
333 | 'http://', requests.adapters.HTTPAdapter(max_retries=retries)) | |
302 |
|
334 | |||
303 | method = settings.get('method_type') or 'post' |
|
335 | method = settings.get('method_type') or 'post' | |
304 | call_method = getattr(req_session, method) |
|
336 | call_method = getattr(req_session, method) | |
305 |
|
337 | |||
306 | headers = headers or {} |
|
338 | headers = headers or {} | |
307 | call_headers.update(headers) |
|
339 | call_headers.update(headers) | |
|
340 | auth = get_auth(settings) | |||
308 |
|
341 | |||
309 |
log.debug('calling Webhook with method: %s', |
|
342 | log.debug('calling Webhook with method: %s, and auth:%s', | |
|
343 | call_method, auth) | |||
310 | resp = call_method(url, json={ |
|
344 | resp = call_method(url, json={ | |
311 | 'token': token, |
|
345 | 'token': token, | |
312 | 'event': data |
|
346 | 'event': data | |
313 | }, headers=call_headers) |
|
347 | }, headers=call_headers, auth=auth) | |
314 | log.debug('Got Webhook response: %s', resp) |
|
348 | log.debug('Got Webhook response: %s', resp) | |
315 |
|
349 | |||
316 | resp.raise_for_status() # raise exception on a failed request |
|
350 | resp.raise_for_status() # raise exception on a failed request |
General Comments 0
You need to be logged in to leave comments.
Login now