Name | fastgithub JSON |
Version |
0.0.5
JSON |
| download |
home_page | None |
Summary | A Python library to supercharge your GitHub organization with bots and webhooks. |
upload_time | 2024-11-27 09:54:03 |
maintainer | None |
docs_url | None |
author | None |
requires_python | >=3.12 |
license | MIT License Copyright (c) 2023 Vincent Duchauffour Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
keywords |
automation
bot
ci
fastapi
github
web
webhook
|
VCS |
 |
bugtrack_url |
|
requirements |
No requirements were recorded.
|
Travis-CI |
No Travis.
|
coveralls test coverage |
No coveralls.
|
<p align="center">
<a href="https://github.com/VDuchauffour/fastgithub">
<img src="https://github.com/VDuchauffour/fastgithub/blob/main/assets/fastgithub.png?raw=true" alt="FastGithub written in white with a drawing of a bolt." width="85%" height="auto">
</a>
</p>
<p align="center" markdown=1>
<i>A Python library to supercharge your GitHub organization with bots and webhooks. </i>
</p>
<p align="center" markdown=1>
<a href="https://github.com/VDuchauffour/fastgithub/actions/workflows/ci.yml">
<img src="https://github.com/VDuchauffour/fastgithub/actions/workflows/ci.yml/badge.svg" alt="CI Pipeline">
</a>
<a href="https://github.com/VDuchauffour/fastgithub/actions/workflows/release.yml">
<img src="https://github.com/VDuchauffour/fastgithub/actions/workflows/release.yml/badge.svg" alt="Release">
</a>
<a href="https://codecov.io/gh/VDuchauffour/fastgithub">
<img src="https://codecov.io/gh/VDuchauffour/fastgithub/branch/main/graph/badge.svg" alt="Codecov">
</a>
<br>
<a href="https://github.com/astral-sh/ruff">
<img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v2.json" alt="Ruff">
</a>
<a href="https://github.com/pre-commit/pre-commit">
<img src="https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit" alt="Pre-commit">
</a>
<a href="https://spdx.org/licenses/">
<img src="https://img.shields.io/github/license/VDuchauffour/fastgithub?color=blueviolet" alt="License">
</a>
<br>
<a href="https://pypi.org/project/fastgithub/">
<img src="https://img.shields.io/pypi/pyversions/fastgithub.svg?logo=python&label=Python&logoColor=gold" alt="PyPI - Python version">
</a>
<a href="https://pypi.org/project/fastgithub/">
<img src="https://img.shields.io/pypi/v/fastgithub.svg?logo=pypi&label=PyPI&logoColor=gold" alt="PyPI - Version">
</a>
</p>
</p>
<hr>
<p align="justify">
<b>FastGitHub</b> is a Python package for <b>FastAPI</b>, offering a GitHub webhooks handler and easy Bot creation utilities, streamlined through <b>recipes</b> for easy operations on Github organizations and repositories.
</p>
<p><b>More informations about Github webhooks and payloads</b>: <a href="https://docs.github.com/en/webhooks/webhook-events-and-payloads">docs.github.com/en/webhooks/webhook-events-and-payloads</a></p>
<hr>
## Features
- ⚙ **Seamless experience**: GitHub webhook handler and router classes that just works.
- ⚡️ **FastAPI native**: Build for FastAPI but can be easily integrate with any WSGI web application framework.
- 🔌 **Battery included**: Come with a set of built-in recipes for the most common GitHub operations.
- ️⛏ **Modularity**: Recipes can be easily defined for tailor-made needs.
## Requirements
<p>Before installing FastGitHub, ensure you have the following prerequisites:</p>
<ul>
<li><b>Python:</b> Version 3.12 or newer.</li>
<li><b>FastAPI:</b> FastGitHub is built to work with FastAPI, so having FastAPI in your project is essential.</li>
</ul>
## ️️Installation
Install the package from the PyPI registry.
```shell
pip install fastgithub
```
## Usage
FastGitHub usually involves 3 steps to handle GitHub webhooks:
1. Define the recipes you want to use.
2. Attach these recipes to a `GithubWebhookHandler`.
3. Include a `webhook_router` in your FastAPI application.
### Recipes
To define a `Recipe` (or `GithubRecipe`), simply add `events` property that returns a `dict` with the events as keys and their methods to execute. Use `*` to trigger the recipe on any events.
To use a `GithubRecipe`, a `Github` instance from [PyGithub](https://github.com/PyGithub/PyGithub) is required when instantiating the class.
You can also use raw functions, although this is not the best solution.
```python
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}!")
def very_simple_recipe(payload: Payload) -> None:
print(f"Hello from: {payload['repository']}")
```
#### Available recipes
- `AutoCreatePullRequest`: create a PR when a new branch is pushed.
- `LabelsFromCommits`: add label to a PR using commit messages (a default config is provided).
GitHub recipes can be imported from `fastgithub.recipes.github`.
### Webhook handler
Here's a basic example how to define a `GithubWebhookHandler` with SHA256 signature verification. Setting `signature_verification=None` allows you to use the handler without signature verification (which is not at all the recommended way to publish GitHub webhook).
```python
from fastgithub import GithubWebhookHandler, SignatureVerificationSHA256
signature_verification = SignatureVerificationSHA256(secret="mysecret")
webhook_handler = GithubWebhookHandler(signature_verification)
```
You can use the `plan` method to set recipes to a handler. The `listen` handler's method allows you attach recipe functions to specific events. The `listen` method can also be used as a decorator.
```python
webhook_handler.plan([Hello()])
webhook_handler.listen("pull_request", [very_simple_recipe])
@webhook_handler.listen("pull_request")
def another_simple_recipe(payload: Payload) -> None:
print(f"Hello from: {payload['repository']}")
```
### Webhook router
The `webhook_router` function returns a `fastapi.APIRouter`. You can adopte the inner logic of this function to suit your needs.
```python
import uvicorn
from fastapi import FastAPI
from fastgithub import webhook_router
app = FastAPI()
router = webhook_router(handler=webhook_handler, path="/postreceive")
app.include_router(router)
if __name__ == "__main__":
uvicorn.run(app)
```
## Development
In order to install all development dependencies, run the following command:
```shell
uv sync
```
To ensure that you follow the development workflow, please setup the pre-commit hooks:
```shell
uv run pre-commit install
```
## Acknowledgements
- Initial ideas and designs were inspired by [python-github-webhook](https://github.com/bloomberg/python-github-webhook) and [python-github-bot-api](https://github.com/NiklasRosenstein/python-github-bot-api/).
- README.md layout was inspired by [FastCRUD](https://github.com/igorbenav/fastcrud).
Raw data
{
"_id": null,
"home_page": null,
"name": "fastgithub",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.12",
"maintainer_email": null,
"keywords": "automation, bot, ci, fastapi, github, web, webhook",
"author": null,
"author_email": "Vincent Duchauffour <vincent.duchauffour@proton.me>",
"download_url": "https://files.pythonhosted.org/packages/5a/af/83f9448f213be51a92b138171084ee672b38f3064a089b2b602596ee59d5/fastgithub-0.0.5.tar.gz",
"platform": null,
"description": "<p align=\"center\">\n <a href=\"https://github.com/VDuchauffour/fastgithub\">\n <img src=\"https://github.com/VDuchauffour/fastgithub/blob/main/assets/fastgithub.png?raw=true\" alt=\"FastGithub written in white with a drawing of a bolt.\" width=\"85%\" height=\"auto\">\n </a>\n</p>\n <p align=\"center\" markdown=1>\n <i>A Python library to supercharge your GitHub organization with bots and webhooks. </i>\n </p>\n <p align=\"center\" markdown=1>\n <a href=\"https://github.com/VDuchauffour/fastgithub/actions/workflows/ci.yml\">\n <img src=\"https://github.com/VDuchauffour/fastgithub/actions/workflows/ci.yml/badge.svg\" alt=\"CI Pipeline\">\n </a>\n <a href=\"https://github.com/VDuchauffour/fastgithub/actions/workflows/release.yml\">\n <img src=\"https://github.com/VDuchauffour/fastgithub/actions/workflows/release.yml/badge.svg\" alt=\"Release\">\n </a>\n <a href=\"https://codecov.io/gh/VDuchauffour/fastgithub\">\n <img src=\"https://codecov.io/gh/VDuchauffour/fastgithub/branch/main/graph/badge.svg\" alt=\"Codecov\">\n </a>\n <br>\n <a href=\"https://github.com/astral-sh/ruff\">\n <img src=\"https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/charliermarsh/ruff/main/assets/badge/v2.json\" alt=\"Ruff\">\n </a>\n <a href=\"https://github.com/pre-commit/pre-commit\">\n <img src=\"https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit\" alt=\"Pre-commit\">\n </a>\n <a href=\"https://spdx.org/licenses/\">\n <img src=\"https://img.shields.io/github/license/VDuchauffour/fastgithub?color=blueviolet\" alt=\"License\">\n </a>\n <br>\n <a href=\"https://pypi.org/project/fastgithub/\">\n <img src=\"https://img.shields.io/pypi/pyversions/fastgithub.svg?logo=python&label=Python&logoColor=gold\" alt=\"PyPI - Python version\">\n </a>\n <a href=\"https://pypi.org/project/fastgithub/\">\n <img src=\"https://img.shields.io/pypi/v/fastgithub.svg?logo=pypi&label=PyPI&logoColor=gold\" alt=\"PyPI - Version\">\n </a>\n </p>\n</p>\n<hr>\n <p align=\"justify\">\n <b>FastGitHub</b> is a Python package for <b>FastAPI</b>, offering a GitHub webhooks handler and easy Bot creation utilities, streamlined through <b>recipes</b> for easy operations on Github organizations and repositories.\n </p>\n<p><b>More informations about Github webhooks and payloads</b>: <a href=\"https://docs.github.com/en/webhooks/webhook-events-and-payloads\">docs.github.com/en/webhooks/webhook-events-and-payloads</a></p>\n<hr>\n\n## Features\n\n- \u2699 **Seamless experience**: GitHub webhook handler and router classes that just works.\n- \u26a1\ufe0f **FastAPI native**: Build for FastAPI but can be easily integrate with any WSGI web application framework.\n- \ud83d\udd0c **Battery included**: Come with a set of built-in recipes for the most common GitHub operations.\n- \ufe0f\u26cf **Modularity**: Recipes can be easily defined for tailor-made needs.\n\n## Requirements\n\n<p>Before installing FastGitHub, ensure you have the following prerequisites:</p>\n<ul>\n <li><b>Python:</b> Version 3.12 or newer.</li>\n <li><b>FastAPI:</b> FastGitHub is built to work with FastAPI, so having FastAPI in your project is essential.</li>\n</ul>\n\n## \ufe0f\ufe0fInstallation\n\nInstall the package from the PyPI registry.\n\n```shell\npip install fastgithub\n```\n\n## Usage\n\nFastGitHub usually involves 3 steps to handle GitHub webhooks:\n\n1. Define the recipes you want to use.\n2. Attach these recipes to a `GithubWebhookHandler`.\n3. Include a `webhook_router` in your FastAPI application.\n\n### Recipes\n\nTo define a `Recipe` (or `GithubRecipe`), simply add `events` property that returns a `dict` with the events as keys and their methods to execute. Use `*` to trigger the recipe on any events.\n\nTo use a `GithubRecipe`, a `Github` instance from [PyGithub](https://github.com/PyGithub/PyGithub) is required when instantiating the class.\n\nYou can also use raw functions, although this is not the best solution.\n\n```python\nfrom collections.abc import Callable\n\nfrom fastgithub import Recipe, GithubRecipe\nfrom fastgithub.helpers.github import GithubHelper\nfrom fastgithub.types import Payload\n\n\nclass Hello(Recipe):\n @property\n def events(self) -> dict[str, Callable]:\n return {\"*\": self.__call__}\n\n def __call__(self, payload: Payload):\n print(f\"Hello from: {payload['repository']}\")\n\n\nclass MyGithubRecipe(GithubRecipe):\n @property\n def events(self) -> dict[str, Callable]:\n return {\"push\": self.__call__, \"pull_request\": self.__call__}\n\n def __call__(self, payload: Payload):\n gh = GithubHelper(self.github, repo_fullname=payload[\"repository\"][\"full_name\"])\n if not gh.rate_status.too_low():\n print(f\"Hello from {gh.repo.full_name}!\")\n\n\ndef very_simple_recipe(payload: Payload) -> None:\n print(f\"Hello from: {payload['repository']}\")\n```\n\n#### Available recipes\n\n- `AutoCreatePullRequest`: create a PR when a new branch is pushed.\n- `LabelsFromCommits`: add label to a PR using commit messages (a default config is provided).\n\nGitHub recipes can be imported from `fastgithub.recipes.github`.\n\n### Webhook handler\n\nHere's a basic example how to define a `GithubWebhookHandler` with SHA256 signature verification. Setting `signature_verification=None` allows you to use the handler without signature verification (which is not at all the recommended way to publish GitHub webhook).\n\n```python\nfrom fastgithub import GithubWebhookHandler, SignatureVerificationSHA256\n\nsignature_verification = SignatureVerificationSHA256(secret=\"mysecret\")\nwebhook_handler = GithubWebhookHandler(signature_verification)\n```\n\nYou can use the `plan` method to set recipes to a handler. The `listen` handler's method allows you attach recipe functions to specific events. The `listen` method can also be used as a decorator.\n\n```python\nwebhook_handler.plan([Hello()])\n\nwebhook_handler.listen(\"pull_request\", [very_simple_recipe])\n\n\n@webhook_handler.listen(\"pull_request\")\ndef another_simple_recipe(payload: Payload) -> None:\n print(f\"Hello from: {payload['repository']}\")\n```\n\n### Webhook router\n\nThe `webhook_router` function returns a `fastapi.APIRouter`. You can adopte the inner logic of this function to suit your needs.\n\n```python\nimport uvicorn\nfrom fastapi import FastAPI\n\nfrom fastgithub import webhook_router\n\napp = FastAPI()\nrouter = webhook_router(handler=webhook_handler, path=\"/postreceive\")\napp.include_router(router)\n\nif __name__ == \"__main__\":\n uvicorn.run(app)\n```\n\n## Development\n\nIn order to install all development dependencies, run the following command:\n\n```shell\nuv sync\n```\n\nTo ensure that you follow the development workflow, please setup the pre-commit hooks:\n\n```shell\nuv run pre-commit install\n```\n\n## Acknowledgements\n\n- Initial ideas and designs were inspired by [python-github-webhook](https://github.com/bloomberg/python-github-webhook) and [python-github-bot-api](https://github.com/NiklasRosenstein/python-github-bot-api/).\n- README.md layout was inspired by [FastCRUD](https://github.com/igorbenav/fastcrud).\n",
"bugtrack_url": null,
"license": "MIT License Copyright (c) 2023 Vincent Duchauffour Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.",
"summary": "A Python library to supercharge your GitHub organization with bots and webhooks. ",
"version": "0.0.5",
"project_urls": {
"Documentation": "https://github.com/VDuchauffour/fastgithub",
"Homepage": "https://github.com/VDuchauffour/fastgithub"
},
"split_keywords": [
"automation",
" bot",
" ci",
" fastapi",
" github",
" web",
" webhook"
],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "011b1fd08c79f7a0616ee39337039cbd09e3e3899f7ec0e60b246fc6e7ae6262",
"md5": "ee01a815ff47947bce0f141b43b0f591",
"sha256": "8e98e0fac4bed4cc9d36a4f81dc75c3c7a4f51a9380f45136f4aa61cd897e2b1"
},
"downloads": -1,
"filename": "fastgithub-0.0.5-py3-none-any.whl",
"has_sig": false,
"md5_digest": "ee01a815ff47947bce0f141b43b0f591",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.12",
"size": 12983,
"upload_time": "2024-11-27T09:54:02",
"upload_time_iso_8601": "2024-11-27T09:54:02.439938Z",
"url": "https://files.pythonhosted.org/packages/01/1b/1fd08c79f7a0616ee39337039cbd09e3e3899f7ec0e60b246fc6e7ae6262/fastgithub-0.0.5-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "5aaf83f9448f213be51a92b138171084ee672b38f3064a089b2b602596ee59d5",
"md5": "47c56de08ee49d312365f63cd6af6266",
"sha256": "938f700f8a0b59b9a7606d9e3714d33d86cda4c5b98f853fb2a70e43655bb923"
},
"downloads": -1,
"filename": "fastgithub-0.0.5.tar.gz",
"has_sig": false,
"md5_digest": "47c56de08ee49d312365f63cd6af6266",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.12",
"size": 91334,
"upload_time": "2024-11-27T09:54:03",
"upload_time_iso_8601": "2024-11-27T09:54:03.934139Z",
"url": "https://files.pythonhosted.org/packages/5a/af/83f9448f213be51a92b138171084ee672b38f3064a089b2b602596ee59d5/fastgithub-0.0.5.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-11-27 09:54:03",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "VDuchauffour",
"github_project": "fastgithub",
"travis_ci": false,
"coveralls": false,
"github_actions": true,
"lcname": "fastgithub"
}