aboutsummaryrefslogtreecommitdiff
path: root/bugzilla-post-receive.py
blob: a9413c3dd17047a3d143902ad1830c37d74788ae (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
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/repositories"


def generate_web_url(rev):
    pwd = os.getcwd()
    if re.match(GIT_DIR_BASE, pwd):
        dir = pwd.split(GIT_DIR_BASE)[1]
    else:
        dir = pwd
    return "%s%s/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.content)
        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))

if OLDREV != 0000000000000000000000000000000000000000:
    revs = get_git_revlist()
    for rev in revs:
        commit_log = get_commit_log(rev)
        update_if_bug(commit_log, rev)