]>
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 | ||
03162da9 | 15 | def zwrite(self, id, message, extra_sig=None): |
cace67b2 EB |
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 | 25 | signature = self.config.get('ZephyrPlugin', 'signature') |
03162da9 AD |
26 | if extra_sig: |
27 | if signature: | |
28 | signature = "%s: %s" % (signature, extra_sig, ) | |
29 | else: | |
30 | signature = extra_sig | |
9bb42342 JR |
31 | if signature: |
32 | command += ['-s', signature] | |
d23d10ba GT |
33 | p = subprocess.Popen(command + |
34 | ['-c', zclass, | |
cace67b2 EB |
35 | '-i', 'trac-#%s' % id], |
36 | stdin=subprocess.PIPE) | |
0e5f06a1 | 37 | p.stdin.write(message.replace('@', '@@').encode('utf-8', 'replace')) |
cace67b2 EB |
38 | p.stdin.close() |
39 | p.wait() | |
08cf6d42 AK |
40 | |
41 | def format_text(self, text): | |
99f25bfd | 42 | text = re.sub(quoted_re, u'> […]\n', text) |
08cf6d42 AK |
43 | lines = textwrap.fill(text).split('\n') |
44 | if len(lines) > 5: | |
45 | lines = lines[:5] + [u'[…]'] | |
46 | return '\n'.join(lines) | |
cace67b2 | 47 | |
03162da9 AD |
48 | def get_url(self, ticket): |
49 | return ticket.env.abs_href.ticket(ticket.id) | |
50 | ||
cace67b2 | 51 | def ticket_created(self, ticket): |
e3cc23ba JR |
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'], | |
c00679cc | 56 | ticket['priority'], |
e3cc23ba | 57 | ttype, |
c00679cc GT |
58 | ticket['summary'], |
59 | self.format_text(ticket['description'])) | |
03162da9 | 60 | self.zwrite(ticket.id, message, extra_sig=self.get_url(ticket)) |
cace67b2 EB |
61 | |
62 | def ticket_changed(self, ticket, comment, author, old_values): | |
3271653b | 63 | message = "(%s)\n" % ticket['summary'] |
c5aba9f6 AK |
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) | |
cace67b2 | 76 | else: |
c5aba9f6 AK |
77 | message += "%s changed %s.\n" % (author, name) |
78 | if comment: | |
79 | message += "%s commented:\n%s\n" % (author, self.format_text(comment)) | |
03162da9 | 80 | self.zwrite(ticket.id, message, extra_sig=self.get_url(ticket)) |
cace67b2 EB |
81 | |
82 | def ticket_deleted(self, ticket): | |
c5aba9f6 | 83 | message = "%s deleted ticket %d" % (author, ticket.id) |
03162da9 | 84 | self.zwrite(ticket.id, message, extra_sig=self.get_url(ticket)) |