Github -> ngrok -> Flask -> Github
A project to listen to incoming Github Repositories webhooks and specifically target at repository creation events to:
- Create an open issue with
- Issue title and body
- Tag
good first issue
- @mention
pandaedward
- Automatically enable protected-branch on default branch
- ngrok
- Allows a web server running in local machine to be exposed to the internet without needing a public URL. More information about
ngrok
HERE - An
ngrok
account and some set up are required. I will discuss more later - In a high level we start up
ngrok
before everything else and configure GitHub to invokengrok
provided URL back up by ourFlask
web server
- Allows a web server running in local machine to be exposed to the internet without needing a public URL. More information about
- Github (obviously)
- A GitHub account (free to create), a GitHub organization (free to create)
- Protected branches are available in public repositories with GitHub Free and GitHub Free for organizations, and in public and private repositories with GitHub Pro, GitHub Team, GitHub Enterprise Cloud, and GitHub Enterprise Server
- If the organization is NOT in one of the paid-for plan, create the repository as Public in the testing step
- Flask - Python
flask
version 1.1.2 is in use. Please checkrequirements.txt
file- In a high level Flask sets up a Python web server to listen to incoming
POST
requests forwarded byngrok
- Pygithub - Python
pygithub
version 1.54.1 is in use. Please checkrequirements.txt
file- The official GitHub python library enables interaction with GitHub API v3
- Create a
ngrok
account, download the executable command and set up auth token like below$ ./ngrok authtoken YOUR_NGROK_AUTH_TOKEN
- Start
ngrok
by$ ./ngrok http 5000
and leave the terminal window running - Make note and copy the command output http url, it looks something like
http://23e5ab11586f.ngrok.io/
- Port 5000 is python
flask
default listening port.ngrok
needs to forward traffic toflask
listening port - For detail please find official ngrok setup guide
- Strictly development only as we are using http. For production please configure https for secure access
- Log on to GitHub and create an organization. Go to the organization's Settings - Webhooks - Add webhook
- I won't go in details about how to do this step. Please find office GitHub documentation Creating webhooks
- Paste the
ngrok
http url to Payload URL field during creating a webhook. Add suffix/payload
to the url. Make it looks something likehttp://23e5ab11586f.ngrok.io/payload
- Set Content type to
application/json
- Choose individual events to trigger the Github webhook. I pick
Repositories
event to limit the webhook call. - Your configuration except Payload URL should look like
- GitHub will now make webhook call to the Payload URL everytime an repository event occurred in the organization.
- Continue on GitHub, generate a
Personal Access Token
. Official GitHub documentation HERE - Clone this repository and
$ cd
to the directory- The stack is tested with Python version
3.6
- Ensure all Python dependencies are installed with command
$ pip3 install requirements.txt
- Use the
Personal Access Token
generated above to set an OS or project level environment variable key:GITHUB_PAT
value:_PERSONAL_ACCESS_TOKEN_
- Start the server with command
$ python3 server.py
. Leave the terminal window running - If
Flask
web server is started successfully it will be reachable viahttp://127.0.0.1:5000
. Test the URL to ensure a200
success code is observed. - Also test browsing to your
ngrok
public urlhttp://NGROK_UUID.ngrok.io/
to make sure a200
success code is there. We knowngrok
tunnel forwarding to our localflask
server on port 5000 is working smoothly.
- The stack is tested with Python version
- On GitHub, navigate to the organization and click on
New
to create an repository - Give the repository a name and choose
Public
to create it as a public repository, protected branch is supported only with one of the paid-for plans for the GitHub organization. Also check one ofAdd a README file
orAdd .gitignore
boxes to set a default branch in which ourPython
script will automatically enable protection upon. - Hit
Create repository
button and GitHub will fire a webhook tongrok
url which will be forwarded to ourflask
web server. Check back onPython
terminal window for console logs. - Navigate to the newly created repository and make sure issue is created and default branch is protected
Inspired by:
https://docs.github.com/en/developers/webhooks-and-events/webhooks/creating-webhooks
https://docs.github.com/en/rest/reference/orgs#webhooks
https://docs.github.com/en/developers/webhooks-and-events/webhooks/configuring-your-server-to-receive-payloads
https://github.com/github/platform-samples/tree/master/hooks/python/configuring-your-server
https://gist.github.com/joseywoermann/3bd07b7e846fca7d54d37fe10328994b
https://pygithub.readthedocs.io/en/latest/introduction.html