Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 22 additions & 27 deletions patchset-created
100644 → 100755
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/python2.7
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# Copyright (c) 2008-2011 Tobias Hieta <tobias@hieta.se>
Expand All @@ -15,7 +15,7 @@
# 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
# Script is tested with Redmine 3.4 stable and Gerrit 2.15.2

# 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"
Expand All @@ -41,64 +41,59 @@ REDMINE_ISSUE_ID_REGEX = '\#(\d+)'
GERRIT_PROJECTS = ["PROJECT_NAME"]

import optparse
from subprocess import check_output # TODO: you'll need python 2.7 for this
from subprocess import check_output
import sys
import re
import httplib
import http.client
import json

if __name__ == '__main__':
parser = optparse.OptionParser()
parser.add_option('-c', '--change', dest='changeid')
parser.add_option('-k', '--kind', dest='kind')
parser.add_option('-u', '--change-url', dest='changeurl')
parser.add_option('-w', '--change-owner', dest='changeowner')
parser.add_option('-n', '--change-owner-username', dest='changeuser')
parser.add_option('-p', '--project', dest='project')
parser.add_option('-b', '--branch', dest='branch')
parser.add_option('-s', '--uploader', dest='uploader')
parser.add_option('-l', '--uploader-username', dest='uploaderuser')
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
print("wrong project " + 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!"
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)
print("This is not the first patchset ({}) for this issue ({}), will not update the status".format(options.patchset, options.changeurl))
sys.exit(0)

# Don't change the status, only put a note. Otherwise uncomment the lines below.
# Don't change the status, only put a note. Otherwise uncomment the line 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
# IN_REVIEW_STATUS = 10

regex = re.compile(REDMINE_ISSUE_ID_REGEX, re.IGNORECASE)
mgi = regex.finditer(commitmsg)
mgi = regex.finditer(commitmsg.decode("utf-8"))
for mg in mgi:
redmineid = int(mg.group(1))
if not redmineid or redmineid == 0:
print "no issue set here"
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
redminecomment = "Gerrit received a related patchset "
redminecomment += "'{}' for Issue #{}.\n".format(options.patchset, redmineid)
redminecomment += "Uploader: {}\n".format(options.uploader)
redminecomment += "Change-Id: {}\n".format(options.changeid)
redminecomment += "Gerrit URL: {}\n".format(options.changeurl)

jsonstruct = {"issue":{}}
jsonstruct["issue"]["notes"] = redminecomment
Expand All @@ -110,9 +105,9 @@ if __name__ == '__main__':
puturl = "/issues/%d.json" % (redmineid)

if REDMINE_HOST_USING_SSL:
connection = httplib.HTTPSConnection(REDMINE_HOST, REDMINE_HOST_PORT_NUMBER)
connection = http.client.HTTPSConnection(REDMINE_HOST, REDMINE_HOST_PORT_NUMBER)
else:
connection = httplib.HTTPConnection(REDMINE_HOST, REDMINE_HOST_PORT_NUMBER)
connection = http.client.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()
Expand Down