Skip to main content

GitHub Webhooks Framework

Project description

Python GitHub Webhooks Framework

NOTE: Forked from github_webhooks as I needed to access more things:

  • query params from incoming webhooks for identification
  • background_tasks to handle long running webhook actions.

Simple and lightweight micro framework for quick integration with GitHub webhooks.
It's based on FastAPI and pydantic, nothing more!
Async and mypy friendly.

Run CI
PyPI

Installation

Just add github-webhooks-framework2 package.
Example:

  • pip install github-webhooks-framework2
  • poetry add github-webhooks-framework2

Example

Create file example.py and copy next code:

import uvicorn
from pydantic import BaseModel

from github_webhooks import create_app
from github_webhooks.schemas import WebhookCommonPayload


# WebhookCommonPayload is based on pydantic.BaseModel
class PullRequestPayload(WebhookCommonPayload):
    class Pull(BaseModel):
        title: str
        url: str

    action: str
    pull_request: Pull


# Initialize Webhook App
app = create_app()


# Register webhook handler:
#   `pull_request` - name of an event to handle
#   `PullRequestPayload` - webhook payload will be parsed into this model
@app.hooks.register('pull_request', PullRequestPayload)
async def handler(payload: PullRequestPayload) -> None:
    print(f'New pull request {payload.pull_request.title}')
    print(f'  link: {payload.pull_request.url}')
    print(f'  author: {payload.sender.login}')


if __name__ == '__main__':
    # start uvicorn server
    uvicorn.run(app)

Let's have detailed overview.

We start by defining payload Model to parse incoming Pull Request Body.

class PullRequestPayload(WebhookCommonPayload):
    class Pull(BaseModel):
        title: str
        url: str

    action: str
    pull_request: Pull

In this example we only want to get action, pull_request.title and pull_request.url from payload.
By subclassing WebhookCommonPayload model will automatically get sender, repository and organization fields.

Next - we are creating ASGI app (based on FastAPI app)

app = create_app()

Optionally we can provide here secret_token Github Webhook secret

app = create_app(secret_token='super-secret-token')

And time to define our handler

@app.hooks.register('pull_request', PullRequestPayload)
async def handler(payload: PullRequestPayload) -> None:
    print(f'New pull request {payload.pull_request.title}')
    print(f'  link: {payload.pull_request.url}')
    print(f'  author: {payload.sender.login}')

We are using here @app.hooks.register deco, which accepts 2 arguments:

  • event: str - name of webhook event
  • payload_cls: pydantic.BaseModel - pydantic model class to parse request, subclassed from pydantic.BaseModel or WebhookCommonPayload.

And our handler function may include the following (named) params:

async def handler(payload: PullRequestPayload, headers: WebhookHeaders, query_params: QueryParams, background_tasks: BackgroundTasks) -> Optional[str]:
    # `headers` will be WebhookHeaders model with Github Webhook headers parsed.
    ...

An example setup can be found in example/server.py.

References:

Development

  1. Install local venv: python -m venv .venv
  2. Activate: . .venv/bin/activate
  3. Install deps with pip install -r requirements.txt -r requirements-test.txt
  4. When done with packages: ./freeze-requirements.sh

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

github_webhooks_framework2-0.2.2.tar.gz (20.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

github_webhooks_framework2-0.2.2-py3-none-any.whl (20.8 kB view details)

Uploaded Python 3

File details

Details for the file github_webhooks_framework2-0.2.2.tar.gz.

File metadata

File hashes

Hashes for github_webhooks_framework2-0.2.2.tar.gz
Algorithm Hash digest
SHA256 e2949cea46ce60e8188327d94a41cdca1cf46b829814a8c80f13d5e0963014b9
MD5 58e53d0d5f5f211eb1293e35756ca1d3
BLAKE2b-256 638eefbcdbceb2e9bcc4a8a6ccbab17ee3bf69e5d3f84994db6b95cd821489d6

See more details on using hashes here.

File details

Details for the file github_webhooks_framework2-0.2.2-py3-none-any.whl.

File metadata

File hashes

Hashes for github_webhooks_framework2-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 70c385b264dad6f1e35f4ee5ec6fe8e68102141ba0de9351326e410df299ca97
MD5 a98ee7eeb6c6846c034139178b21b750
BLAKE2b-256 393e00f9353428be06a755d2f7bc7169c57d44ef1abaa2aafb668ac280f21f2d

See more details on using hashes here.

Supported by

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