A Python library to supercharge your GitHub organization with bots and webhooks.
Project description
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c135e9aa119bf1b6fea489142e2dec6484c1487a84e8bb370622669f3b163f74
|
|
| MD5 |
0c965b6ccd7b5212fed9fd72bfa7c329
|
|
| BLAKE2b-256 |
3d1a6eb7a36fa4a89d4fffe3b850f310d6bd5e73488056e9931ce1ec142fecd7
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4cf96aa16060197c9eebfd4ab414f100e194886fc4a90770228d3e68d3ca88f7
|
|
| MD5 |
68edf39422e03ea171fea317fa770b83
|
|
| BLAKE2b-256 |
b4f6de2bbe4723e5fc972481ac945feb2cbe7dd3dac168bb60737a0e59dac5db
|