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
ngrokHERE - An
ngrokaccount and some set up are required. I will discuss more later - In a high level we start up
ngrokbefore everything else and configure GitHub to invokengrokprovided URL back up by ourFlaskweb 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
flaskversion 1.1.2 is in use. Please checkrequirements.txtfile- In a high level Flask sets up a Python web server to listen to incoming
POSTrequests forwarded byngrok
- Pygithub - Python
pygithubversion 1.54.1 is in use. Please checkrequirements.txtfile- The official GitHub python library enables interaction with GitHub API v3
- Create a
ngrokaccount, download the executable command and set up auth token like below$ ./ngrok authtoken YOUR_NGROK_AUTH_TOKEN- Start
ngrokby$ ./ngrok http 5000and 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
flaskdefault listening port.ngrokneeds to forward traffic toflasklistening 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
ngrokhttp url to Payload URL field during creating a webhook. Add suffix/payloadto 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
Repositoriesevent 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
$ cdto 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 Tokengenerated above to set an OS or project level environment variable key:GITHUB_PATvalue:_PERSONAL_ACCESS_TOKEN_ - Start the server with command
$ python3 server.py. Leave the terminal window running - If
Flaskweb server is started successfully it will be reachable viahttp://127.0.0.1:5000. Test the URL to ensure a200success code is observed. - Also test browsing to your
ngrokpublic urlhttp://NGROK_UUID.ngrok.io/to make sure a200success code is there. We knowngroktunnel forwarding to our localflaskserver on port 5000 is working smoothly.
- The stack is tested with Python version
- On GitHub, navigate to the organization and click on
Newto create an repository - Give the repository a name and choose
Publicto 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 fileorAdd .gitignoreboxes to set a default branch in which ourPythonscript will automatically enable protection upon. - Hit
Create repositorybutton and GitHub will fire a webhook tongrokurl which will be forwarded to ourflaskweb server. Check back onPythonterminal 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
