#!/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" # TXN="$2" # TRAC_ENV='/somewhere/trac/project/' # TRAC_URL='http://trac.mysite.com/project/' # LOG=`/usr/bin/svnlook log -r $REV $REPOS` # /usr/bin/python /usr/local/src/trac/contrib/trac-post-commit-hook \ # -p "$TRAC_ENV" \ # -m "$LOG" \ # -s "$TRAC_URL" # # It searches commit messages for text in the form of: # command #1 # command #1, #2 # command #1 & #2 # command #1 and #2 # # 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. # # closes, fixes # The specified issue numbers are closed with the contents of this # commit message being added to it. # references, refs, addresses, re # 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 import time from trac.env import open_environment from trac.Notify import TicketNotifyEmail from trac.ticket import Ticket 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('-e', '--require-envelope', dest='env', 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('-m', '--msg', dest='msg', help='The log message to search.') parser.add_option('-s', '--siteurl', dest='url', help='The base URL to the project\'s trac website (to which ' '/ticket/## is appended). If this is not specified, ' 'the project URL from trac.ini will be used.') (options, args) = parser.parse_args(sys.argv[1:]) if options.env: leftEnv = '\\' + options.env[0] rghtEnv = '\\' + options.env[1] else: leftEnv = '' rghtEnv = '' commandPattern = re.compile(leftEnv + r'(?P[A-Za-z]*).?(?P#[0-9]+(?:(?:[, &]*|[ ]?and[ ]?)#[0-9]+)*)' + rghtEnv) ticketPattern = re.compile(r'#([0-9]*)') class CommitHook: _supported_cmds = {'addresses': '_cmdRefs', 're': '_cmdRefs', 'references': '_cmdRefs', 'refs': '_cmdRefs', 'see': '_cmdRefs'} def __init__(self, project=options.project, msg=options.msg, url=options.url): self.now = int(time.time()) self.env = open_environment(project) if url is None: url = self.env.config.get('project', 'url') self.env.href = Href(url) self.env.abs_href = Href(url) cmdGroups = commandPattern.findall(msg) good = False for cmd, tkts in cmdGroups: good = True if CommitHook._supported_cmds.has_key(cmd.lower()): func = getattr(self, CommitHook._supported_cmds[cmd.lower()]) func(ticketPattern.findall(tkts)) if(good == False): print>>sys.stderr, 'no ticket referenced' sys.exit(1) def _cmdRefs(self, tickets): for tkt_id in tickets: try: project=options.project self.env = open_environment(project) # TODO: check each ticket to make sure it is in the doing state otherwise report the error and exit 1 ticket = Ticket(self.env, tkt_id) if(ticket['ticketaction'] != 'Doing'): sys.exit(1) except Exception, e: print>>sys.stderr, 'invalid ticket state for ticket #%s' % (tkt_id) sys.exit(1) if __name__ == "__main__": if len(sys.argv) < 5: print "For usage: %s --help" % (sys.argv[0]) else: CommitHook()