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