Skip to main content

A Python library to supercharge your GitHub organization with bots and webhooks.

Project description

FastGitHub

CI/CD CI Pipeline Release Codecov
Meta Ruff Pre-commit License
Package PyPI - Python version PyPI - Version

About this project

FastGitHub provides a GitHub webhooks handler for FastAPI to automate your workflows.

FastGitHub also provides sets of automations (named recipes).

More informations about Github webhooks and payloads can be found here.

️️⚙️ Installation

Install the package from the PyPI registry.

pip install fastgithub

⚡ Usage

Example

This is a basic example that handles the creation of a PR during a push event and the extraction of labels from the PR's commit messages.

import os

import uvicorn
from fastapi import FastAPI
from github import Auth, Github

from fastgithub import GithubWebhookHandler, SignatureVerificationSHA256, webhook_router
from fastgithub.recipes.github import AutoCreatePullRequest, LabelsFromCommits

signature_verification = SignatureVerificationSHA256(secret="mysecret")  # noqa: S106
webhook_handler = GithubWebhookHandler(signature_verification)

github = Github(auth=Auth.Token(os.environ["GITHUB_TOKEN"]))

webhook_handler.listen("push", [AutoCreatePullRequest(github)])
webhook_handler.listen("pull_request", [LabelsFromCommits(github)])


app = FastAPI()
router = webhook_router(handler=webhook_handler, path="/postreceive")
app.include_router(router)

if __name__ == "__main__":
    uvicorn.run(app)

You can define your own Recipe (or GithubRecipe) by inherit from these classes. A Recipe need a class attribute events that take a list of events to listen to, by default the recipe is listen by any type of event (ie. *).

The webhook_router uses the __call__ method to perform the hooks.

from collections.abc import Callable

from fastgithub import Recipe, GithubRecipe
from fastgithub.helpers.github import GithubHelper
from fastgithub.types import Payload


class Hello(Recipe):
    @property
    def events(self) -> dict[str, Callable]:
        return {"*": self.__call__}

    def __call__(self, payload: Payload):
        print(f"Hello from: {payload['repository']}")


class MyGithubRecipe(GithubRecipe):
    @property
    def events(self) -> dict[str, Callable]:
        return {"push": self.__call__, "pull_request": self.__call__}

    def __call__(self, payload: Payload):
        gh = GithubHelper(self.github, repo_fullname=payload["repository"]["full_name"])
        if not gh.rate_status.too_low():
            print(f"Hello from {gh.repo.full_name}!")

⛏️ Development

In order to install all development dependencies, run the following command:

uv sync

To ensure that you follow the development workflow, please setup the pre-commit hooks:

uv run pre-commit install

Acknowledgements

Initial ideas and designs were inspired by python-github-webhook and python-github-bot-api

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

fastgithub-0.0.4.tar.gz (59.5 kB view details)

Uploaded Source

Built Distribution

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

fastgithub-0.0.4-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file fastgithub-0.0.4.tar.gz.

File metadata

  • Download URL: fastgithub-0.0.4.tar.gz
  • Upload date:
  • Size: 59.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.4

File hashes

Hashes for fastgithub-0.0.4.tar.gz
Algorithm Hash digest
SHA256 c135e9aa119bf1b6fea489142e2dec6484c1487a84e8bb370622669f3b163f74
MD5 0c965b6ccd7b5212fed9fd72bfa7c329
BLAKE2b-256 3d1a6eb7a36fa4a89d4fffe3b850f310d6bd5e73488056e9931ce1ec142fecd7

See more details on using hashes here.

File details

Details for the file fastgithub-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: fastgithub-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 11.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.5.4

File hashes

Hashes for fastgithub-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 4cf96aa16060197c9eebfd4ab414f100e194886fc4a90770228d3e68d3ca88f7
MD5 68edf39422e03ea171fea317fa770b83
BLAKE2b-256 b4f6de2bbe4723e5fc972481ac945feb2cbe7dd3dac168bb60737a0e59dac5db

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