]> snippets.scripts.mit.edu Git - Scripts/git/.git/blame - TracZephyrPlugin/ZephyrPlugin.py
TracZephyrPlugin: Display the priority of new tickets.
[Scripts/git/.git] / TracZephyrPlugin / ZephyrPlugin.py
CommitLineData
8293875f
AK
1# -*- coding: utf-8 -*-
2
76e24b1f
EB
3from trac.core import *
4from trac.ticket import ITicketChangeListener
6ae9f02e 5import subprocess
b9d0b655 6import re
76e24b1f 7import textwrap
1246b147 8import shlex
76e24b1f 9
99f25bfd
AK
10quoted_re = re.compile('^(?:> ?\n)*> .+\n(?:>(?: .*)?\n)*', re.MULTILINE)
11
76e24b1f 12class ZephyrPlugin(Component):
cace67b2
EB
13 implements(ITicketChangeListener)
14
15 def zwrite(self, id, message):
16 zclass = self.config.get('ZephyrPlugin', 'class')
17 if zclass == '':
18 return
f503b22f 19 command = shlex.split(self.config.get('ZephyrPlugin', 'command').encode('utf-8'))
da9785cf 20 if not command:
d23d10ba
GT
21 command = ['zwrite', '-q', '-l', '-d']
22 p = subprocess.Popen(command +
23 ['-c', zclass,
cace67b2
EB
24 '-i', 'trac-#%s' % id],
25 stdin=subprocess.PIPE)
0e5f06a1 26 p.stdin.write(message.replace('@', '@@').encode('utf-8', 'replace'))
cace67b2
EB
27 p.stdin.close()
28 p.wait()
08cf6d42
AK
29
30 def format_text(self, text):
99f25bfd 31 text = re.sub(quoted_re, u'> […]\n', text)
08cf6d42
AK
32 lines = textwrap.fill(text).split('\n')
33 if len(lines) > 5:
34 lines = lines[:5] + [u'[…]']
35 return '\n'.join(lines)
cace67b2
EB
36
37 def ticket_created(self, ticket):
c00679cc
GT
38 message = "%s filed a new %s ticket:\n%s\n\n%s" % (ticket['reporter'],
39 ticket['priority'],
40 ticket['summary'],
41 self.format_text(ticket['description']))
cace67b2
EB
42 self.zwrite(ticket.id, message)
43
44 def ticket_changed(self, ticket, comment, author, old_values):
3271653b 45 message = "(%s)\n" % ticket['summary']
c5aba9f6
AK
46 for field in ticket.fields:
47 name = field['name']
48 if name not in old_values:
49 pass
50 elif field['type'] == 'textarea':
51 message += "%s changed %s to:\n%s\n" % (author, name, self.format_text(ticket[name]))
52 elif ticket[name] and old_values[name]:
53 message += "%s changed %s from %s to %s.\n" % (author, name, old_values[name], ticket[name])
54 elif ticket[name]:
55 message += "%s set %s to %s.\n" % (author, name, ticket[name])
56 elif old_values[name]:
57 message += "%s deleted %s.\n" % (author, name)
cace67b2 58 else:
c5aba9f6
AK
59 message += "%s changed %s.\n" % (author, name)
60 if comment:
61 message += "%s commented:\n%s\n" % (author, self.format_text(comment))
cace67b2
EB
62 self.zwrite(ticket.id, message)
63
64 def ticket_deleted(self, ticket):
c5aba9f6
AK
65 message = "%s deleted ticket %d" % (author, ticket.id)
66 self.zwrite(ticket.id, message)