-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
150 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -127,3 +127,5 @@ dmypy.json | |
|
||
# Pyre type checker | ||
.pyre/ | ||
|
||
*.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,50 @@ | ||
# github-webhooks-listener | ||
# GitHub Webhooks Listener | ||
|
||
[![Python 3.8](https://img.shields.io/badge/python-3.8-blue.svg)](https://www.python.org/downloads/release/python-380/) | ||
|
||
A simple listener that will trigger custom scripts when it receives events from GitHub. | ||
|
||
## Usage | ||
|
||
```bash | ||
$ git clone git@github.com:suhay/github-webhooks-listener.git | ||
$ cd github-webhooks-listener | ||
$ python setup.py install --user | ||
``` | ||
|
||
### .env file | ||
|
||
``` | ||
API_TOKEN=YOUR_GITHUB_SECRET | ||
``` | ||
|
||
### Repo configuration files | ||
|
||
```bash | ||
. | ||
├── README.md | ||
└── sites | ||
└── my-site.json | ||
``` | ||
|
||
```json | ||
my-site.json | ||
|
||
{ | ||
"path": "/home/code/my-site", | ||
"release": { | ||
"build": "yarn && yarn build", | ||
"deploy": "rsync -av --delete public/ /var/www/html/my-site" | ||
} | ||
} | ||
``` | ||
|
||
### Adding listener to GitHub Webhooks | ||
|
||
As of `v0.1.0` - Only the `release` event is supported. | ||
|
||
`https://{domain}/webhooks/{repo}` | ||
or | ||
`https://yoursite.com/webhooks/my-site` | ||
|
||
The `repo` name must match the repository name (minus the user/org name) sent from GitHub and also the respective `.json` file that contains its custom scripts. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
from setuptools import setup, find_packages | ||
|
||
from os import path | ||
|
||
this_directory = path.abspath(path.dirname(__file__)) | ||
with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f: | ||
long_description = f.read() | ||
|
||
setup( | ||
name="github-webhooks-listener", | ||
version="0.1.0", | ||
author="Matt Suhay", | ||
author_email="matt@suhay.dev", | ||
description="A simple listener that will trigger custom scripts when it receives events from GitHub.", | ||
long_description=long_description, | ||
long_description_content_type="text/markdown", | ||
license = "MIT", | ||
url="https://github.com/suhay/github-webhooks-listener", | ||
packages=find_packages(), | ||
keywords = "github webhooks", | ||
classifiers=[ | ||
"Programming Language :: Python :: 3", | ||
"Development Status :: 4 - Beta", | ||
"License :: MIT License", | ||
"Operating System :: OS Independent", | ||
], | ||
install_requires=[ | ||
'quart', | ||
'python-dotenv' | ||
], | ||
python_requires='>=3.8', | ||
) |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
from quart import Quart, request | ||
|
||
from dotenv import load_dotenv | ||
load_dotenv() | ||
|
||
from release import processRelease | ||
|
||
import os | ||
import hashlib | ||
import hmac | ||
import asyncio | ||
|
||
|
||
token = os.environ.get("API_TOKEN") | ||
tokenb = bytes(token, 'utf-8') | ||
|
||
app = Quart(__name__) | ||
|
||
@app.route('/webhooks/<repo>', methods=['GET','POST']) | ||
async def webhooks(repo): | ||
if request.is_json: | ||
data = await request.data | ||
signature = hmac.new(tokenb, data, hashlib.sha1).hexdigest() | ||
|
||
if 'X-Hub-Signature' in request.headers.keys() and hmac.compare_digest(signature, request.headers['X-Hub-Signature'].split('=')[1]): | ||
payload = await request.get_json() | ||
|
||
if payload['repository']['name'] == repo: | ||
if payload['action'] == 'released' and 'release' in payload.keys(): | ||
asyncio.ensure_future(processRelease(repo, payload)) | ||
|
||
return 'Thanks!', 202 | ||
|
||
else: | ||
return 'Signature is wrong...', 401 | ||
|
||
else: | ||
return 'Not sure what this is...', 418 | ||
|
||
@app.errorhandler(404) | ||
def page_not_found(e): | ||
return "?", 404 | ||
|
||
app.run() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import json | ||
import subprocess | ||
|
||
|
||
async def processRelease(repo, payload): | ||
with open('sites/' + repo + '.json') as f: | ||
data = json.load(f) | ||
|
||
if 'release' in data.keys() and 'path' in data.keys(): | ||
commands = ['. ~/.nvm/nvm.sh', 'nvm use'] | ||
|
||
if 'build' in data['release'].keys(): | ||
commands.append(data['release']['build']) | ||
|
||
if 'deploy' in data['release'].keys(): | ||
commands.append(data['release']['deploy']) | ||
|
||
subprocess.check_call(['git', 'fetch', '--all', '--tags'], cwd=data['path']) | ||
subprocess.check_call(['git', 'checkout', 'tags/' + payload['release']['tag_name']], cwd=data['path']) | ||
subprocess.Popen(' && '.join(commands), cwd=data['path'], executable='/bin/bash', shell=True) | ||
|
||
return |