#!/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. # ---------------------------------------------------------------------------- # # Modified by Martin Tomes. # 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" # LOG=`/usr/bin/svnlook log -r $REV $REPOS` # AUTHOR=`/usr/bin/svnlook author -r $REV $REPOS` # TRAC_ENV='/somewhere/trac/' - leave off the project, that is derived from the ticket reference. # # /usr/bin/python /usr/local/src/trac/contrib/trac-post-commit-hook \ # -p "$TRAC_ENV" \ # -r "$REV" \ # -u "$AUTHOR" \ # -m "$LOG" # # This searches for ticket references of the form instname:#number and adds # a comment to those tickets referencing the commit. import re import os import sys import time 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.web.href import Href try: from optparse import OptionParser except ImportError: try: from optik import OptionParser except ImportError: raise ImportError, 'Requires Python 2.3 or the Optik option parsing library.' parser = OptionParser() 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('-u', '--user', dest='user', help='The user who is responsible for this action') parser.add_option('-m', '--msg', dest='msg', help='The log message to search.') parser.add_option('-c', '--encoding', dest='encoding', help='The encoding used by the log message.') parser.add_option('-d', '--dirs', dest='dirlist', help='Directories changed.') (options, args) = parser.parse_args(sys.argv[1:]) ticketPattern = re.compile(r'(?P[A-Za-z0-9]+):#(?P[0-9]*)') class CommitHook: _instance_names = ('product', 'boardsupport', 'functionblocks') def __init__(self, project=options.project, author=options.user, rev=options.rev, msg=options.msg, encoding=options.encoding): msg = to_unicode(msg, encoding) self.author = author self.rev = rev self.now = int(time.time()) # Work out which bits of the repository are being committed to. sections = {} for s in options.dirlist.splitlines(): section = s.split('/')[0] if section.lower() in self._instance_names: sections[section.lower()] = 1 # sections.keys now contains all of the repository subtrees which # have Tracs under attack. ticketGroups = ticketPattern.findall(msg) for inst, tkt_id in ticketGroups: if inst in CommitHook._instance_names: # Now work out the Trac instance. The project parameter # points to the level above the Trac instances. self.env = open_environment(project + '/' + inst) url = self.env.config.get('project', 'url') self.env.href = Href(url) self.env.abs_href = Href(url) # Need to make changset links to the bits of the repository # which are being changed. self.msg = "Changesets:" for section in sections.keys(): self.msg = "%s %s:r%s" % (self.msg, section, rev) self.msg = "%s\n\n%s" % (self.msg, msg) try: db = self.env.get_db_cnx() ticket = Ticket(self.env, int(tkt_id), db) # determine sequence number... cnum = 0 tm = TicketModule(self.env) for change in tm.grouped_changelog_entries(ticket, db): if change['permanent']: cnum += 1 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) else: print>>sys.stderr, 'Invalid Trac instance name %s' % (inst) if __name__ == "__main__": if len(sys.argv) < 5: print "For usage: %s --help" % (sys.argv[0]) else: CommitHook()