Skip to content

Commit 6e767a0

Browse files
committed
Getting relase event set up
1 parent 74ae67a commit 6e767a0

File tree

6 files changed

+150
-1
lines changed

6 files changed

+150
-1
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,3 +127,5 @@ dmypy.json
127127

128128
# Pyre type checker
129129
.pyre/
130+
131+
*.json

README.md

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,50 @@
1-
# github-webhooks-listener
1+
# GitHub Webhooks Listener
2+
3+
[![Python 3.8](https://img.shields.io/badge/python-3.8-blue.svg)](https://www.python.org/downloads/release/python-380/)
4+
5+
A simple listener that will trigger custom scripts when it receives events from GitHub.
6+
7+
## Usage
8+
9+
```bash
10+
$ git clone git@github.com:suhay/github-webhooks-listener.git
11+
$ cd github-webhooks-listener
12+
$ python setup.py install --user
13+
```
14+
15+
### .env file
16+
17+
```
18+
API_TOKEN=YOUR_GITHUB_SECRET
19+
```
20+
21+
### Repo configuration files
22+
23+
```bash
24+
.
25+
├── README.md
26+
└── sites
27+
└── my-site.json
28+
```
29+
30+
```json
31+
my-site.json
32+
33+
{
34+
"path": "/home/code/my-site",
35+
"release": {
36+
"build": "yarn && yarn build",
37+
"deploy": "rsync -av --delete public/ /var/www/html/my-site"
38+
}
39+
}
40+
```
41+
42+
### Adding listener to GitHub Webhooks
43+
44+
As of `v0.1.0` - Only the `release` event is supported.
45+
46+
`https://{domain}/webhooks/{repo}`
47+
or
48+
`https://yoursite.com/webhooks/my-site`
49+
50+
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.

setup.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from setuptools import setup, find_packages
2+
3+
from os import path
4+
5+
this_directory = path.abspath(path.dirname(__file__))
6+
with open(path.join(this_directory, 'README.md'), encoding='utf-8') as f:
7+
long_description = f.read()
8+
9+
setup(
10+
name="github-webhooks-listener",
11+
version="0.1.0",
12+
author="Matt Suhay",
13+
author_email="matt@suhay.dev",
14+
description="A simple listener that will trigger custom scripts when it receives events from GitHub.",
15+
long_description=long_description,
16+
long_description_content_type="text/markdown",
17+
license = "MIT",
18+
url="https://github.com/suhay/github-webhooks-listener",
19+
packages=find_packages(),
20+
keywords = "github webhooks",
21+
classifiers=[
22+
"Programming Language :: Python :: 3",
23+
"Development Status :: 4 - Beta",
24+
"License :: MIT License",
25+
"Operating System :: OS Independent",
26+
],
27+
install_requires=[
28+
'quart',
29+
'python-dotenv'
30+
],
31+
python_requires='>=3.8',
32+
)

sites/.gitkeep

Whitespace-only changes.

src/handler.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
from quart import Quart, request
2+
3+
from dotenv import load_dotenv
4+
load_dotenv()
5+
6+
from release import processRelease
7+
8+
import os
9+
import hashlib
10+
import hmac
11+
import asyncio
12+
13+
14+
token = os.environ.get("API_TOKEN")
15+
tokenb = bytes(token, 'utf-8')
16+
17+
app = Quart(__name__)
18+
19+
@app.route('/webhooks/<repo>', methods=['GET','POST'])
20+
async def webhooks(repo):
21+
if request.is_json:
22+
data = await request.data
23+
signature = hmac.new(tokenb, data, hashlib.sha1).hexdigest()
24+
25+
if 'X-Hub-Signature' in request.headers.keys() and hmac.compare_digest(signature, request.headers['X-Hub-Signature'].split('=')[1]):
26+
payload = await request.get_json()
27+
28+
if payload['repository']['name'] == repo:
29+
if payload['action'] == 'released' and 'release' in payload.keys():
30+
asyncio.ensure_future(processRelease(repo, payload))
31+
32+
return 'Thanks!', 202
33+
34+
else:
35+
return 'Signature is wrong...', 401
36+
37+
else:
38+
return 'Not sure what this is...', 418
39+
40+
@app.errorhandler(404)
41+
def page_not_found(e):
42+
return "?", 404
43+
44+
app.run()

src/release.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import json
2+
import subprocess
3+
4+
5+
async def processRelease(repo, payload):
6+
with open('sites/' + repo + '.json') as f:
7+
data = json.load(f)
8+
9+
if 'release' in data.keys() and 'path' in data.keys():
10+
commands = ['. ~/.nvm/nvm.sh', 'nvm use']
11+
12+
if 'build' in data['release'].keys():
13+
commands.append(data['release']['build'])
14+
15+
if 'deploy' in data['release'].keys():
16+
commands.append(data['release']['deploy'])
17+
18+
subprocess.check_call(['git', 'fetch', '--all', '--tags'], cwd=data['path'])
19+
subprocess.check_call(['git', 'checkout', 'tags/' + payload['release']['tag_name']], cwd=data['path'])
20+
subprocess.Popen(' && '.join(commands), cwd=data['path'], executable='/bin/bash', shell=True)
21+
22+
return

0 commit comments

Comments
 (0)