-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchange-merged
executable file
·86 lines (71 loc) · 2.61 KB
/
change-merged
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
#!/usr/bin/python
#
# This is a simple Gerrit hook for updating Trac with information about what
# changes are in Gerrit review.
#
# It parses the commit messages from Gerrit and looks for a Trac ticket in the
# style of "#1028".
#
# It then adds a informative message to the Trac ticket about the review URL
# and other data. It can also change the status of the issue to indicate that
# this issue is now under gerrit review.
#
# Script is tested with Trac 0.1.2 and Gerrit 2.6.rc0
# set the auth url
TRAC_URL = "http://gerrit:123456@127.0.0.1/projects/trac/login/rpc"
# the regex we use for findig the issue id
TRAC_ISSUE_ID_REGEX = '\#(\d+)'
# which projects to run the script for
GERRIT_PROJECTS = ["test", ]
import optparse
import sys
import re
import xmlrpclib
python27 = True
try:
from subprocess import check_output
except Exception as e:
python27 = False
import subprocess
if __name__ == '__main__':
parser = optparse.OptionParser()
parser.add_option('-c', '--change', dest='changeid')
parser.add_option('-u', '--change-url', dest='changeurl')
parser.add_option('-p', '--project', dest='project')
parser.add_option('-b', '--branch', dest='branch')
parser.add_option('-s', '--submitter', dest='submitter')
parser.add_option('-o', '--commit', dest='commit')
parser.add_option('-t', '--topic', dest='topic')
(options, x) = parser.parse_args(sys.argv)
if options.project not in GERRIT_PROJECTS:
print "wrong project %s" % options.project
sys.exit(0)
if python27:
commitmsg = check_output(['/usr/local/bin/git','cat-file','-p', options.commit])
else:
commitmsg = subprocess.Popen(['/usr/local/bin/git','cat-file','-p', options.commit],
stdout=subprocess.PIPE).communicate()[0]
if not commitmsg or len(commitmsg) < 10:
print "no commit msg!"
sys.exit(0)
regex = re.compile(TRAC_ISSUE_ID_REGEX, re.IGNORECASE)
mg = regex.search(commitmsg)
if not mg:
print "no ticket set here"
sys.exit(0)
tracid = int(mg.group(1))
if not tracid or tracid == 0:
print "no ticket set here"
sys.exit(0)
traccomment = "Reviewed: %s\n" % options.changeurl
submitter = options.submitter
strings = options.submitter.split(' ')
if len(strings) > 1:
submitter = strings[0]
traccomment += "Submitter: %s\n" % submitter
traccomment += "Branch: %s\n\n" % options.branch
traccomment += commitmsg
server = xmlrpclib.ServerProxy(TRAC_URL)
ticket = {'status': 'closed'}
server.ticket.update(tracid, traccomment, ticket)
sys.exit(0)