Fix variable name stomp.
[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 rev = sys.argv[2]
21
22 def refs(ticket):
23         pass
24
25 def qa(ticket):
26         if ticket['phase'] == 'Final Fixing':
27                 ticket['phase'] = 'Final QA'
28         else:
29                 ticket['phase'] = 'Initial QA'
30         ticket['owner'] = ''
31         ticket['status'] = 'new'
32
33 commands = { 're': refs, 'refs': refs, 'qa': qa }
34 commandPattern = re.compile(r'(?P<action>[A-Za-z]*).?(?P<ticket>#[0-9]+(?:(?:[, &]*|[ ]?and[ ]?)#[0-9]+)*)')
35 ticketPattern = re.compile(r'#([0-9]*)')
36 tickets = {}
37
38 env = open_environment(project)
39 repos = env.get_repository()
40 repos.sync()
41
42 changeset = repos.get_changeset(rev)
43
44 for command, ticketList in commandPattern.findall(changeset.message):
45         if commands.has_key(command.lower()):
46                 for ticketId in ticketPattern.findall(ticketList):
47                         tickets.setdefault(ticketId, []).append(commands[command.lower()])
48
49 for ticketId, commands in tickets.iteritems():
50         db = env.get_db_cnx()
51
52         ticket = Ticket(env, int(ticketId), db)
53         for command in commands:
54                 command(ticket)
55
56         # determine sequence number...
57         cnum = 0
58         tm = TicketModule(env)
59         for change in tm.grouped_changelog_entries(ticket, db):
60                 if change['permanent']:
61                         cnum += 1
62
63         now = datetime.now(utc)
64         message = "(In [%s]) %s" % (rev, changeset.message)
65         ticket.save_changes(changeset.author, message, now, db, cnum+1)
66         db.commit()
67
68         tn = TicketNotifyEmail(env)
69         tn.notify(ticket, newticket=0, modtime=now)
70