Skip to content
Snippets Groups Projects
Commit dde4d085 authored by dario's avatar dario
Browse files

initial commit

parents
No related branches found
No related tags found
No related merge requests found
notify.py 0 → 100644
#!/usr/bin/python
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
import json
from tempfile import mkdtemp
from shutil import rmtree
import subprocess
import os
import time
GITLAB_TOKEN = "TODO"
def handle_statusupdate(j, notifier):
if u'object_kind' not in j:
print("no u'object_kind' in j (" + j + "), ignoring")
return
## TODO: more kinds
if j[u'object_kind'] == u"issue":
## TODO: make sure all these exist before accessing
notifier.notify("> {} {} issue #{}: {} in {} [{}]".format(
j[u'user'][u'username'],
j[u'object_attributes'][u'state'],
j[u'object_attributes'][u'iid'],
j[u'object_attributes'][u'title'],
j[u'project'][u'path_with_namespace'],
", ".join([x[u'title'] for x in j[u'labels'] if u'title' in x])))
elif j[u'object_kind'] == u"push":
## TODO: make sure all these exist before accessing
notifier.notify("> {} pushed {} to {}".format(
j[u'user_name'],
"1 commit" if len(j[u'commits']) == 1 else "{} commits".format(len(j[u'commits'])),
j[u'project'][u'path_with_namespace']))
else:
notifier.notify("> unknown/unimplemented issue kind {}".format(
j[u'object_kind']))
class BaseNotification(object):
pass
class IINotification(BaseNotification):
def __init__(self, ircsrv, chan):
super(IINotification, self).__init__()
self.ircsrv = ircsrv
self.chan = chan
self.proc = None
self.dir = mkdtemp()
def start_ii_proc(self):
self.proc = subprocess.Popen(["ii", "-s", self.ircsrv, "-i",
self.dir, "-n", "ircnotify", "ask dario for stuff"])
## well.. i don't know of a better way to wait for ii
## initialization to be done enough
cmd_in = os.path.join(self.dir, self.ircsrv, "in")
while not os.path.exists(cmd_in):
time.sleep(0.01)
with open(cmd_in, "a") as c:
c.write("/j " + self.chan + "\n")
notify_in = os.path.join(self.dir, self.ircsrv, self.chan, "in")
while not os.path.exists(notify_in):
time.sleep(0.01)
def notify(self, message):
cmd_in = os.path.join(self.dir, self.ircsrv, self.chan, "in")
with open(cmd_in, "a") as c:
c.write(message + "\n")
def cleanup(self):
self.proc.terminate()
rmtree(self.dir)
def main():
"""
the main event.
"""
nf = IINotification("irc.fu-berlin.de", "#blumenwiese")
nf.start_ii_proc()
print("ii running..")
nf.notify("test-hi")
## inline so we have nf in scope
class WebhookReceiver(BaseHTTPRequestHandler):
def do_POST(self):
self.rfile._sock.settimeout(5)
secret_token = self.headers.get("X-Gitlab-Token")
if secret_token != GITLAB_TOKEN:
print("request with invalid gitlab secret token '" +
secret_token + "', ignoring")
return
data_string = self.rfile.read(int(self.headers['Content-Length']))
## send dummy OK answer
message = 'OK'
self.send_response(200)
self.send_header("Content-type", "text")
self.send_header("Content-length", str(len(message)))
self.end_headers()
self.wfile.write(message)
## parse data
j = json.loads(data_string)
handle_statusupdate(j, nf)
try:
server = HTTPServer(('', 1234), WebhookReceiver)
print('started web server...')
server.serve_forever()
except KeyboardInterrupt:
print('ctrl-c pressed, shutting down.')
server.socket.close()
finally:
nf.cleanup()
if __name__ == '__main__':
main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment