1

I'm looking for a way to create (not update) a trac ticket in response to a commit message like "Hack code to not kill your dog (TODO: fix this properly to avoid chasing kittens instead)".

I want the trac system to react on the "TODO" keyword and create a ticket with the content of the commit message, the owner set to the committer and the opening commit already referenced.

While searching on SO I found Open and close trac tickets with a single commit which basically says how I could roll my own solution. Which I'd do if there isn't a pre-made one available. So - is there?

Community
  • 1
  • 1
mabi
  • 5,279
  • 2
  • 43
  • 78

2 Answers2

6

I would suggest looking at the official Trac package for python: http://pypi.python.org/pypi/Trac/0.11.4 and docs http://www.edgewall.org/docs/tags-trac-0.11.7/epydoc/trac-module.html

This is what we use to create tickets in Trac from a python script and I think it's fairly simple to use. You could run this python script as a post commit hook for your VCS.

You can start up a trac environment using your project settings and then new up tickets and save them. There's probably a little more to it for you, but this should give you a good idea:

from trac.env import Environment
from trac.ticket import Ticket

env = Environment(projectSettings, create=0)
tkt = Ticket(env)
tkt['summary'] = 'first line of commit message'
tkt['description'] = 'full commit message'
tkt.save_changes(commitAuthor, '')
cdlk
  • 536
  • 3
  • 9
  • That's probably it. I'll have a try next week, for now thanks for pointing out the trac module docs, I can work my way from that. – mabi May 19 '12 at 20:02
  • where do you get `projectSettings` from? Seems to be the path to the trac root? – totaam Jan 23 '21 at 04:32
4

Needless to say, current Trac stable is 0.12.3, but of course development needs to go with your current version. (You didn't tell us in you question.)

On you question, there is a hint on how to implement different functionality on-top of the CommitTicketUpdater from Trac core. It has update and close as built-in actions, so you'll need to do some change like so (based on current Trac trunk):

  • create an additional option commands_create for commands, that create a new ticket with reference to the changeset, as a space-separated list
  • add a class-wide variable self.comment in both of changeset_added and changeset_modified right after comment assignment
  • add a module cmd_create like (untested)

    def cmd_create(self, ticket, changeset, perm):
        if not self.check_perms or 'TICKET_CREATE' in perm:
            # Commit messages in general is used for a ticket comment.
            # New tickets require summary and we'll want description too,
            # because comment is ignored on ticket creation.
            # So we need to do message processing here beforehand.
            ticket['comment'] = None
            ticket['description'] = self.comment
            ticket['owner'] = changeset.author
            ticket['status'] = 'new'
            ticket['summary'] = ' '.join(['TODO from', str(changeset.rev)])
            ticket.insert()
    
  • alter ticket_command so the regexp matches not only the default function-ticket(s) pairs but the unary 'TODO:' as well (sorry, can't make this working right-away now)

  • extend the private module _parse_message to include another case before if func:

       if cmd.startswith('TODO'):
           tickets.update({None : ['create']})
           continue
    
  • change _update_tickets to make the comment saving conditional, because you won't need/want an additional comment on new tickets)

       if ticket['comment']:
           ticket.save_changes(changeset.author, comment, date, db)
    

Ok, ask back as required, if you like to try this approach.

hasienda
  • 2,390
  • 1
  • 13
  • 16
  • This is really impressive, but goes way beyond what I need. @cdlk probably hit the spot, tho I won't have access to a system to try that until next week. Thanks anyway! – mabi May 19 '12 at 19:58