Only put in the username instead of the name+email.
[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 authorPattern = re.compile(r'<(.+)@')
37 tickets = {}
38
39 env = open_environment(project)
40 repos = env.get_repository()
41 repos.sync()
42
43 changeset = repos.get_changeset(rev)
44
45 for command, ticketList in commandPattern.findall(changeset.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                 if change['permanent']:
62                         cnum += 1
63
64         username = authorPattern.findall(changeset.author)[0]
65         now = datetime.now(utc)
66         message = "(In [%s]) %s" % (rev, changeset.message)
67         ticket.save_changes(username, message, now, db, cnum+1)
68         db.commit()
69
70         tn = TicketNotifyEmail(env)
71         tn.notify(ticket, newticket=0, modtime=now)
72