-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathpatchset-created
119 lines (97 loc) · 4.46 KB
/
patchset-created
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#!/usr/bin/python2.7
# -*- coding: utf-8 -*-
#
# Copyright (c) 2008-2011 Tobias Hieta <[email protected]>
# Copyright (c) 2014 Rossen Apostolov <[email protected]>
#
# This is a simple Gerrit hook for updating Redmine with information about what changes
# are in Gerrit review.
#
# It parses the commit messages from Gerrit and looks for a Redmine issue in the style
# of "#1028".
#
# It then adds a informative message to the Redmine issue 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 Redmine 2.4.1 and Gerrit 2.6.1
# set your API key here. You can find it under "My account" in Redmine, i.e. http://redmine.mydomain.com/my/account
REDMINE_API_KEY = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
# the hostname or ip number of redmine, do not include the port number here.
REDMINE_HOST = "redmine.mydomain.com"
# The port number on which redmine is listening, change this to 443 if the
# redmine host is using a secure connection
REDMINE_HOST_PORT_NUMBER = 80
# Set this to true if the redmine host is using a secure connection (SSL)
REDMINE_HOST_USING_SSL = False
# if you want the script to update the status of the issue in redmine
# you'll need to set this to the id number of that status. otherwise set
# it to None and it won't update the status
# the regex we use for findig the issue id
REDMINE_ISSUE_ID_REGEX = '\#(\d+)'
# which projects to run the script for
GERRIT_PROJECTS = ["PROJECT_NAME"]
import optparse
from subprocess import check_output # TODO: you'll need python 2.7 for this
import sys
import re
import httplib
import json
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', '--uploader', dest='uploader')
parser.add_option('-o', '--commit', dest='commit')
parser.add_option('-a', '--patchset', dest='patchset')
parser.add_option('-d', '--is-draft', dest='isdraft')
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)
commitmsg = check_output(['git','cat-file','-p', options.commit])
if not commitmsg or len(commitmsg) < 10:
print "no commit msg!"
sys.exit(0)
# update status only for the first patchset
if int(options.patchset) != 1:
print "This is not the first patchset (%s) for this issue (%s), will not update the status" % (options.patchset, options.changeurl)
sys.exit(0)
# Don't change the status, only put a note. Otherwise uncomment the lines below.
IN_REVIEW_STATUS = None
# for drafts, change the status to "In Progress"...
# if options.isdraft == str("true"):
# IN_REVIEW_STATUS = 2
# ... otherwise change to "Fix Uploaded"
# else:
# IN_REVIEW_STATUS = 10
regex = re.compile(REDMINE_ISSUE_ID_REGEX, re.IGNORECASE)
mgi = regex.finditer(commitmsg)
for mg in mgi:
redmineid = int(mg.group(1))
if not redmineid or redmineid == 0:
print "no issue set here"
sys.exit(0)
if options.isdraft == str("true"):
redminecomment = "Gerrit received a related DRAFT patchset '%s' for Issue #%d.\n" % (options.patchset, redmineid)
else:
redminecomment = "Gerrit received a related patchset '%s' for Issue #%d.\n" % (options.patchset, redmineid)
redminecomment += "Uploader: %s\n" % options.uploader
redminecomment += "Change-Id: %s\n" % options.changeid
redminecomment += "Gerrit URL: %s\n" % options.changeurl
jsonstruct = {"issue":{}}
jsonstruct["issue"]["notes"] = redminecomment
if IN_REVIEW_STATUS:
jsonstruct["issue"]["status_id"]=IN_REVIEW_STATUS
jsondata = json.dumps(jsonstruct)
puturl = "/issues/%d.json" % (redmineid)
if REDMINE_HOST_USING_SSL:
connection = httplib.HTTPSConnection(REDMINE_HOST, REDMINE_HOST_PORT_NUMBER)
else:
connection = httplib.HTTPConnection(REDMINE_HOST, REDMINE_HOST_PORT_NUMBER)
connection.request('PUT', puturl, jsondata, {"Content-Type":"application/json", "X-Redmine-API-Key":REDMINE_API_KEY})
response = connection.getresponse()
sys.exit(0)