forked from adamlaska/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrac-post-commit-hook
More file actions
executable file
·241 lines (209 loc) · 9.41 KB
/
trac-post-commit-hook
File metadata and controls
executable file
·241 lines (209 loc) · 9.41 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#!/usr/bin/env python
# trac-post-commit-hook
# ----------------------------------------------------------------------------
# Copyright (c) 2004 Stephen Hansen
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to
# deal in the Software without restriction, including without limitation the
# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
# sell copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
# IN THE SOFTWARE.
# ----------------------------------------------------------------------------
# This Subversion post-commit hook script is meant to interface to the
# Trac (http://www.edgewall.com/products/trac/) issue tracking/wiki/etc
# system.
#
# It should be called from the 'post-commit' script in Subversion, such as
# via:
#
# REPOS="$1"
# REV="$2"
# TRAC_ENV="/path/to/tracenv"
#
# /usr/bin/python /usr/local/src/trac/contrib/trac-post-commit-hook \
# -p "$TRAC_ENV" -r "$REV"
#
# (all the other arguments are now deprecated and not needed anymore)
#
# It searches commit messages for text in the form of:
# command #1
# command #1, #2
# command #1 & #2
# command #1 and #2
#
# Instead of the short-hand syntax "#1", "ticket:1" can be used as well, e.g.:
# command ticket:1
# command ticket:1, ticket:2
# command ticket:1 & ticket:2
# command ticket:1 and ticket:2
#
# In addition, the ':' character can be omitted and issue or bug can be used
# instead of ticket.
#
# You can have more then one command in a message. The following commands
# are supported. There is more then one spelling for each command, to make
# this as user-friendly as possible.
#
# close, closed, closes, fix, fixed, fixes
# The specified issue numbers are closed with the contents of this
# commit message being added to it.
# references, refs, addresses, re, see
# The specified issue numbers are left in their current status, but
# the contents of this commit message are added to their notes.
#
# A fairly complicated example of what you can do is with a commit message
# of:
#
# Changed blah and foo to do this or that. Fixes #10 and #12, and refs #12.
#
# This will close #10 and #12, and add a note to #12.
import re
import os
import sys
from datetime import datetime
from optparse import OptionParser, OptionGroup
import syslog
syslog.openlog("post-commit-trac", syslog.LOG_PID, syslog.LOG_LOCAL0)
log = syslog.syslog
parser = OptionParser()
depr = '(not used anymore)'
parser.add_option('-e', '--require-envelope', dest='envelope', default='',
help="""
Require commands to be enclosed in an envelope.
If -e[], then commands must be in the form of [closes #4].
Must be two characters.""")
parser.add_option('-p', '--project', dest='project',
help='Path to the Trac project.')
parser.add_option('-r', '--revision', dest='rev',
help='Repository revision number.')
parser.add_option('-a', '--action', dest='action', default=None, help='The action to take on a ticket (default: none)')
group = OptionGroup(parser, "DEPRECATED OPTIONS")
group.add_option('-u', '--user', dest='user',
help='The user who is responsible for this action '+depr)
group.add_option('-m', '--msg', dest='msg',
help='The log message to search '+depr)
group.add_option('-c', '--encoding', dest='encoding',
help='The encoding used by the log message '+depr)
group.add_option('-s', '--siteurl', dest='url',
help=depr+' the base_url from trac.ini will always be used.')
parser.add_option_group(group)
(options, args) = parser.parse_args(sys.argv[1:])
if options.project and not 'PYTHON_EGG_CACHE' in os.environ:
os.environ['PYTHON_EGG_CACHE'] = os.path.join(options.project, '.egg-cache')
from trac.env import open_environment
from trac.ticket.notification import TicketNotifyEmail
from trac.ticket import Ticket
from trac.ticket.web_ui import TicketModule
# TODO: move grouped_changelog_entries to model.py
from trac.util.text import to_unicode
from trac.util.datefmt import utc
from trac.versioncontrol.api import NoSuchChangeset
ticket_prefix = '(?:#|(?:ticket|issue|bug)s?[: ]?#?)'
ticket_reference = ticket_prefix + '[0-9]+'
action_pattern = "(?:(?i)(?:fix(?: for)?|fixes|close|closes|addresses|references|refs|re|relates to|related to|see|described in))"
ticket_command = (r'(?P<action>%s).?(?P<ticket>%s(?:(?:[, &]*|[ ]?and[ ]?)%s)*)' % (action_pattern, ticket_reference, ticket_reference))
if options.envelope:
ticket_command = r'\%s%s\%s' % (options.envelope[0], ticket_command,
options.envelope[1])
command_re = re.compile(ticket_command)
ticket_re = re.compile(ticket_prefix + '([0-9]+)')
class CommitHook:
# _supported_cmds = {'close': '_cmdClose',
# 'closed': '_cmdClose',
# 'closes': '_cmdClose',
# 'fix': '_cmdClose',
# 'fixed': '_cmdClose',
# 'fixes': '_cmdClose',
# 'addresses': '_cmdRefs',
# 're': '_cmdRefs',
# 'references': '_cmdRefs',
# 'refs': '_cmdRefs',
# 'see': '_cmdRefs'}
_supported_cmds = {'fix': '_cmdClose',
'fix for': '_cmdClose',
'fixes': '_cmdClose',
'close': '_cmdClose',
'closes': '_cmdClose',
'addresses': '_cmdRefs',
'described in': '_cmdRefs',
'references': '_cmdRefs',
'refs': '_cmdRefs',
're': '_cmdRefs',
'relates to': '_cmdRefs',
'related to': '_cmdRefs',
'see': '_cmdRefs',
}
def __init__(self, project=options.project, author=options.user,
rev=options.rev, url=options.url):
self.env = open_environment(project)
repos = self.env.get_repository()
repos.sync()
# Instead of bothering with the encoding, we'll use unicode data
# as provided by the Trac versioncontrol API (#1310).
try:
chgset = repos.get_changeset(rev)
except NoSuchChangeset:
return # out of scope changesets are not cached
self.author = chgset.author
self.rev = rev
self.msg = "From [%s]:\n\n%s" % (rev, chgset.message)
self.now = datetime.now(utc)
cmd_groups = command_re.findall(self.msg)
tickets = {}
for cmd, tkts in cmd_groups:
funcname = CommitHook._supported_cmds.get(cmd.lower(), '')
if funcname:
for tkt_id in ticket_re.findall(tkts):
func = getattr(self, funcname)
tickets.setdefault(tkt_id, []).append(func)
for tkt_id, cmds in tickets.iteritems():
try:
fixes = False
db = self.env.get_db_cnx()
ticket = Ticket(self.env, int(tkt_id), db)
for cmd in cmds:
if cmd == self._cmdClose:
fixes = True
cmd(ticket)
# determine sequence number...
cnum = 0
tm = TicketModule(self.env)
for change in tm.grouped_changelog_entries(ticket, db):
if change['permanent']:
cnum += 1
if fixes:
self.msg = "Fixed in [%s]:\n\n%s" % (rev, chgset.message)
log("Updating ticket #%s: %s" % (tkt_id, chgset.message))
ticket.save_changes(self.author, self.msg, self.now, db, cnum+1)
db.commit()
tn = TicketNotifyEmail(self.env)
tn.notify(ticket, newticket=0, modtime=self.now)
except Exception, e:
# import traceback
# traceback.print_exc(file=sys.stderr)
print>>sys.stderr, 'Unexpected error while processing ticket ' \
'ID %s: %s' % (tkt_id, e)
def _cmdClose(self, ticket):
ticket['status'] = 'closed'
ticket['resolution'] = 'fixed'
def _cmdRefs(self, ticket):
pass
if __name__ == "__main__":
if len(sys.argv) < 5:
print "For usage: %s --help" % (sys.argv[0])
print
print "Note that the deprecated options will be removed in Trac 0.12."
else:
CommitHook()