Skip to main content

Flask extension for creating Github Apps

Project description

Flask-GithubApplication

PyPI - Downloads CodeQL

Flask extension for rapid Github app development in Python, in the spirit of probot

GitHub Apps help automate GitHub workflows. Examples include preventing merging of pull requests with "WIP" in the title or closing stale issues and pull requests.

Getting Started

Installation

To install Flask-GitHubApplication:

pip install flask-githubapplication

Create GitHub App

Follow GitHub's docs on creating a github app.

You can, in principle, register any type of payload to be sent to the app!

Once you do this, please note down the GitHub app Id, the GitHub app secret, and make sure to create a private key for it! These three elements are required to run your app.

Build the Flask App

The GithubApp package has a decorator, @on, that will allow you to register events, and actions, to specific functions. For instance,

@github_app.on('issues.opened')
def cruel_closer():
    #do stuff here

Will trigger whenever the app receives a Github payload with the X-Github-Event header set to issues, and an action field in the payload field containing opened

Following this logic, you can make your app react in a unique way for every combination of event and action. Refer to the Github documentation for all the details about events and the actions they support, as well as for sample payloads for each. You can also have something like

@github_app.on('issues')
def issue_tracker():
    #do stuff here

The above function will do stuff here for every issues event received. This can be useful for specific workflows, to bring developers in early.

Inside the function, you can access the received request via the conveniently named request variable. You can access its payload by simply getting it: request.payload

You can find a complete example (containing this cruel_closer function), in the samples folder of this repo. It is a fully functioning flask Github App.

Run it locally

For quick iteration, you can set up your environment as follows:

EXPORT GITHUBAPP_SECRET=False # this will circumvent request verification
EXPORT FLASK_APP=/path/to/your/flask/app.py # the file does not need to be named app.py! But it has to be the python file that instantiates the Flask app. For instance, samples/cruel_closer/app.py

This will make your flask application run in debug mode. This means that, as you try sending payloads and tweak functions, fix issues, etc., as soon as you save the python code, the flask application will reload itself and run the new code immediately. Once that is in place, run your github app

flask run

Now, you can send requests! The port is 5000 by default but that can also be overridden. Check flask run --help for more details. Anyway! Now, on to sending test payloads!

curl -H "X-GitHub-Event: <your_event>" -H "Content-Type: application/json" -X POST -d @./path/to/payload.json http://localhost:5000

Install your GitHub App

Settings > Applications > Configure

If you were to install the cruel closer app, any repositories that you give the GitHub app access to will cruelly close all new issues, be careful.

Deploy your GitHub App

Bear in mind that you will need to run the app somewhere. It is possible, and fairly easy, to host the app in something like Kubernetes, or simply containerised, in a machine somewhere. You will need to be careful to expose the flask app port to the outside world so the app can receive the payloads from Github. The deployed flask app will need to be reachable from the same URL you set as the webhook url. However, this is getting a little bit into Docker/Kubernetes territory so we will not go too deep.

Usage

GitHubApp Instance Attributes

payload: In the context of a hook request, a Python dict representing the hook payload (raises a RuntimeError outside a hook context).

installation_token: The token used to authenticate as the app installation. This can be used to call api's not supported by GhApi like Github's GraphQL API

GithubApp Instance Methods

client: a GhApi client authenticated as the app installation (raises a RuntimeError inside a hook context without a valid request)

Configuration

GITHUBAPP_ID: GitHub app ID as an int (required). Default: None

GITHUBAPP_KEY: Private key used to sign access token requests as bytes or utf-8 encoded string (required). Default: None

GITHUBAPP_SECRET: Secret used to secure webhooks as bytes or utf-8 encoded string (required). Set to False to disable verification.

GITHUBAPP_URL: URL of GitHub instance (used for GitHub Enterprise) as a string. Default: None

GITHUBAPP_ROUTE: Path used for GitHub hook requests as a string. Default: '/'

You can find an example on how to init all these config variables in the cruel_closer sample app

Cruel Closer

The cruelcloser sample app will use information of the received payload (which is received every time an issue is opened), will _find said issue and close it without regard.

Credits:

Much of this project originated from flask-github. It currently uses Github3.py which was missing many endpoints and lacked good documentation.

This version will be using GhApi instead since it has 100% api coverage.

Due to the breaking nature and downstream effects of this changes, a new project was created.

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

Flask-GitHubApplication-1.2.0.tar.gz (19.8 kB view details)

Uploaded Source

Built Distribution

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

Flask_GitHubApplication-1.2.0-py3-none-any.whl (20.4 kB view details)

Uploaded Python 3

File details

Details for the file Flask-GitHubApplication-1.2.0.tar.gz.

File metadata

  • Download URL: Flask-GitHubApplication-1.2.0.tar.gz
  • Upload date:
  • Size: 19.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.8.11

File hashes

Hashes for Flask-GitHubApplication-1.2.0.tar.gz
Algorithm Hash digest
SHA256 0bba1e5288905d6ea54f88026c306e68c3296311bc8ee00fa37ef531c53f82f8
MD5 419c16af52f62f55312a5589d8fc6d04
BLAKE2b-256 d9f14ec384e9a0df40abc02eef4a60f88fb2fe6a92c3a7ea7b79cde053e3a61a

See more details on using hashes here.

File details

Details for the file Flask_GitHubApplication-1.2.0-py3-none-any.whl.

File metadata

  • Download URL: Flask_GitHubApplication-1.2.0-py3-none-any.whl
  • Upload date:
  • Size: 20.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.6.4 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.1 CPython/3.8.11

File hashes

Hashes for Flask_GitHubApplication-1.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8495a3b86fdf944de695946148b100b0c3b0e278435a4e41eb19259590842dc0
MD5 585f608b93670aaf46079246f9a63625
BLAKE2b-256 41f47d645d6df257a2ba5dbd97ca4fe42b9d982917075c79e8729f1ed089e875

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