Project

General

Profile

Could I presumptuous consult a "Updating Redmine issue st... ยป patchset_created.bat

Sherry Lin, 2016-11-14 09:06

 
1
rem = """
2
python "%~f0" %*
3
goto exit
4
"""
5

    
6
# -*- coding: utf-8 -*-
7
#
8
#   Copyright (c) 2008-2011 Tobias Hieta <tobias@hieta.se>
9
#   Copyright (c) 2014      Rossen Apostolov <rossen@mochiron.org>
10
#   Copyright (c) 2015      Thomas Loffler <loeffler@spooner-web.de>
11
#
12

    
13
# This is a simple Gerrit hook for updating Redmine with information about what changes
14
# are in Gerrit review.
15
#
16
# It parses the commit messages from Gerrit and looks for a Redmine issue in the style
17
# of "#1028".
18
#
19
# It then adds a informative message to the Redmine issue about the review URL and other
20
# data. It can also change the status of the issue to indicate that this issue is now
21
# under gerrit review.
22
#
23
# Script is tested with Redmine 2.6.1 and Gerrit 2.10.4
24

    
25
# set your API key here. You can find it under "My account" in Redmine, i.e. http://redmine.mydomain.com/my/account
26
REDMINE_API_KEY = "c5820fb1cb201d899c999ba549ac484b6be223fa"
27

    
28
# the hostname or ip number of redmine, do not include the port number here.
29
REDMINE_HOST = "172.16.112.123"
30

    
31
# The port number on which redmine is listening, change this to 443 if the
32
# redmine host is using a secure connection 
33
REDMINE_HOST_PORT_NUMBER = 7777
34

    
35
# Set this to true if the redmine host is using a secure connection (SSL)
36
REDMINE_HOST_USING_SSL = False
37

    
38
# if you want the script to update the status of the issue in redmine
39
# you'll need to set this to the id number of that status. otherwise set
40
# it to None and it won't update the status
41

    
42
# the regex we use for findig the issue id
43
REDMINE_ISSUE_ID_REGEX = '\#(\d+)'
44
print REDMINE_ISSUE_ID_REGEX
45
# which projects to run the script for
46
GERRIT_PROJECTS = ["PROJECT_NAME"]
47
print GERRIT_PROJECTS
48
import optparse
49
from subprocess import check_output # TODO: you'll need python 2.7 for this
50
import sys
51
import re
52
import httplib
53
import json
54

    
55
if __name__ == '__main__':
56
    parser = optparse.OptionParser()
57
    parser.add_option('-c', '--change', dest='changeid')
58
    parser.add_option('-u', '--change-url', dest='changeurl')
59
    parser.add_option('-p', '--project', dest='project')
60
    parser.add_option('-b', '--branch', dest='branch')
61
    parser.add_option('-s', '--uploader', dest='uploader')
62
    parser.add_option('-o', '--commit', dest='commit')
63
    parser.add_option('-a', '--patchset', dest='patchset')
64
    parser.add_option('-d', '--is-draft', dest='isdraft')
65
    parser.add_option('-t', '--topic', dest='topic')
66
    parser.add_option('--kind', dest='kind')
67
    parser.add_option('--change-owner', dest='change-owner')
68

    
69
    (options, x) = parser.parse_args(sys.argv)
70

    
71
    if options.project not in GERRIT_PROJECTS:
72
        print "wrong project %s" % options.project
73
        sys.exit(0)
74

    
75
    commitmsg = check_output(['git','cat-file','-p', options.commit])
76
    if not commitmsg or len(commitmsg) < 10:
77
        print "no commit msg!"
78
        sys.exit(0)
79

    
80
# update status only for the first patchset
81
    if int(options.patchset) != 1:
82
        print "This is not the first patchset (%s) for this issue (%s), will not update the status" % (options.patchset, options.changeurl)
83
        sys.exit(0)
84

    
85
# Don't change the status, only put a note. Otherwise uncomment the lines below.
86
    IN_REVIEW_STATUS = None
87
# for drafts, change the status to "In Progress"...
88
#    if options.isdraft == str("true"):
89
#        IN_REVIEW_STATUS = 2
90
# ... otherwise change to "Fix Uploaded"
91
#    else:
92
#        IN_REVIEW_STATUS = 10
93

    
94
    regex = re.compile(REDMINE_ISSUE_ID_REGEX, re.IGNORECASE)
95
    mgi = regex.finditer(commitmsg)
96
    for mg in mgi:
97
       redmineid = int(mg.group(1))
98
       if not redmineid or redmineid == 0:
99
           print "no issue set here"
100
           sys.exit(0)
101

    
102
       if options.isdraft == str("true"):
103
           redminecomment = "Gerrit received a related DRAFT patchset '%s' for Issue #%d.\n" % (options.patchset, redmineid)
104
       else:
105
           redminecomment = "Gerrit received a related patchset '%s' for Issue #%d.\n" % (options.patchset, redmineid)
106
       redminecomment += "Uploader: %s\n" % options.uploader
107
       redminecomment += "Change-Id: %s\n" % options.changeid
108
       redminecomment += "Gerrit URL: %s\n" % options.changeurl
109

    
110
       jsonstruct = {"issue":{}}
111
       jsonstruct["issue"]["notes"] = redminecomment
112
       if IN_REVIEW_STATUS:
113
           jsonstruct["issue"]["status_id"]=IN_REVIEW_STATUS
114

    
115
       jsondata = json.dumps(jsonstruct)
116

    
117
       puturl = "/redmine/issues/%d.json" % (redmineid)
118

    
119
       if REDMINE_HOST_USING_SSL:
120
           connection = httplib.HTTPSConnection(REDMINE_HOST, REDMINE_HOST_PORT_NUMBER)
121
       else:
122
           connection = httplib.HTTPConnection(REDMINE_HOST, REDMINE_HOST_PORT_NUMBER)
123

    
124
       connection.request('PUT', puturl, jsondata, {"Content-Type":"application/json", "X-Redmine-API-Key":REDMINE_API_KEY})
125
       response = connection.getresponse()
126
    sys.exit(0)
127

    
128

    
129
DosExitLabel = """
130
:exit
131
exit
132
rem """
    (1-1/1)