Skip to content

Commit

Permalink
Getting relase event set up
Browse files Browse the repository at this point in the history
  • Loading branch information
suhay committed Jul 3, 2020
1 parent 74ae67a commit 6e767a0
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 1 deletion.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,5 @@ dmypy.json

# Pyre type checker
.pyre/

*.json
51 changes: 50 additions & 1 deletion README.md
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.
32 changes: 32 additions & 0 deletions setup.py
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 added sites/.gitkeep
Empty file.
44 changes: 44 additions & 0 deletions src/handler.py
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()
22 changes: 22 additions & 0 deletions src/release.py
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

0 comments on commit 6e767a0

Please sign in to comment.