##// END OF EJS Templates
scm_app: add more debug info when unhandled errors happen on vcsserver.
scm_app: add more debug info when unhandled errors happen on vcsserver.

File last commit:

r3061:a44afbe5 default
r3093:e2f600bd default
Show More
webhook.py
274 lines | 12.9 KiB | text/x-python | PythonLexer
dan
integrations: add webhook integration
r444 # -*- coding: utf-8 -*-
release: update copyright year to 2018
r2487 # Copyright (C) 2012-2018 RhodeCode GmbH
dan
integrations: add webhook integration
r444 #
# 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 <http://www.gnu.org/licenses/>.
#
# 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 __future__ import unicode_literals
dan
forms: add deform for integration settings forms
r518 import deform
webhook: add possibility to specify username and password during a call.
r2137 import deform.widget
dan
integrations: add webhook integration
r444 import logging
import requests
webhook: add possibility to specify username and password during a call.
r2137 import requests.adapters
dan
integrations: add webhook integration
r444 import colander
webhooks: added variables into the call URL. Fixes #4211
r938 from requests.packages.urllib3.util.retry import Retry
dan
integrations: add webhook integration
r444
integrations: webhook, allow to set a custom header. Fixes #5384
r2086 import rhodecode
dan
integrations: add webhook integration
r444 from rhodecode import events
dan
integrations: refactor/cleanup + features, fixes #4181...
r731 from rhodecode.translation import _
integrations: expose some common methods for web based calls of integrations.
r2580 from rhodecode.integrations.types.base import (
webhook: abstract webhook handler to base for easier re-usage.
r2585 IntegrationTypeBase, get_auth, get_web_token, get_url_vars,
WebhookDataHandler, WEBHOOK_URL_VARS)
webhook: execute via celery for async.
r2410 from rhodecode.lib.celerylib import run_task, async_task, RequestContextTask
webhook: use textarea and format url vars better for webhook integration....
r2584 from rhodecode.model.validation_schema import widgets
dan
integrations: add webhook integration
r444
Martin Bornhold
logging: Use __name__ when requesting a logger
r504 log = logging.getLogger(__name__)
dan
integrations: add webhook integration
r444
celery: celery 4.X support. Fixes #4169...
r2359
integrations: expose actor user_id, and username in webhook integration templates args....
r1709 # updating this required to update the `common_vars` passed in url calling func
webhooks: added variables into the call URL. Fixes #4211
r938
integrations: expose some common methods for web based calls of integrations.
r2580 URL_VARS = get_url_vars(WEBHOOK_URL_VARS)
webhooks: added variables into the call URL. Fixes #4211
r938
dan
integrations: refactor/cleanup + features, fixes #4181...
r731 class WebhookSettingsSchema(colander.Schema):
dan
integrations: add webhook integration
r444 url = colander.SchemaNode(
colander.String(),
dan
integrations: refactor/cleanup + features, fixes #4181...
r731 title=_('Webhook URL'),
webhooks: added variables into the call URL. Fixes #4211
r938 description=
webhook: use textarea and format url vars better for webhook integration....
r2584 _('URL to which Webhook should submit data. If used some of the '
'variables would trigger multiple calls, like ${branch} or '
'${commit_id}. Webhook will be called as many times as unique '
'objects in data in such cases.'),
dan
integrations: refactor/cleanup + features, fixes #4181...
r731 missing=colander.required,
required=True,
dan
integrations: add webhook integration
r444 validator=colander.url,
webhook: use textarea and format url vars better for webhook integration....
r2584 widget=widgets.CodeMirrorWidget(
help_block_collapsable_name='Show url variables',
help_block_collapsable=(
'E.g http://my-serv/trigger_job/${{event_name}}'
'?PR_ID=${{pull_request_id}}'
'\nFull list of vars:\n{}'.format(URL_VARS)),
codemirror_mode='text',
codemirror_options='{"lineNumbers": false, "lineWrapping": true}'),
dan
integrations: add webhook integration
r444 )
secret_token = colander.SchemaNode(
colander.String(),
dan
integrations: refactor/cleanup + features, fixes #4181...
r731 title=_('Secret Token'),
webhook: improve spelling and help text.
r2136 description=_('Optional string used to validate received payloads. '
'It will be sent together with event data in JSON'),
dan
integrations: add webhook integration
r444 default='',
dan
integrations: refactor/cleanup + features, fixes #4181...
r731 missing='',
dan
forms: add deform for integration settings forms
r518 widget=deform.widget.TextInputWidget(
integrations: webhook, allow to set a custom header. Fixes #5384
r2086 placeholder='e.g. secret_token'
),
)
webhook: add possibility to specify username and password during a call.
r2137 username = colander.SchemaNode(
colander.String(),
title=_('Username'),
description=_('Optional username to authenticate the call.'),
default='',
missing='',
widget=deform.widget.TextInputWidget(
placeholder='e.g. admin'
),
)
password = colander.SchemaNode(
colander.String(),
title=_('Password'),
description=_('Optional password to authenticate the call.'),
default='',
missing='',
widget=deform.widget.PasswordWidget(
placeholder='e.g. secret.',
redisplay=True,
),
)
integrations: webhook, allow to set a custom header. Fixes #5384
r2086 custom_header_key = colander.SchemaNode(
colander.String(),
title=_('Custom Header Key'),
description=_('Custom Header name to be set when calling endpoint.'),
default='',
missing='',
widget=deform.widget.TextInputWidget(
webhook: use textarea and format url vars better for webhook integration....
r2584 placeholder='e.g: Authorization'
integrations: webhook, allow to set a custom header. Fixes #5384
r2086 ),
)
custom_header_val = colander.SchemaNode(
colander.String(),
title=_('Custom Header Value'),
description=_('Custom Header value to be set when calling endpoint.'),
default='',
missing='',
widget=deform.widget.TextInputWidget(
webhook: use textarea and format url vars better for webhook integration....
r2584 placeholder='e.g. Basic XxXxXx'
dan
forms: add deform for integration settings forms
r518 ),
dan
integrations: add webhook integration
r444 )
webhook: add possibility to use POST or GET methods for calling webhooks.
r1382 method_type = colander.SchemaNode(
colander.String(),
title=_('Call Method'),
webhook: improve spelling and help text.
r2136 description=_('Select if the Webhook call should be made '
webhook: add possibility to use POST or GET methods for calling webhooks.
r1382 'with POST or GET.'),
default='post',
missing='',
widget=deform.widget.RadioChoiceWidget(
values=[('get', 'GET'), ('post', 'POST')],
inline=True
),
)
dan
integrations: add webhook integration
r444
class WebhookIntegrationType(IntegrationTypeBase):
key = 'webhook'
dan
integrations: refactor/cleanup + features, fixes #4181...
r731 display_name = _('Webhook')
webhook: abstract webhook handler to base for easier re-usage.
r2585 description = _('send JSON data to a url endpoint')
integrations: use classmethod for icon extraction for easier code readability.
r2576
@classmethod
def icon(cls):
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>'''
dan
integrations: refactor/cleanup + features, fixes #4181...
r731
dan
integrations: add webhook integration
r444 valid_events = [
events.PullRequestCloseEvent,
events.PullRequestMergeEvent,
events.PullRequestUpdateEvent,
events.PullRequestCommentEvent,
events.PullRequestReviewEvent,
events.PullRequestCreateEvent,
events.RepoPushEvent,
events.RepoCreateEvent,
]
dan
forms: add deform for integration settings forms
r518 def settings_schema(self):
dan
integrations: add webhook integration
r444 schema = WebhookSettingsSchema()
schema.add(colander.SchemaNode(
colander.Set(),
dan
forms: add deform for integration settings forms
r518 widget=deform.widget.CheckboxChoiceWidget(
values=sorted(
[(e.name, e.display_name) for e in self.valid_events]
)
),
dan
integrations: add webhook integration
r444 description="Events activated for this integration",
name='events'
))
return schema
def send_event(self, event):
webhook: abstract webhook handler to base for easier re-usage.
r2585 log.debug(
'handling event %s with Webhook integration %s', event.name, self)
dan
integrations: add webhook integration
r444
if event.__class__ not in self.valid_events:
logging: use lazy parameter evaluation in log calls.
r3061 log.debug('event not valid: %r', event)
dan
integrations: add webhook integration
r444 return
if event.name not in self.settings['events']:
logging: use lazy parameter evaluation in log calls.
r3061 log.debug('event ignored: %r', event)
dan
integrations: add webhook integration
r444 return
data = event.as_dict()
webhooks: added variables into the call URL. Fixes #4211
r938 template_url = self.settings['url']
integrations: webhook, allow to set a custom header. Fixes #5384
r2086 headers = {}
webhook: add possibility to specify username and password during a call.
r2137 head_key = self.settings.get('custom_header_key')
head_val = self.settings.get('custom_header_val')
integrations: webhook, allow to set a custom header. Fixes #5384
r2086 if head_key and head_val:
headers = {head_key: head_val}
webhook: abstract webhook handler to base for easier re-usage.
r2585 handler = WebhookDataHandler(template_url, headers)
integrations: webhook, allow to set a custom header. Fixes #5384
r2086
webhooks: added variables into the call URL. Fixes #4211
r938 url_calls = handler(event, data)
log.debug('webhook: calling following urls: %s',
[x[0] for x in url_calls])
webhook: execute via celery for async.
r2410
run_task(post_to_webhook, url_calls, self.settings)
dan
integrations: add webhook integration
r444
celery: celery 4.X support. Fixes #4169...
r2359 @async_task(ignore_result=True, base=RequestContextTask)
webhook: add possibility to use POST or GET methods for calling webhooks.
r1382 def post_to_webhook(url_calls, settings):
webhook: execute via celery for async.
r2410 """
Example data::
{'actor': {'user_id': 2, 'username': u'admin'},
'actor_ip': u'192.168.157.1',
'name': 'repo-push',
'push': {'branches': [{'name': u'default',
'url': 'http://rc.local:8080/hg-repo/changelog?branch=default'}],
'commits': [{'author': u'Marcin Kuzminski <marcin@rhodecode.com>',
'branch': u'default',
'date': datetime.datetime(2017, 11, 30, 12, 59, 48),
'issues': [],
'mentions': [],
'message': u'commit Thu 30 Nov 2017 13:59:48 CET',
'message_html': u'commit Thu 30 Nov 2017 13:59:48 CET',
'message_html_title': u'commit Thu 30 Nov 2017 13:59:48 CET',
'parents': [{'raw_id': '431b772a5353dad9974b810dd3707d79e3a7f6e0'}],
'permalink_url': u'http://rc.local:8080/_7/changeset/a815cc738b9651eb5ffbcfb1ce6ccd7c701a5ddf',
'raw_id': 'a815cc738b9651eb5ffbcfb1ce6ccd7c701a5ddf',
'refs': {'bookmarks': [], 'branches': [u'default'], 'tags': [u'tip']},
'reviewers': [],
'revision': 9L,
'short_id': 'a815cc738b96',
'url': u'http://rc.local:8080/hg-repo/changeset/a815cc738b9651eb5ffbcfb1ce6ccd7c701a5ddf'}],
'issues': {}},
'repo': {'extra_fields': '',
'permalink_url': u'http://rc.local:8080/_7',
'repo_id': 7,
'repo_name': u'hg-repo',
'repo_type': u'hg',
'url': u'http://rc.local:8080/hg-repo'},
'server_url': u'http://rc.local:8080',
'utc_timestamp': datetime.datetime(2017, 11, 30, 13, 0, 1, 569276)
"""
webhooks: added variables into the call URL. Fixes #4211
r938 max_retries = 3
http: ported old depracated retry format for http client.
r1958 retries = Retry(
total=max_retries,
backoff_factor=0.15,
status_forcelist=[500, 502, 503, 504])
integrations: webhook, allow to set a custom header. Fixes #5384
r2086 call_headers = {
'User-Agent': 'RhodeCode-webhook-caller/{}'.format(
rhodecode.__version__)
} # updated below with custom ones, allows override
events: expose shadow repo build url.
r2582 auth = get_auth(settings)
webhook: abstract webhook handler to base for easier re-usage.
r2585 token = get_web_token(settings)
for url, headers, data in url_calls:
webhooks: added variables into the call URL. Fixes #4211
r938 req_session = requests.Session()
http: ported old depracated retry format for http client.
r1958 req_session.mount( # retry max N times
webhooks: added variables into the call URL. Fixes #4211
r938 'http://', requests.adapters.HTTPAdapter(max_retries=retries))
webhook: add possibility to use POST or GET methods for calling webhooks.
r1382 method = settings.get('method_type') or 'post'
call_method = getattr(req_session, method)
integrations: webhook, allow to set a custom header. Fixes #5384
r2086 headers = headers or {}
call_headers.update(headers)
webhook: add possibility to specify username and password during a call.
r2137 log.debug('calling Webhook with method: %s, and auth:%s',
call_method, auth)
integrations: parse pushed tags, and lightweight tags for git....
r2422 if settings.get('log_data'):
log.debug('calling webhook with data: %s', data)
webhook: add possibility to use POST or GET methods for calling webhooks.
r1382 resp = call_method(url, json={
webhooks: added variables into the call URL. Fixes #4211
r938 'token': token,
'event': data
requests: added a default timeout for operation calling the endpoint url....
r2950 }, headers=call_headers, auth=auth, timeout=60)
webhook: improve spelling and help text.
r2136 log.debug('Got Webhook response: %s', resp)
integrations: webhook, allow to set a custom header. Fixes #5384
r2086
integrations: webhook handle response errors and show response for easier debugging.
r2581 try:
resp.raise_for_status() # raise exception on a failed request
except Exception:
log.error(resp.text)
raise