aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatt Hart <matthew.hart@linaro.org>2015-11-10 22:10:23 +0000
committerMatt Hart <matthew.hart@linaro.org>2015-11-12 12:54:24 +0000
commit89175994028df15a480314fe478f26e6cf4e630e (patch)
tree149af09ab5d6fc4148b09fd17abfad7219a4066e
parent849113e10be0600d8260d3207eb35914e7b5939c (diff)
Add a bugzilla commit hook script to update bugs
This script is executed as the "post-recieve" git hook via a wrapper to add the username/password. It is passed args via STDIN. It searches for bug information in the commit message and sets the bug to resolved fixed if it finds any. Waiting on comment from stakeholders to know if it should comment on the bug aswell. Change-Id: I2cb0236416aba41e8f1bb1223be45172ab83e2e5
-rwxr-xr-xbugzilla-post-receive.py96
1 files changed, 96 insertions, 0 deletions
diff --git a/bugzilla-post-receive.py b/bugzilla-post-receive.py
new file mode 100755
index 0000000..11a8100
--- /dev/null
+++ b/bugzilla-post-receive.py
@@ -0,0 +1,96 @@
+#!/usr/bin/python2.7
+
+import requests
+
+import argparse
+import json
+import os
+import re
+import sys
+import subprocess
+
+data = sys.stdin.readline().strip().split()
+OLDREV = data[0]
+NEWREV = data[1]
+REF = data[2]
+REGEX = "bug\s*(?:#|)\s*(?P<bug>\d+)"
+
+parser = argparse.ArgumentParser(
+ description='Git commit hook for updating bugzilla')
+parser.add_argument('--apikey', "-u", help="Bugzilla REST API Key")
+parser.add_argument('--base', "-b",
+ help="Bugzilla url base (https://bugs.linaro.org)",
+ default="https://bugs.linaro.org")
+args = parser.parse_args()
+
+HEADERS = {"Content-Type": "application/json", "Accept": "application/json"}
+BUGZILLA_REST = "%s/rest" % args.base
+BUGZILLA_LINK_BASE = "%s/show_bug.cgi?id=" % args.base
+GIT_URL_BASE = "https://git.linaro.org"
+GIT_DIR_BASE = "/srv/respositories"
+
+
+def generate_web_url(rev):
+ pwd = os.environ["PWD"]
+ if re.match(GIT_DIR_BASE, pwd):
+ dir = pwd.split(GIT_DIR_BASE)[1]
+ else:
+ dir = pwd
+ return "%s%s.git/commit/%s" % (GIT_URL_BASE, dir, rev)
+
+
+def do_put(url, data):
+ requests.put(url, data=json.dumps(data), headers=HEADERS)
+
+
+def set_resolved_fixed(id):
+ print "Updating bug at %s%i to: RESOLVED_FIXED" % (BUGZILLA_LINK_BASE, id)
+ url = "%s/bug/%i?api_key=%s" % (BUGZILLA_REST, id, args.apikey)
+ data = {"status": "RESOLVED", "resolution": "FIXED"}
+ result = requests.put(url, json.dumps(data), headers=HEADERS)
+ if result.status_code == 200:
+ data = json.loads(result.text)
+ changes = data["bugs"][0]["changes"]
+ if len(changes) > 0:
+ return True
+ return False
+
+
+def add_comment(comment, id):
+ url = "%s/bug/%i/comment?api_key=%s" % (BUGZILLA_REST, id, args.apikey)
+ data = {"comment": comment}
+ requests.post(url, json.dumps(data), headers=HEADERS)
+
+
+def generate_comment(rev):
+ return "Updated automatically due to git commit\n" \
+ " %s\n" \
+ "Status changed to: RESOLVED_FIXED" % generate_web_url(rev)
+
+
+def get_git_revlist():
+ output = subprocess.check_output(["git", "rev-list",
+ "%s..%s" % (OLDREV, NEWREV)])
+ return output.split()
+
+
+def get_commit_log(rev):
+ log = subprocess.check_output(["git", "log", '--format=%B', "-n", "1",
+ rev.strip()])
+ return log
+
+
+def update_if_bug(log, rev):
+ reg = re.compile(REGEX, re.IGNORECASE | re.MULTILINE | re.DOTALL)
+ result = re.search(reg, log)
+ if result:
+ for bug in result.groups():
+ changed = set_resolved_fixed(int(bug))
+ if changed:
+ add_comment(generate_comment(rev), int(bug))
+
+
+revs = get_git_revlist()
+for rev in revs:
+ commit_log = get_commit_log(rev)
+ update_if_bug(commit_log, rev)