8daedd18102d71338c746c270071c6a024a71049
[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         ttype='ticket'
42         if ticket['type'] != 'defect':
43             ttype=ticket['type']
44         message = "%s filed a new %s %s:\n%s\n\n%s" % (ticket['reporter'],
45                                                            ticket['priority'],
46                                                            ttype,
47                                                            ticket['summary'],
48                                                            self.format_text(ticket['description']))
49         self.zwrite(ticket.id, message)
50     
51     def ticket_changed(self, ticket, comment, author, old_values):
52         message = "(%s)\n" % ticket['summary']
53         for field in ticket.fields:
54             name = field['name']
55             if name not in old_values:
56                 pass
57             elif field['type'] == 'textarea':
58                 message += "%s changed %s to:\n%s\n" % (author, name, self.format_text(ticket[name]))
59             elif ticket[name] and old_values[name]:
60                 message += "%s changed %s from %s to %s.\n" % (author, name, old_values[name], ticket[name])
61             elif ticket[name]:
62                 message += "%s set %s to %s.\n" % (author, name, ticket[name])
63             elif old_values[name]:
64                 message += "%s deleted %s.\n" % (author, name)
65             else:
66                 message += "%s changed %s.\n" % (author, name)
67         if comment:
68             message += "%s commented:\n%s\n" % (author, self.format_text(comment))
69         self.zwrite(ticket.id, message)
70     
71     def ticket_deleted(self, ticket):
72         message = "%s deleted ticket %d" % (author, ticket.id)
73         self.zwrite(ticket.id, message)