> > I'll post code later when I have it generalized and cleaned up, seeking > ideas now from the people more aware of what Trac's become since I first > threw together that hook. :) >
Doing so was easier then I thought. Attached is a workflow-enabled version of the hook. --Stephen --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Trac Users" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/trac-users?hl=en -~----------~----~----~----~------~----~------~--~---
#!/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" # # /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. # # The hook is configured in the environment's trac.ini's svn-post-commit-hook # section. # # You specify commands in terms of groups. An example set of commands would # be: # # [svn-post-commit-hook] # command_groups = close, refs # close_commands = close, closed, closes, fix, fixes, fixed # refs_commands = references, refs, addresses, re, see # # For the purpose of the above settings, 'close' and 'closes' and 'fixes' # are all considered identical. Many options are provided to make it as # user-friendly as possible to use. # # As long as at least one command is included in the commit message, the # entire contents of the message will be added to the specified ticket. # # You can have more then one command in a message. # # In addition to this, you may specify a search list of workflow actions # that should be performed if available on the ticket. The first matching # action found will be executed; if none of the actions are found in the # ticket, the hook will simply give up quietly. Its not considered an # error. # # Actions are specified as a list named <group>_actions. So the actions # that you want to be executed for commands in close_commands you would # specify in close_actions. This is optional; if there are no actions # then the command will cause the commit message to be added, and all is # fine. # # # A more complete example that can be used in the basic workflow would be: # # [svn-post-commit-hook] # command_groups = close, refs # close_commands = close, closed, closes, fix, fixes, fixed # refs_commands = references, refs, addresses, re, see # close_actions = resolve # # TODO: How to handle the action 'operations', and in particular from # above set_resolution? Perhaps allow "close_actions = resolve=fixed" # I don't know the API to pass such into the workflow API. Must dig. # # A fairly complicated example of what you can do is in an enterprise or # more complicated workflow would be: # # [svn-post-commit-hook] # command_groups = close, answer, refs # close_commands = close, closed, closes, fix, fixes, fixed # refs_commands = references, refs, addresses, re, see # answer_commands = answer, answers # answer_actions = provideinfo, provideinfo_new # close_actions = test, resolve # # In the above example, we assume that there are two statuses, "infoneeded" # and "infoneeded_new". The former has an action called "provideinfo" that # will send the ticket back to Assigned; the latter has an action called # "provideinfo_new" that will set the status back to New. # # If a commit message has "answers #4", the hook will see if ticket #4 has # a provideinfo action; if so it performs it and the ticket becomes Assigned. # If it has provideinfo_new instead, the ticket becomes New. Otherwise the # ticket is left alone as nothing was specified that is understood in terms # of the workflow. # # The close action will first try to see if the ticket is in a position to # be sent to QA via the test action; failing that it'll see if it can be # resolved. # # As with the previous examples, the reference commands do nothing special. # # Given the above, the following more complicated message shows how the # hook will try to easily parse through what you intend: # # Changed blah and foo to do this or that. Fixes #10 and #12, and refs #12. # # This will send #10 and #12 to QA, and add a note to #12. 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, TicketSystem 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 trac.perm import PermissionCache from optparse import OptionParser 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('-u', '--user', dest='user', help='The user who is responsible for this action '+depr) parser.add_option('-m', '--msg', dest='msg', help='The log message to search '+depr) parser.add_option('-c', '--encoding', dest='encoding', help='The encoding used by the log message '+depr) parser.add_option('-s', '--siteurl', dest='url', help=depr+' the base_url from trac.ini will always be used.') (options, args) = parser.parse_args(sys.argv[1:]) ticket_prefix = '(?:#|(?:ticket|issue|bug)[: ]?)' ticket_reference = ticket_prefix + '[0-9]+' ticket_command = (r'(?P<action>[A-Za-z]*).?' '(?P<ticket>%s(?:(?:[, &]*|[ ]?and[ ]?)%s)*)' % (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]+)') # TODO: This is ugly but the workflow API expects a Request object with a PermissionCache # on it to check for the allowability of actions, and I can't see a better way to get # about it. class DummyRequest: def __init__(self, env, username): self.perm = PermissionCache(env, username) # close_commands = close, closed, closes, fix, fixes, fixed # refs_commands = references, refs, addresses, re, see class CommitHook: # The defaults are suitable to mimic previous behavior in an environment that is # using the 'basic' workflow. default_supported_commands = { "close": "close", "closes": "close", "closed": "close", "fix": "close", "fixes": "close", "fixed": "close", "references": "refs", "refs": "refs", "addresses": "refs", "re": "refs", "see": "refs" } default_command_actions = { "close": ["resolve"], "refs": [] } 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() supported_commands = {} command_actions = {} for group in self.env.config.get('svn-post-commit-hook', 'command_groups').split(','): group = group.strip() if not group: continue for command in self.env.config.get('svn-post-commit-hook', '%s_commands' % group).split(','): supported_commands[command.strip()] = group command_actions[group] = self.env.config.get('svn-post-commit-hook', '%s_actions' % group, '').split(',') supported_commands = supported_commands or self.default_supported_commands command_actions = command_actions or self.default_command_actions # 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 = "(In [%s]) %s" % (rev, chgset.message) self.now = datetime.now(utc) cmd_groups = command_re.findall(self.msg) tickets = {} for cmd, tkts in cmd_groups: command_group = supported_commands.get(cmd.lower(), '') if command_group: for tkt_id in ticket_re.findall(tkts): tickets.setdefault(tkt_id, []).append(command_group) req = DummyRequest(self.env, self.author) for tkt_id, cmds in tickets.iteritems(): try: db = self.env.get_db_cnx() ticket = Ticket(self.env, int(tkt_id), db) # If multiple commands are given for a particular ticket, then the # first one that has actions set are the actions that will be # executed. desiredActions = [] for cmd in cmds: desiredActions = desiredActions or command_actions.get(cmd, None) # determine sequence number... cnum = 0 tm = TicketModule(self.env) for change in tm.grouped_changelog_entries(ticket, db): if change['permanent']: cnum += 1 # Determine all the actions that the ticket has available availableActions = TicketSystem(self.env).get_available_actions(req, ticket) # See if any actions we want are actually available chosenAction = None for action in availableActions: if action in desiredActions: chosenAction = action break if chosenAction: controllers = self._get_action_controllers(req, ticket, chosenAction) for controller in controllers: changes = controller.get_ticket_changes(req, ticket, action) for key, value in changes.items(): ticket[key] = value 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: print>>sys.stderr, 'Unexpected error while processing ticket ' \ 'ID %s: %s' % (tkt_id, e) def _get_action_controllers(self, req, ticket, action): """Generator yielding the controllers handling the given `action`""" for controller in TicketSystem(self.env).action_controllers: actions = [a for w,a in controller.get_ticket_actions(req, ticket)] if action in actions: yield controller 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()
