##// END OF EJS Templates
webhook: updated docs/examples.
marcink -
r3375:7adf2b03 default
parent child Browse files
Show More
@@ -1,264 +1,263 b''
1 # -*- coding: utf-8 -*-
1 # -*- coding: utf-8 -*-
2
2
3 # Copyright (C) 2012-2019 RhodeCode GmbH
3 # Copyright (C) 2012-2019 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
22
23 import deform
23 import deform
24 import deform.widget
24 import deform.widget
25 import logging
25 import logging
26 import colander
26 import colander
27
27
28 import rhodecode
28 import rhodecode
29 from rhodecode import events
29 from rhodecode import events
30 from rhodecode.translation import _
30 from rhodecode.translation import _
31 from rhodecode.integrations.types.base import (
31 from rhodecode.integrations.types.base import (
32 IntegrationTypeBase, get_auth, get_web_token, get_url_vars,
32 IntegrationTypeBase, get_auth, get_web_token, get_url_vars,
33 WebhookDataHandler, WEBHOOK_URL_VARS, requests_retry_call)
33 WebhookDataHandler, WEBHOOK_URL_VARS, requests_retry_call)
34 from rhodecode.lib.celerylib import run_task, async_task, RequestContextTask
34 from rhodecode.lib.celerylib import run_task, async_task, RequestContextTask
35 from rhodecode.model.validation_schema import widgets
35 from rhodecode.model.validation_schema import widgets
36
36
37 log = logging.getLogger(__name__)
37 log = logging.getLogger(__name__)
38
38
39
39
40 # updating this required to update the `common_vars` passed in url calling func
40 # updating this required to update the `common_vars` passed in url calling func
41
41
42 URL_VARS = get_url_vars(WEBHOOK_URL_VARS)
42 URL_VARS = get_url_vars(WEBHOOK_URL_VARS)
43
43
44
44
45 class WebhookSettingsSchema(colander.Schema):
45 class WebhookSettingsSchema(colander.Schema):
46 url = colander.SchemaNode(
46 url = colander.SchemaNode(
47 colander.String(),
47 colander.String(),
48 title=_('Webhook URL'),
48 title=_('Webhook URL'),
49 description=
49 description=
50 _('URL to which Webhook should submit data. If used some of the '
50 _('URL to which Webhook should submit data. If used some of the '
51 'variables would trigger multiple calls, like ${branch} or '
51 'variables would trigger multiple calls, like ${branch} or '
52 '${commit_id}. Webhook will be called as many times as unique '
52 '${commit_id}. Webhook will be called as many times as unique '
53 'objects in data in such cases.'),
53 'objects in data in such cases.'),
54 missing=colander.required,
54 missing=colander.required,
55 required=True,
55 required=True,
56 validator=colander.url,
56 validator=colander.url,
57 widget=widgets.CodeMirrorWidget(
57 widget=widgets.CodeMirrorWidget(
58 help_block_collapsable_name='Show url variables',
58 help_block_collapsable_name='Show url variables',
59 help_block_collapsable=(
59 help_block_collapsable=(
60 'E.g http://my-serv/trigger_job/${{event_name}}'
60 'E.g http://my-serv/trigger_job/${{event_name}}'
61 '?PR_ID=${{pull_request_id}}'
61 '?PR_ID=${{pull_request_id}}'
62 '\nFull list of vars:\n{}'.format(URL_VARS)),
62 '\nFull list of vars:\n{}'.format(URL_VARS)),
63 codemirror_mode='text',
63 codemirror_mode='text',
64 codemirror_options='{"lineNumbers": false, "lineWrapping": true}'),
64 codemirror_options='{"lineNumbers": false, "lineWrapping": true}'),
65 )
65 )
66 secret_token = colander.SchemaNode(
66 secret_token = colander.SchemaNode(
67 colander.String(),
67 colander.String(),
68 title=_('Secret Token'),
68 title=_('Secret Token'),
69 description=_('Optional string used to validate received payloads. '
69 description=_('Optional string used to validate received payloads. '
70 'It will be sent together with event data in JSON'),
70 'It will be sent together with event data in JSON'),
71 default='',
71 default='',
72 missing='',
72 missing='',
73 widget=deform.widget.TextInputWidget(
73 widget=deform.widget.TextInputWidget(
74 placeholder='e.g. secret_token'
74 placeholder='e.g. secret_token'
75 ),
75 ),
76 )
76 )
77 username = colander.SchemaNode(
77 username = colander.SchemaNode(
78 colander.String(),
78 colander.String(),
79 title=_('Username'),
79 title=_('Username'),
80 description=_('Optional username to authenticate the call.'),
80 description=_('Optional username to authenticate the call.'),
81 default='',
81 default='',
82 missing='',
82 missing='',
83 widget=deform.widget.TextInputWidget(
83 widget=deform.widget.TextInputWidget(
84 placeholder='e.g. admin'
84 placeholder='e.g. admin'
85 ),
85 ),
86 )
86 )
87 password = colander.SchemaNode(
87 password = colander.SchemaNode(
88 colander.String(),
88 colander.String(),
89 title=_('Password'),
89 title=_('Password'),
90 description=_('Optional password to authenticate the call.'),
90 description=_('Optional password to authenticate the call.'),
91 default='',
91 default='',
92 missing='',
92 missing='',
93 widget=deform.widget.PasswordWidget(
93 widget=deform.widget.PasswordWidget(
94 placeholder='e.g. secret.',
94 placeholder='e.g. secret.',
95 redisplay=True,
95 redisplay=True,
96 ),
96 ),
97 )
97 )
98 custom_header_key = colander.SchemaNode(
98 custom_header_key = colander.SchemaNode(
99 colander.String(),
99 colander.String(),
100 title=_('Custom Header Key'),
100 title=_('Custom Header Key'),
101 description=_('Custom Header name to be set when calling endpoint.'),
101 description=_('Custom Header name to be set when calling endpoint.'),
102 default='',
102 default='',
103 missing='',
103 missing='',
104 widget=deform.widget.TextInputWidget(
104 widget=deform.widget.TextInputWidget(
105 placeholder='e.g: Authorization'
105 placeholder='e.g: Authorization'
106 ),
106 ),
107 )
107 )
108 custom_header_val = colander.SchemaNode(
108 custom_header_val = colander.SchemaNode(
109 colander.String(),
109 colander.String(),
110 title=_('Custom Header Value'),
110 title=_('Custom Header Value'),
111 description=_('Custom Header value to be set when calling endpoint.'),
111 description=_('Custom Header value to be set when calling endpoint.'),
112 default='',
112 default='',
113 missing='',
113 missing='',
114 widget=deform.widget.TextInputWidget(
114 widget=deform.widget.TextInputWidget(
115 placeholder='e.g. Basic XxXxXx'
115 placeholder='e.g. Basic XxXxXx'
116 ),
116 ),
117 )
117 )
118 method_type = colander.SchemaNode(
118 method_type = colander.SchemaNode(
119 colander.String(),
119 colander.String(),
120 title=_('Call Method'),
120 title=_('Call Method'),
121 description=_('Select if the Webhook call should be made '
121 description=_('Select a HTTP method to use when calling the Webhook.'),
122 'with POST, GET or PUT.'),
123 default='post',
122 default='post',
124 missing='',
123 missing='',
125 widget=deform.widget.RadioChoiceWidget(
124 widget=deform.widget.RadioChoiceWidget(
126 values=[('get', 'GET'), ('post', 'POST'), ('put', 'PUT')],
125 values=[('get', 'GET'), ('post', 'POST'), ('put', 'PUT')],
127 inline=True
126 inline=True
128 ),
127 ),
129 )
128 )
130
129
131
130
132 class WebhookIntegrationType(IntegrationTypeBase):
131 class WebhookIntegrationType(IntegrationTypeBase):
133 key = 'webhook'
132 key = 'webhook'
134 display_name = _('Webhook')
133 display_name = _('Webhook')
135 description = _('send JSON data to a url endpoint')
134 description = _('send JSON data to a url endpoint')
136
135
137 @classmethod
136 @classmethod
138 def icon(cls):
137 def icon(cls):
139 return '''<?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>'''
138 return '''<?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>'''
140
139
141 valid_events = [
140 valid_events = [
142 events.PullRequestCloseEvent,
141 events.PullRequestCloseEvent,
143 events.PullRequestMergeEvent,
142 events.PullRequestMergeEvent,
144 events.PullRequestUpdateEvent,
143 events.PullRequestUpdateEvent,
145 events.PullRequestCommentEvent,
144 events.PullRequestCommentEvent,
146 events.PullRequestReviewEvent,
145 events.PullRequestReviewEvent,
147 events.PullRequestCreateEvent,
146 events.PullRequestCreateEvent,
148 events.RepoPushEvent,
147 events.RepoPushEvent,
149 events.RepoCreateEvent,
148 events.RepoCreateEvent,
150 ]
149 ]
151
150
152 def settings_schema(self):
151 def settings_schema(self):
153 schema = WebhookSettingsSchema()
152 schema = WebhookSettingsSchema()
154 schema.add(colander.SchemaNode(
153 schema.add(colander.SchemaNode(
155 colander.Set(),
154 colander.Set(),
156 widget=deform.widget.CheckboxChoiceWidget(
155 widget=deform.widget.CheckboxChoiceWidget(
157 values=sorted(
156 values=sorted(
158 [(e.name, e.display_name) for e in self.valid_events]
157 [(e.name, e.display_name) for e in self.valid_events]
159 )
158 )
160 ),
159 ),
161 description="Events activated for this integration",
160 description="Events activated for this integration",
162 name='events'
161 name='events'
163 ))
162 ))
164 return schema
163 return schema
165
164
166 def send_event(self, event):
165 def send_event(self, event):
167 log.debug(
166 log.debug(
168 'handling event %s with Webhook integration %s', event.name, self)
167 'handling event %s with Webhook integration %s', event.name, self)
169
168
170 if event.__class__ not in self.valid_events:
169 if event.__class__ not in self.valid_events:
171 log.debug('event not valid: %r', event)
170 log.debug('event not valid: %r', event)
172 return
171 return
173
172
174 if event.name not in self.settings['events']:
173 if event.name not in self.settings['events']:
175 log.debug('event ignored: %r', event)
174 log.debug('event ignored: %r', event)
176 return
175 return
177
176
178 data = event.as_dict()
177 data = event.as_dict()
179 template_url = self.settings['url']
178 template_url = self.settings['url']
180
179
181 headers = {}
180 headers = {}
182 head_key = self.settings.get('custom_header_key')
181 head_key = self.settings.get('custom_header_key')
183 head_val = self.settings.get('custom_header_val')
182 head_val = self.settings.get('custom_header_val')
184 if head_key and head_val:
183 if head_key and head_val:
185 headers = {head_key: head_val}
184 headers = {head_key: head_val}
186
185
187 handler = WebhookDataHandler(template_url, headers)
186 handler = WebhookDataHandler(template_url, headers)
188
187
189 url_calls = handler(event, data)
188 url_calls = handler(event, data)
190 log.debug('webhook: calling following urls: %s',
189 log.debug('webhook: calling following urls: %s', [x[0] for x in url_calls])
191 [x[0] for x in url_calls])
192
190
193 run_task(post_to_webhook, url_calls, self.settings)
191 run_task(post_to_webhook, url_calls, self.settings)
194
192
195
193
196 @async_task(ignore_result=True, base=RequestContextTask)
194 @async_task(ignore_result=True, base=RequestContextTask)
197 def post_to_webhook(url_calls, settings):
195 def post_to_webhook(url_calls, settings):
198 """
196 """
199 Example data::
197 Example data::
200
198
201 {'actor': {'user_id': 2, 'username': u'admin'},
199 {'actor': {'user_id': 2, 'username': u'admin'},
202 'actor_ip': u'192.168.157.1',
200 'actor_ip': u'192.168.157.1',
203 'name': 'repo-push',
201 'name': 'repo-push',
204 'push': {'branches': [{'name': u'default',
202 'push': {'branches': [{'name': u'default',
205 'url': 'http://rc.local:8080/hg-repo/changelog?branch=default'}],
203 'url': 'http://rc.local:8080/hg-repo/changelog?branch=default'}],
206 'commits': [{'author': u'Marcin Kuzminski <marcin@rhodecode.com>',
204 'commits': [{'author': u'Marcin Kuzminski <marcin@rhodecode.com>',
207 'branch': u'default',
205 'branch': u'default',
208 'date': datetime.datetime(2017, 11, 30, 12, 59, 48),
206 'date': datetime.datetime(2017, 11, 30, 12, 59, 48),
209 'issues': [],
207 'issues': [],
210 'mentions': [],
208 'mentions': [],
211 'message': u'commit Thu 30 Nov 2017 13:59:48 CET',
209 'message': u'commit Thu 30 Nov 2017 13:59:48 CET',
212 'message_html': u'commit Thu 30 Nov 2017 13:59:48 CET',
210 'message_html': u'commit Thu 30 Nov 2017 13:59:48 CET',
213 'message_html_title': u'commit Thu 30 Nov 2017 13:59:48 CET',
211 'message_html_title': u'commit Thu 30 Nov 2017 13:59:48 CET',
214 'parents': [{'raw_id': '431b772a5353dad9974b810dd3707d79e3a7f6e0'}],
212 'parents': [{'raw_id': '431b772a5353dad9974b810dd3707d79e3a7f6e0'}],
215 'permalink_url': u'http://rc.local:8080/_7/changeset/a815cc738b9651eb5ffbcfb1ce6ccd7c701a5ddf',
213 'permalink_url': u'http://rc.local:8080/_7/changeset/a815cc738b9651eb5ffbcfb1ce6ccd7c701a5ddf',
216 'raw_id': 'a815cc738b9651eb5ffbcfb1ce6ccd7c701a5ddf',
214 'raw_id': 'a815cc738b9651eb5ffbcfb1ce6ccd7c701a5ddf',
217 'refs': {'bookmarks': [], 'branches': [u'default'], 'tags': [u'tip']},
215 'refs': {'bookmarks': [],
218 'reviewers': [],
216 'branches': [u'default'],
219 'revision': 9L,
217 'tags': [u'tip']},
220 'short_id': 'a815cc738b96',
218 'reviewers': [],
221 'url': u'http://rc.local:8080/hg-repo/changeset/a815cc738b9651eb5ffbcfb1ce6ccd7c701a5ddf'}],
219 'revision': 9L,
222 'issues': {}},
220 'short_id': 'a815cc738b96',
221 'url': u'http://rc.local:8080/hg-repo/changeset/a815cc738b9651eb5ffbcfb1ce6ccd7c701a5ddf'}],
222 'issues': {}},
223 'repo': {'extra_fields': '',
223 'repo': {'extra_fields': '',
224 'permalink_url': u'http://rc.local:8080/_7',
224 'permalink_url': u'http://rc.local:8080/_7',
225 'repo_id': 7,
225 'repo_id': 7,
226 'repo_name': u'hg-repo',
226 'repo_name': u'hg-repo',
227 'repo_type': u'hg',
227 'repo_type': u'hg',
228 'url': u'http://rc.local:8080/hg-repo'},
228 'url': u'http://rc.local:8080/hg-repo'},
229 'server_url': u'http://rc.local:8080',
229 'server_url': u'http://rc.local:8080',
230 'utc_timestamp': datetime.datetime(2017, 11, 30, 13, 0, 1, 569276)
230 'utc_timestamp': datetime.datetime(2017, 11, 30, 13, 0, 1, 569276)
231 }
232 """
231
233
232 """
233 call_headers = {
234 call_headers = {
234 'User-Agent': 'RhodeCode-webhook-caller/{}'.format(
235 'User-Agent': 'RhodeCode-webhook-caller/{}'.format(rhodecode.__version__)
235 rhodecode.__version__)
236 } # updated below with custom ones, allows override
236 } # updated below with custom ones, allows override
237
237
238 auth = get_auth(settings)
238 auth = get_auth(settings)
239 token = get_web_token(settings)
239 token = get_web_token(settings)
240
240
241 for url, headers, data in url_calls:
241 for url, headers, data in url_calls:
242 req_session = requests_retry_call()
242 req_session = requests_retry_call()
243
243
244 method = settings.get('method_type') or 'post'
244 method = settings.get('method_type') or 'post'
245 call_method = getattr(req_session, method)
245 call_method = getattr(req_session, method)
246
246
247 headers = headers or {}
247 headers = headers or {}
248 call_headers.update(headers)
248 call_headers.update(headers)
249
249
250 log.debug('calling Webhook with method: %s, and auth:%s',
250 log.debug('calling Webhook with method: %s, and auth:%s', call_method, auth)
251 call_method, auth)
252 if settings.get('log_data'):
251 if settings.get('log_data'):
253 log.debug('calling webhook with data: %s', data)
252 log.debug('calling webhook with data: %s', data)
254 resp = call_method(url, json={
253 resp = call_method(url, json={
255 'token': token,
254 'token': token,
256 'event': data
255 'event': data
257 }, headers=call_headers, auth=auth, timeout=60)
256 }, headers=call_headers, auth=auth, timeout=60)
258 log.debug('Got Webhook response: %s', resp)
257 log.debug('Got Webhook response: %s', resp)
259
258
260 try:
259 try:
261 resp.raise_for_status() # raise exception on a failed request
260 resp.raise_for_status() # raise exception on a failed request
262 except Exception:
261 except Exception:
263 log.error(resp.text)
262 log.error(resp.text)
264 raise
263 raise
General Comments 0
You need to be logged in to leave comments. Login now