From 438c2eadd375e21ff7f52452553925845350be41 Mon Sep 17 00:00:00 2001 From: Jesse Morgan Date: Sun, 25 Jan 2015 20:48:45 -0800 Subject: Adding HTTP request to webhook. --- deployerbot.py | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/deployerbot.py b/deployerbot.py index 2ce5b0b..c596b06 100644 --- a/deployerbot.py +++ b/deployerbot.py @@ -1,5 +1,7 @@ -#!/usr/bin/python +import hmac, hashlib +import json import re +import requests """ DeployerBot sends fires webhooks on command. Although you could really do @@ -61,7 +63,34 @@ class DeployerBot: return def deploy(self, IRC, sender, dest, application): - dest.msg("Ok, I will deploy %s" % application) + key = application.lower() + if key not in self.applications: + dest.msg("I don't know how to deploy %s" % application) + return + + app, repo, url, secret = self.applications[key] + dest.msg("Ok, I'm deploying %s..." % application) + + payload = { + "ref": "refs/heads/master", + "repository": { + "name": repo, + "clone_url": "https://github.com/%s.git" % repo, + } + } + data = json.dumps(payload) + + headers = {'content-type': 'application/json', 'X-GitHub-Event': 'push'} + if secret: + digest = hmac.new(secret.encode(), data.encode(), hashlib.sha1) + headers['X-Hub-Signature'] = "sha1=" + digest.hexdigest() + + r = requests.post(url, data=data, headers=headers) + + if r.status_code == requests.codes.ok: + dest.msg("I successfully deployed %s" % application) + else: + dest.msg("I failed to deploy %s. I got HTTP status code %d" % (application, r.status_code)) def listWebHooks(self, IRC, sender, dest): -- cgit v1.2.3