Skip to main content

Python GitHub Webhooks Framework

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-framework package.
Example:

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

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 must be any of this signatures:

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

And the last - let's launch it.
For example with uvicorn

uvicorn example:app

Webhook will be available on http://localhost:8000/hook

That's it! Now you have a webhook server, which can handle incoming Github Webhook requests.

Metadata

Release files for github-webhooks-framework 0.1.11

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for github-webhooks-framework 0.1.11
File Size Uploaded Provenance Actions
github-webhooks-framework-0.1.11.tar.gz 19.0 kB Without provenance Details for github-webhooks-framework-0.1.11.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for github-webhooks-framework 0.1.11
File Interpreter ABI Platform Actions
github_webhooks_framework-0.1.11-py3-none-any.whl Python 3 none any Details for github_webhooks_framework-0.1.11-py3-none-any.whl

Total release size: 52.7 kB

Release files / github-webhooks-framework-0.1.11.tar.gz

Download URL github-webhooks-framework-0.1.11.tar.gz
Size 19.0 kB
Tags Source
SHA-256 checksum
How to use checksums
d2a0402fc2532347513c620a7de328e89f4f3abaa0d21b57a6f46a4d2e0a7abf
BLAKE2b-256 checksum
How to use checksums
233dc5eac245daae4f22b7e31c93e30bc0ab7bc2c6a3472bb6cc690dbe0cfd20
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.9.10 Linux/5.11.0-1028-azure

Release files / github_webhooks_framework-0.1.11-py3-none-any.whl

Download URL github_webhooks_framework-0.1.11-py3-none-any.whl
Size 33.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
90627550f65fbac395a4ff45a41e103333a1765b0f104a3ea060c7ab62276f32
BLAKE2b-256 checksum
How to use checksums
db87759636e602b5071ecbf0e080dfedb1e2f93fa602a731f342a93bedbab1d8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.13 CPython/3.9.10 Linux/5.11.0-1028-azure

Security for github-webhooks-framework 0.1.11

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page