Flask blueprints for handling GitHub and GitLab webhooks
Project description
githubWebhook
A Flask blueprint for receiving GitHub or GitLab webhooks and acting upon them.
By default the blueprint performs a git pull and optionally unittest.testSuite
tests upon receiving a valid webhook request.
The idea behind this is to allow for automatic testing or deployment without using GitHub Actions.
However due to the open ended nature of the blueprint this behavior can be easily customized.
Setup
- Setup the webhook on the git side During the setup be sure to pay close attention to any opportunities to input any sort of secret key. You will need that key later if you want to enable webhook verification THIS IS SOMETHING THAT I GREATLY ADVISE YOU DO. For GitHub that would be the secret string that you provide during creation and for GitLab that would be the secret token.
- Clone this repo or add it as a submodule
- Install packages from requirements.txt
- Create an instance of
webhookBlueprint
with your settings - Register the instance within a Flask app of your choice
If you are lost you can always look at official GitHub resources, or look at wsgi.py where an example configured Flask webapp is located.
webhookBlueprint class
The webhook receiving behavior was packaged together as a single class derived from the Flask blueprint class.
Default behavior
Upon receiving a POST
request to the / endpoint the blueprint:
- Verifies the request's validity (Optional but very recommended)
- Performs a
git pull
- Runs a test suite (Optional)
- If the tests failed it tries to revert the pull
- Returns to GitHub or GitLab the results of the pull and tests if they have been performed
Customization
You can easily tweak the class to your liking in two ways.
- Some settings can be tweaked during blueprint instance creation.
You can:
- enable or disable webhook verification by providing (or not providing) a
webhookToken
- enable unit test running by providing a
unittest.testSuite
instance - enable logging by providing a
logging.Logger
instance - change blueprint name to avoid conflicts during blueprint registration
- change the command used to invoke git
- change the OS environment used by child git processes
- enable or disable webhook verification by providing (or not providing) a
- More advanced changes require creating a subclass from webhookBlueprint
This isn't that daunting.
There are two methods in the class:
receiveWebhook
andprocessWebhook
. Override the former to change how the raw request is handled and verified. Override the latter to change what is done once the webhook is verified.
License
This work is licensed under a Creative Commons Attribution 4.0 International License.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distribution
Hashes for gitAppWebhook-0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 98efc9f7761176ce10445a0f5f60fda0dff72f394401039d7b563b16f0c2de5e |
|
MD5 | d0c3c774e1c842ec0fe2279db95881be |
|
BLAKE2b-256 | cbb927fa0c389ee8d0b9e7e784dc9e243bcb4520db437d38769bfa1b9081c25a |