diff --git a/lugito b/lugito index 8b38dfd..ff35040 100755 --- a/lugito +++ b/lugito @@ -1,16 +1,18 @@ #!/usr/bin/env python3 +import re import http import hmac import json import socket import threading import logging +from time import sleep from flask import Flask, request from pprint import pprint from hashlib import sha256 from phabricator import Phabricator -from time import sleep +from launchpadlib.launchpad import Launchpad website = "https://phab.lubuntu.me" phab = Phabricator(host=website+"/api/", token="API KEY") @@ -19,6 +21,12 @@ username = "lugito" server = "irc.freenode.net" port = 6667 channel = "#lubuntu-devel" +lp = Launchpad.login_with("lugito", "staging", "devel") +bugmessage = ("This bug has been marked as fixed in the Git repository: LINK\n" + "The commit message is the following: COMMITMESSAGE\n\n" + "(Note: I am only a bot. If this message was received in error, " + "please contact my owners on the Lubuntu Team.)") +cursupportedrels = ["Cosmic", "Bionic", "Xenial", "Trusty"] logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.DEBUG) logger = logging.getLogger(__name__) @@ -158,6 +166,73 @@ def listenirc(): elif ircmsg.find("https://phab.lubuntu.me/T") != -1: ircbot(ircmsg, "link") +@app.route("/commithook") +def commithook(): + data = request.data + hash = hmac.new(bytes(u"HMAC KEY", "utf-8"), data, sha256) + # We MUST ensure that the request came from Phab. + if hash.hexdigest() == request.headers["X-Phabricator-Webhook-Signature"]: + data = json.loads(data) + logger.debug(data) + + exists = True + try: + # Try to find the object. + search = phab.transaction.search(objectIdentifier=data["object"]["phid"])["data"] + # Find the author too. + userlookup = search[0]["authorPHID"] + who = str(dict(phab.phid.query(phids=[userlookup]))[userlookup]["fullName"]) + # If the object exists, no worries, let's just return a good response. + except http.client.HTTPException: + exists = False + + if exists: + if data["object"]["type"] == "CMIT": + logger.debug("It's a commit!") + commitphid = data["object"]["phid"] + phidquery = phab.phid.query(phids=[commitphid])[commitphid] + commitmessage = phidquery["fullName"].replace(phidquery["name"] + ": ", "") + + # When we're messing with bug importances, we need to be + # absolutely 100% sure we aren't screwing with the rest of the bug. + # Since there's no super clean way to get source package names, + # we have to hardcode them. + lpname = None + if "rDEFAULTSETTINGS" in phidquery["name"]: + lpname = "lubuntu-default-settings" + elif "rART" in phidquery["name"]: + lpname = "lubuntu-artwork" + elif "rCALASETTINGS" in phidquery["name"]: + lpname = "calamares-settings-ubuntu" + else: + logger.debug("Somehow, an unsupported repository showed up here.") + + if lpname: + # https://help.launchpad.net/Code/Git#Linking_to_bugs + regexp = re.compile(r"lp:\s+\#\d+(?:,\s*\#\d+)*") + lpbugs = regexp.search(commitmessage.lower()).group(0).strip(",").replace("#", "") + for bug in lpbugs.split(", "): + goodtask = None + lbug = lp.load("/bugs/"+str(bug).strip()) + bug = lbug + for task in bug.bug_tasks: + for rel in cursupportedrels: + if lpname + " (Ubuntu " + rel + ")" in task.bug_target_display_name: + goodtask = task + break + if not goodtask: + if lpname + " (Ubuntu)" in task.bug_target_display_name: + goodtask = task + if goodtask: + message = bugmessage + message.replace("LINK", website + "/" + phidquery["name"]) + message.replace("COMMITMESSAGE", commitmessage) + bug.newMessage(content=message) + goodtask.status = "Fix Committed" + goodtask.lp_save() + + return "OK" + @app.route("/irc", methods=["POST"]) def main(): data = request.data