Skip to main content

Flask blueprints for handling GitHub and GitLab webhooks

Project description

githubWebhook

A Python library providing Flask blueprints for receiving GitHub or GitLab webhooks and acting upon them. The library provides webhooks allowing for automatic deployment, testing and integrations. However due to the open ended nature of the blueprint this behavior can be easily customized thanks to the very open ended class dependency tree.

Setup

  1. 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.

  2. Install this package

    • Using pip

      pip install gitAppWebhook
      
    • By cloning the repository

      git clone https://github.com/TCA166/gitWebhook.git
      pip install -r gitWebhook/requirements.txt
      
    • By including this repo as a submodule

      git submodule add https://github.com/example/repo.git gitWebhook
      pip install -r gitWebhook/requirements.txt
      
  3. Create an instance of webhookBlueprint (or either of it's subclasses) with your settings

    import gitWebhook
    
    wb = gitWebhook.webhookBlueprint(token, url_prefix="/")
    
  4. Register the instance within a Flask app of your choice

    from flask import Flask
    
    app = Flask(__name__)
    app.register_blueprint(wb)
    

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.

Blueprint classes

This library provides a basic blueprint derived class for only receiving webhooks and a few derived classes with different webhook processing capabilities.

webhookBlueprint

This very basic class itself has no webhook processing capabilities, but functions as a base from which webhook receiving blueprints may be derived from. It fully implements all the verification required for GitHub and GitLab blueprints and as such should be the class you should derive from.

webhookBlueprint behavior

  1. Verifies the request's validity (Optional but very recommended)
  2. Returns 200

pullerWebhookBlueprint

This class derived from webhookBlueprint is aimed to be used as a means of automating deployment and testing on servers. If you don't want to use GitHub actions, you can always use a Flask app with this blueprint registered.

pullerWebhookBlueprint behavior

Upon receiving a POST request to the / endpoint the blueprint:

  1. Verifies the request's validity (Optional but very recommended)
  2. Performs a git pull
  3. Runs a test suite (Optional)
    1. If the tests failed it tries to revert the pull
  4. Returns to GitHub or GitLab the results of the pull and tests if they have been performed

functionWebhookBlueprint

This class derived from webhookBlueprint is aimed to be used as a means of integrating different services. You provide it on initialization with a list of Callable taking in webhook payloads as a single argument, and these functions will be called upon receiving a webhook. Thus you can easily integrate services with this class by simply having integration happen in such a Callable.

functionWebhookBlueprint behavior

Upon receiving a POST request to the / endpoint the blueprint:

  1. Verifies the request's validity (Optional but very recommended)
  2. Calls all functions contained within it's functions list
  3. If any returned False it returns a failure to origin.

Customization

You can easily tweak any of the classes to your liking in two ways.

  1. 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 (pullerWebhookBlueprint)
    • enable logging by providing a logging.Logger instance
    • change blueprint name to avoid conflicts during blueprint registration
    • change the command used to invoke git (pullerWebhookBlueprint)
    • change the OS environment used by child git processes (pullerWebhookBlueprint)
  2. More advanced changes require creating a subclass from webhookBlueprint This isn't that daunting. There are two methods in the class: receiveWebhook and processWebhook. 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

CCimg
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 Distribution

gitappwebhook-0.2.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

gitAppWebhook-0.2-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file gitappwebhook-0.2.tar.gz.

File metadata

  • Download URL: gitappwebhook-0.2.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for gitappwebhook-0.2.tar.gz
Algorithm Hash digest
SHA256 baa791218fe3fb34cdcd9f14927b7316501a8106c9bb86693ab5f9e06370f321
MD5 8165abaca7753498f29bd65b0b31da3c
BLAKE2b-256 16d0d952ec5d3739496509bd261698f79f7f24733a89610c28860f1aa6527452

See more details on using hashes here.

Provenance

File details

Details for the file gitAppWebhook-0.2-py3-none-any.whl.

File metadata

  • Download URL: gitAppWebhook-0.2-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.9.19

File hashes

Hashes for gitAppWebhook-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b95553835b6fdc330b2bcd55e56b8b106144f1e7d7b0001fe64ab8a46276f311
MD5 b2443e308d28c5cdf8ff71247ba01e48
BLAKE2b-256 4f44695172928f5d6e7a0a617a0f1217e95a5b5fc2553990e5d3c1066992c261

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page