Use a 0.11 idioim for tracking cnum. Seems to have fixed a cnum stomp I had
[git-central.git] / server / post-receive-trac.py
1 #!/usr/bin/env python
2
3 # This should work:
4 #
5 #    Changed blah and foo to do this or that. Re #10 and #12, and qa #12.
6 #
7
8 import re
9 import sys
10 from datetime import datetime
11
12 from trac.env import open_environment
13 from trac.ticket.notification import TicketNotifyEmail
14 from trac.ticket import Ticket
15 from trac.ticket.web_ui import TicketModule
16 from trac.util.datefmt import utc
17 from trac.versioncontrol.api import NoSuchChangeset
18
19 project = sys.argv[1]
20 refname = sys.argv[2]
21 describe = sys.argv[3]
22 describe_tags = sys.argv[4]
23 rev = sys.argv[5]
24 author = sys.argv[6]
25 message = sys.argv[7]
26
27 def refs(ticket):
28         pass
29
30 def qa(ticket):
31         if ticket['phase'] == 'Final Fixing':
32                 ticket['phase'] = 'Final QA'
33         else:
34                 ticket['phase'] = 'Initial QA'
35         ticket['owner'] = ''
36         ticket['status'] = 'new'
37
38 commands = { 're': refs, 'refs': refs, 'qa': qa }
39 commandPattern = re.compile(r'(?P<action>[A-Za-z]*).?(?P<ticket>#[0-9]+(?:(?:[, &]*|[ ]?and[ ]?)#[0-9]+)*)')
40 ticketPattern = re.compile(r'#([0-9]*)')
41 authorPattern = re.compile(r'<(.+)@')
42 tickets = {}
43
44 env = open_environment(project)
45 for command, ticketList in commandPattern.findall(message):
46         if commands.has_key(command.lower()):
47                 for ticketId in ticketPattern.findall(ticketList):
48                         tickets.setdefault(ticketId, []).append(commands[command.lower()])
49
50 for ticketId, commands in tickets.iteritems():
51         db = env.get_db_cnx()
52
53         ticket = Ticket(env, int(ticketId), db)
54         for command in commands:
55                 command(ticket)
56
57         # determine sequence number...
58         cnum = 0
59         tm = TicketModule(env)
60         for change in tm.grouped_changelog_entries(ticket, db):
61                 c_cnum = change.get('cnum', None)
62                 if c_cnum and int(c_cnum) > cnum:
63                         cnum = int(c_cnum)
64
65         username = authorPattern.findall(author)[0]
66         now = datetime.now(utc)
67         message = "(On %s [changeset:%s %s]) %s" % (refname, rev, describe_tags, message)
68         ticket['branch'] = refname
69         ticket.save_changes(username, message, now, db, cnum+1)
70         db.commit()
71
72         tn = TicketNotifyEmail(env)
73         tn.notify(ticket, newticket=0, modtime=now)
74