]>
Commit | Line | Data |
---|---|---|
8293875f AK |
1 | # -*- coding: utf-8 -*- |
2 | ||
76e24b1f EB |
3 | from trac.core import * |
4 | from trac.ticket import ITicketChangeListener | |
6ae9f02e | 5 | import subprocess |
b9d0b655 | 6 | import re |
76e24b1f | 7 | import textwrap |
1246b147 | 8 | import shlex |
76e24b1f | 9 | |
99f25bfd AK |
10 | quoted_re = re.compile('^(?:> ?\n)*> .+\n(?:>(?: .*)?\n)*', re.MULTILINE) |
11 | ||
76e24b1f | 12 | class 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 | 21 | command = ['zwrite', '-q', '-l', '-d'] |
776bd957 EB |
22 | opcode = self.config.get('ZephyrPlugin', 'opcode') |
23 | if opcode: | |
24 | command += ['-O', opcode] | |
9bb42342 JR |
25 | signature = self.config.get('ZephyrPlugin', 'signature') |
26 | if signature: | |
27 | command += ['-s', signature] | |
d23d10ba GT |
28 | p = subprocess.Popen(command + |
29 | ['-c', zclass, | |
cace67b2 EB |
30 | '-i', 'trac-#%s' % id], |
31 | stdin=subprocess.PIPE) | |
0e5f06a1 | 32 | p.stdin.write(message.replace('@', '@@').encode('utf-8', 'replace')) |
cace67b2 EB |
33 | p.stdin.close() |
34 | p.wait() | |
08cf6d42 AK |
35 | |
36 | def format_text(self, text): | |
99f25bfd | 37 | text = re.sub(quoted_re, u'> […]\n', text) |
08cf6d42 AK |
38 | lines = textwrap.fill(text).split('\n') |
39 | if len(lines) > 5: | |
40 | lines = lines[:5] + [u'[…]'] | |
41 | return '\n'.join(lines) | |
cace67b2 EB |
42 | |
43 | def ticket_created(self, ticket): | |
e3cc23ba JR |
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'], | |
c00679cc | 48 | ticket['priority'], |
e3cc23ba | 49 | ttype, |
c00679cc GT |
50 | ticket['summary'], |
51 | self.format_text(ticket['description'])) | |
cace67b2 EB |
52 | self.zwrite(ticket.id, message) |
53 | ||
54 | def ticket_changed(self, ticket, comment, author, old_values): | |
3271653b | 55 | message = "(%s)\n" % ticket['summary'] |
c5aba9f6 AK |
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) | |
cace67b2 | 68 | else: |
c5aba9f6 AK |
69 | message += "%s changed %s.\n" % (author, name) |
70 | if comment: | |
71 | message += "%s commented:\n%s\n" % (author, self.format_text(comment)) | |
cace67b2 EB |
72 | self.zwrite(ticket.id, message) |
73 | ||
74 | def ticket_deleted(self, ticket): | |
c5aba9f6 AK |
75 | message = "%s deleted ticket %d" % (author, ticket.id) |
76 | self.zwrite(ticket.id, message) |