Blame doc/using_webhooks.rst
|
Pierre-Yves Chibon |
b5fb9a |
Using web-hooks
|
|
Pierre-Yves Chibon |
b5fb9a |
===============
|
|
Pierre-Yves Chibon |
b5fb9a |
|
|
Pierre-Yves Chibon |
b5fb9a |
Web-hooks are a notification system that could be compared to a callback.
|
|
Pierre-Yves Chibon |
b5fb9a |
Basically, pagure will make a HTTP POST request to one or more third party
|
|
Pierre-Yves Chibon |
b5fb9a |
server/application with information about what is or just happened.
|
|
Pierre-Yves Chibon |
f694ba |
|
|
Pierre-Yves Chibon |
f694ba |
To set-up a web-hook, simply go to the settings page of your project and
|
|
Pierre-Yves Chibon |
f694ba |
enter the URL to the server that will receive the notifications.
|
|
Pierre-Yves Chibon |
f694ba |
|
|
Pierre-Yves Chibon |
f694ba |
In the settings page is also present a web-hook key which is used by the
|
|
Pierre-Yves Chibon |
f694ba |
server to sign the message sent and which you can use to ensure the
|
|
Pierre-Yves Chibon |
f694ba |
notifications received are coming from the right source.
|
|
Pierre-Yves Chibon |
414e87 |
|
|
Pierre-Yves Chibon |
414e87 |
Each POST request made contains two specific headers:
|
|
Pierre-Yves Chibon |
414e87 |
|
|
Pierre-Yves Chibon |
414e87 |
::
|
|
Pierre-Yves Chibon |
414e87 |
|
|
Pierre-Yves Chibon |
414e87 |
X-Pagure-Topic
|
|
Pierre-Yves Chibon |
414e87 |
X-Pagure-Signature
|
|
Pierre-Yves Chibon |
414e87 |
|
|
Pierre-Yves Chibon |
414e87 |
|
|
Pierre-Yves Chibon |
414e87 |
``X-Pagure-Topic`` is a global header giving a clue about the type of action
|
|
Pierre-Yves Chibon |
414e87 |
that just occured. For example ``issue.edit``.
|
|
Pierre-Yves Chibon |
414e87 |
|
|
Pierre-Yves Chibon |
414e87 |
|
|
Pierre-Yves Chibon |
414e87 |
``X-Pagure-Signature`` contains the signature of the message allowing to
|
|
Pierre-Yves Chibon |
414e87 |
check that the message comes from pagure.
|
|
Pierre-Yves Chibon |
72c25a |
|
|
Pierre-Yves Chibon |
72c25a |
|
|
Pierre-Yves Chibon |
72c25a |
Pagure relies on ``hmac`` to sign the content of its messages. If you want
|
|
Pierre-Yves Chibon |
ed2aeb |
to validate the message, in python, you can do something like the following:
|
|
Pierre-Yves Chibon |
72c25a |
|
|
Pierre-Yves Chibon |
72c25a |
::
|
|
Pierre-Yves Chibon |
72c25a |
|
|
Pierre-Yves Chibon |
72c25a |
import hmac
|
|
Pierre-Yves Chibon |
010886 |
import hashlib
|
|
Pierre-Yves Chibon |
72c25a |
|
|
Pierre-Yves Chibon |
72c25a |
payload = # content you received in the POST request
|
|
Pierre-Yves Chibon |
72c25a |
headers = # headers of the POST request
|
|
Pierre-Yves Chibon |
72c25a |
project_web_hook_key = # private web-hook key of the project
|
|
Pierre-Yves Chibon |
72c25a |
|
|
Pierre-Yves Chibon |
72c25a |
hashhex = hmac.new(
|
|
Pierre-Yves Chibon |
72c25a |
str(project_web_hook_key), payload, hashlib.sha1).hexdigest()
|
|
Pierre-Yves Chibon |
72c25a |
|
|
Pierre-Yves Chibon |
72c25a |
if hashhex != headers.get('X-Pagure-Signature'):
|
|
Pierre-Yves Chibon |
72c25a |
raise Exception('Message received with an invalid signature')
|