madssj / mercurial-trac-hook
A trac commit hook for mercurial.
Clone this repository (size: 20.1 KB): HTTPS / SSH
$ hg clone http://bitbucket.org/madssj/mercurial-trac-hook/
| commit 10: | 979dba11dfe6 |
| parent 9: | a6edbc058422 |
| parent 7: | 619a952eaf61 |
| branch: | default |
| tags: | tip |
merged
mercurial-trac-hook /
trachook.py
| r10:979dba11dfe6 | 194 loc | 7.1 KB | embed / history / annotate / raw / |
|---|
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 | #!/usr/bin/env python
# trac-post-commit-hook
# ----------------------------------------------------------------------------
# Copyright (c) 2004 Stephen Hansen, Mads Sulau Joergensen
#
# 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.
# ----------------------------------------------------------------------------
from mercurial import demandimport
demandimport.disable()
import re
import os
import sys
from datetime import datetime
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
from mercurial.i18n import _
from mercurial.node import short
from mercurial import cmdutil, templater, util
_supported_cmds = {'close': '_cmdClose',
'closed': '_cmdClose',
'closes': '_cmdClose',
'fix': '_cmdClose',
'fixed': '_cmdClose',
'fixes': '_cmdClose',
'addresses': '_cmdRefs',
're': '_cmdRefs',
'references': '_cmdRefs',
'refs': '_cmdRefs',
'see': '_cmdRefs'}
ticket_prefix = '(?:#|(?:ticket|issue|bug)[: ]?)'
time_pattern = r'[ ]?(?:\((?:(?:spent|sp)[ ]?)?(-?[0-9]*(?:\.[0-9]+)?)\))?'
ticket_reference = ticket_prefix + '[0-9]+'+time_pattern
support_cmds_pattern = '|'.join(_supported_cmds.keys())
ticket_command = (r'(?P<action>(?:%s))[ ]*'
'(?P<ticket>%s(?:(?:[, &]*|[ ]?and[ ]?)%s)*)' %
(support_cmds_pattern,ticket_reference, ticket_reference))
# command_re = re.compile(ticket_command)
# ticket_re = re.compile(ticket_prefix + '([0-9]+)')
command_re = re.compile(ticket_command)
ticket_re = re.compile(ticket_prefix + '([0-9]+)'+time_pattern)
class CommitHook:
def init_env(self, project):
self.env = open_environment(project)
def __init__(self, project):
self.init_env(project)
self.repos = self.env.get_repository()
self.repos.sync()
def update(self, ctx, rev, url=None):
# Instead of bothering with the encoding, we'll use unicode data
# as provided by the Trac versioncontrol API (#1310).
try:
chgset = self.repos.get_changeset(rev)
self.msg = "(In [%s]) %s" % (rev, chgset.message)
self.author = chgset.author
except NoSuchChangeset:
self.msg = "(In [%s]) %s" % (rev, ctx.description())
self.author = ctx.user()
self.rev = rev
self.now = datetime.now(utc)
cmd_groups = command_re.findall(self.msg.lower())
tickets = {}
#print 'Command groups: %s' % cmd_groups
for cmd, tkts, xxx1, xxx2 in cmd_groups:
funcname = _supported_cmds.get(cmd.lower(), '')
if funcname:
for tkt_id, spent in ticket_re.findall(tkts):
func = getattr(self, funcname)
lst = tickets.setdefault(tkt_id, [])
lst.append([func, spent])
#print "Tickets: %s" % tickets
for tkt_id, vals in tickets.iteritems():
spent_total = 0.0
try:
db = self.env.get_db_cnx()
ticket = Ticket(self.env, int(tkt_id), db)
for (cmd, spent) in vals:
cmd(ticket)
if spent:
spent_total += float(spent)
hours_txt = ''
if spent_total != 0.0:
hours_txt = 'adding %.2f hour(s) spent' % spent_total
print ' %s ticket %s %s' % (self.action, str(tkt_id), hours_txt)
# determine sequence number...
cnum = 0
tm = TicketModule(self.env)
for change in tm.grouped_changelog_entries(ticket, db):
if change['permanent']:
cnum += 1
if spent_total:
self._setTimeTrackerFields(ticket, spent_total)
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)
continue
def _cmdClose(self, ticket):
ticket['status'] = 'closed'
ticket['resolution'] = 'fixed'
self.action = 'closing'
def _cmdRefs(self, ticket):
self.action = 'commenting'
def _setTimeTrackerFields(self, ticket, spent):
if (spent != ''):
spentTime = float(spent)
if (ticket.values.has_key('hours')):
ticket['hours'] = str(spentTime)
def hook(ui, repo, hooktype, node=None, **kwargs):
"""
Mercurial trac commit hook.
"""
if node is None:
raise util.Abort(_('hook type %s does not pass a changeset id') % hooktype)
project = ui.config('trac-hook', 'root', None)
url = ui.config('trac-hook', 'url', None)
if project is None:
raise util.Abort(_('you need to configure the trac-hook in your hgrc - root missing'))
ctx = repo.changectx(node)
rev = ctx.rev()
try:
# try mercurial 1.1+ API
until = len(repo.changelog)
except TypeError:
# fallback to previous
until = repo.changelog.count()
trac_hook = CommitHook(project)
for r in set(range(rev, until)):
r = short(repo.lookup(r))
c = repo.changectx(r)
# print 'running trac-hook for change %s:%s' % (str(c.rev()), r)
trac_hook.update(c, r, url)
|
