Create and trigger a webhook tutorial
Was this helpful?
The next thing we want to do is push something to the repository to see if we can trigger the webhook. Start by creating a new file with the following text:
Let's try out a webhook.
Save the file, as file.txt
, to your repository directory on your local system.
Add, commit, and push the file to your Bitbucket repository:
$ git add file.txt\n$ git commit -m 'Initial commit'\n$ git push
Return to the ngrok URL and you will see the POST request with the event payload that Bitbucket sends to your server.
You have now successfully created and triggered a webhook!
From the cloned repository on your local system, open the listener.py
file.
Read through this file. Notice that it explains what prints to the command line after you run honcho start
and what prints to the webhook URL.
This file also includes code that uses data from the POST payload to output a notification.
@app.route('/webhook', methods=['GET', 'POST'])\ndef tracking():\n if request.method == 'POST':\n data = request.get_json()\n commit_author = data['actor']['username']\n commit_hash = data['push']['changes'][0]['new']['target']['hash'][:7]\n commit_url = data['push']['changes'][0]['new']['target']['links']['html']['href']\n if _platform == "darwin":\n from pync import Notifier\n Notifier.notify('%s committed %s\\nClick to view in Bitbucket' % (commit_author, commit_hash), title='Webhook received!', open=commit_url)\n else:\n print 'Webhook received! %s committed %s' % (commit_author, commit_hash)\n return 'OK'\n else:\n return displayHTML(request)
Modify the tracking()
function to update how the Webhook Listener uses the POST data. You can update the payload parameters it uses or what the function does with the data.
Save the listener.py
file.
Run honcho start
again and push another file to your repository with the webhook.
You should see the changes you made to the listener.py
file.
Was this helpful?