]> sipb.mit.edu Git - snippets/.git/blob - TracZephyrPlugin/ZephyrPlugin.py
TracZephyrPlugin: Allow setting an opcode on outgoing zephyrs.
[snippets/.git] / TracZephyrPlugin / ZephyrPlugin.py
1 # -*- coding: utf-8 -*-
2
3 from trac.core import *
4 from trac.ticket import ITicketChangeListener
5 import subprocess
6 import re
7 import textwrap
8 import shlex
9
10 quoted_re = re.compile('^(?:> ?\n)*> .+\n(?:>(?: .*)?\n)*', re.MULTILINE)
11
12 class ZephyrPlugin(Component):
13     implements(ITicketChangeListener)
14     
15     def zwrite(self, id, message):
16         zclass = self.config.get('ZephyrPlugin', 'class')
17         if zclass == '':
18             return
19         command = shlex.split(self.config.get('ZephyrPlugin', 'command').encode('utf-8'))
20         if not command:
21             command = ['zwrite', '-q', '-l', '-d']
22         opcode = self.config.get('ZephyrPlugin', 'opcode')
23         if opcode:
24             command += ['-O', opcode]
25         p = subprocess.Popen(command +
26                              ['-c', zclass,
27                               '-i', 'trac-#%s' % id],
28                              stdin=subprocess.PIPE)
29         p.stdin.write(message.replace('@', '@@').encode('utf-8', 'replace'))
30         p.stdin.close()
31         p.wait()
32
33     def format_text(self, text):
34         text = re.sub(quoted_re, u'> […]\n', text)
35         lines = textwrap.fill(text).split('\n')
36         if len(lines) > 5:
37             lines = lines[:5] + [u'[…]']
38         return '\n'.join(lines)
39     
40     def ticket_created(self, ticket):
41         message = "%s filed a new %s ticket:\n%s\n\n%s" % (ticket['reporter'],
42                                                            ticket['priority'],
43                                                            ticket['summary'],
44                                                            self.format_text(ticket['description']))
45         self.zwrite(ticket.id, message)
46     
47     def ticket_changed(self, ticket, comment, author, old_values):
48         message = "(%s)\n" % ticket['summary']
49         for field in ticket.fields:
50             name = field['name']
51             if name not in old_values:
52                 pass
53             elif field['type'] == 'textarea':
54                 message += "%s changed %s to:\n%s\n" % (author, name, self.format_text(ticket[name]))
55             elif ticket[name] and old_values[name]:
56                 message += "%s changed %s from %s to %s.\n" % (author, name, old_values[name], ticket[name])
57             elif ticket[name]:
58                 message += "%s set %s to %s.\n" % (author, name, ticket[name])
59             elif old_values[name]:
60                 message += "%s deleted %s.\n" % (author, name)
61             else:
62                 message += "%s changed %s.\n" % (author, name)
63         if comment:
64             message += "%s commented:\n%s\n" % (author, self.format_text(comment))
65         self.zwrite(ticket.id, message)
66     
67     def ticket_deleted(self, ticket):
68         message = "%s deleted ticket %d" % (author, ticket.id)
69         self.zwrite(ticket.id, message)