673c339b99dfd5eff8b2b571dfa855a5e6f83315
[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         signature = self.config.get('ZephyrPlugin', 'signature')
26         if signature:
27             command += ['-s', signature]
28         p = subprocess.Popen(command +
29                              ['-c', zclass,
30                               '-i', 'trac-#%s' % id],
31                              stdin=subprocess.PIPE)
32         p.stdin.write(message.replace('@', '@@').encode('utf-8', 'replace'))
33         p.stdin.close()
34         p.wait()
35
36     def format_text(self, text):
37         text = re.sub(quoted_re, u'> […]\n', text)
38         lines = textwrap.fill(text).split('\n')
39         if len(lines) > 5:
40             lines = lines[:5] + [u'[…]']
41         return '\n'.join(lines)
42     
43     def ticket_created(self, ticket):
44         ttype='ticket'
45         if ticket['type'] != 'defect':
46             ttype=ticket['type']
47         message = "%s filed a new %s %s:\n%s\n\n%s" % (ticket['reporter'],
48                                                            ticket['priority'],
49                                                            ttype,
50                                                            ticket['summary'],
51                                                            self.format_text(ticket['description']))
52         self.zwrite(ticket.id, message)
53     
54     def ticket_changed(self, ticket, comment, author, old_values):
55         message = "(%s)\n" % ticket['summary']
56         for field in ticket.fields:
57             name = field['name']
58             if name not in old_values:
59                 pass
60             elif field['type'] == 'textarea':
61                 message += "%s changed %s to:\n%s\n" % (author, name, self.format_text(ticket[name]))
62             elif ticket[name] and old_values[name]:
63                 message += "%s changed %s from %s to %s.\n" % (author, name, old_values[name], ticket[name])
64             elif ticket[name]:
65                 message += "%s set %s to %s.\n" % (author, name, ticket[name])
66             elif old_values[name]:
67                 message += "%s deleted %s.\n" % (author, name)
68             else:
69                 message += "%s changed %s.\n" % (author, name)
70         if comment:
71             message += "%s commented:\n%s\n" % (author, self.format_text(comment))
72         self.zwrite(ticket.id, message)
73     
74     def ticket_deleted(self, ticket):
75         message = "%s deleted ticket %d" % (author, ticket.id)
76         self.zwrite(ticket.id, message)