Maxim Uvarov | 1acb99b | 2017-05-08 23:40:58 +0300 | [diff] [blame^] | 1 | #!/usr/bin/python |
| 2 | |
| 3 | # bugzilla github push web hook |
| 4 | # |
| 5 | # Scripts updates bugzilla bug with merged commit message |
| 6 | |
| 7 | from __future__ import print_function |
| 8 | import pprint |
| 9 | import bugzilla |
| 10 | |
| 11 | |
| 12 | import cgi |
| 13 | import pickle |
| 14 | import sys |
| 15 | import time |
| 16 | import json |
| 17 | from StringIO import StringIO |
| 18 | import sys, urllib |
| 19 | from cgi import parse_qs, escape |
| 20 | import re |
| 21 | |
| 22 | qin = sys.stdin.read() |
| 23 | f = open('python_%s.dump' % time.time(), 'w') |
| 24 | pickle.dump(qin, f) |
| 25 | f.close() |
| 26 | |
| 27 | #fname = "python_1492447091.72.dump" |
| 28 | #qin = pickle.load( open(fname, "rb" ) ) |
| 29 | |
| 30 | def msg_has_bug(msg): |
| 31 | buglist = set() |
| 32 | print("%s\n" % msg) |
| 33 | for m in re.finditer('https://bugs\.linaro\.org/show_bug\.cgi\?id=([0-9]+)', msg): |
| 34 | buglist.add(m.group(1)) |
| 35 | |
| 36 | for m in re.finditer(r'[bB]ug #([0-9]+)', msg): |
| 37 | buglist.add(m.group(1)) |
| 38 | |
| 39 | for m in re.finditer(r'[bB]ug ([0-9]+)', msg): |
| 40 | buglist.add(m.group(1)) |
| 41 | |
| 42 | for m in re.finditer(r'[bB]ug: ([0-9]+)', msg): |
| 43 | buglist.add(m.group(1)) |
| 44 | |
| 45 | for m in re.finditer(r'[Ff]ixes: ([0-9]+)', msg): |
| 46 | buglist.add(m.group(1)) |
| 47 | |
| 48 | print("%s\n" % str(buglist)) |
| 49 | return buglist |
| 50 | |
| 51 | URL = "https://bugs.linaro.org" |
| 52 | |
| 53 | bzapi = bugzilla.Bugzilla(URL) |
| 54 | if not bzapi.logged_in: |
| 55 | bzapi.login("login@linaro.org", "password") |
| 56 | |
| 57 | print("Content-type: text/html\n") |
| 58 | print("""<!DOCTYPE HTML> |
| 59 | <html> |
| 60 | <head> |
| 61 | <meta charset="utf-8"> |
| 62 | <title>some title</title> |
| 63 | </head> |
| 64 | <body>""") |
| 65 | |
| 66 | io = StringIO(qin) |
| 67 | js = json.load(io) |
| 68 | |
| 69 | for c in js["commits"]: |
| 70 | bugset = msg_has_bug(c["message"]) |
| 71 | for bugnum in bugset: |
| 72 | bug = bzapi.getbug(bugnum) |
| 73 | bug_msg = "%s\n%s\n%s %s\n%s\n" % (c["url"], |
| 74 | c["timestamp"], |
| 75 | c["author"]["name"], c["author"]["email"], |
| 76 | c["message"]) |
| 77 | |
| 78 | update = bzapi.build_update(comment=bug_msg) |
| 79 | bzapi.update_bugs([bug.id], update) |
| 80 | print("Posted message to bug %s" % bugnum) |
| 81 | |
| 82 | print("<h1>all ok!</h1>") |
| 83 | print("</body></html>") |