Created
November 26, 2017 08:46
-
-
Save blackandred/3d879715bdc3dc232de11d63cdfa2e88 to your computer and use it in GitHub Desktop.
Gogs: Push hook handler
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
import tornado.ioloop | |
import tornado.web | |
import json | |
import os | |
class MainHandler(tornado.web.RequestHandler): | |
config = {} | |
def initialize(self, config): | |
self.config = config | |
def get(self): | |
""" | |
Handles GET method | |
:return: | |
""" | |
self.set_status(200) | |
self.write('{"status": true, "message": "pong"}') | |
def post(self): | |
""" | |
Handles POST method | |
:return: | |
""" | |
try: | |
data = json.loads(self.request.body) | |
if data['secret'] in self.config: | |
print('Running deployment command "' + str(self.config[data['secret']]['deployment_command'])) | |
os.system(str(self.config[data['secret']]['deployment_command'])) | |
else: | |
print('Invalid key "' + str(data['secret']) + '"') | |
self.set_status(200) | |
self.write('{"status": true}') | |
except Exception as e: | |
print(e) | |
self.set_status(400) | |
self.write('{"status": false}') | |
self.finish() | |
class GitHookHandlerApplication: | |
config = {} | |
def __init__(self): | |
""" | |
Load config | |
:return: | |
""" | |
if os.path.isfile(os.path.expanduser('~/.git-hook-handler')): | |
self.config = json.loads(open(os.path.expanduser('~/.git-hook-handler'), 'r').read()) | |
def get_server(self): | |
""" | |
Returns server instance | |
:return: | |
""" | |
return tornado.web.Application([ | |
(r"/", MainHandler, dict(config=self.config)), | |
(r"/ping", MainHandler, dict(config=self.config)), | |
]) | |
if __name__ == "__main__": | |
app = GitHookHandlerApplication() | |
app.get_server().listen(5440) | |
tornado.ioloop.IOLoop.current().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment