]> snippets.scripts.mit.edu Git - Scripts/git/.git/blame_incremental - TracZephyrPlugin/ZephyrPlugin.py
TracZephyrPlugin: Allow setting an opcode on outgoing zephyrs.
[Scripts/git/.git] / TracZephyrPlugin / ZephyrPlugin.py
... / ...
CommitLineData
1# -*- coding: utf-8 -*-
2
3from trac.core import *
4from trac.ticket import ITicketChangeListener
5import subprocess
6import re
7import textwrap
8import shlex
9
10quoted_re = re.compile('^(?:> ?\n)*> .+\n(?:>(?: .*)?\n)*', re.MULTILINE)
11
12class 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 message = "%s filed a new %s ticket:\n%s\n\n%s" % (ticket['reporter'],
42 ticket['priority'],
43 ticket['summary'],
44 self.format_text(ticket['description']))
45 self.zwrite(ticket.id, message)
46
47 def ticket_changed(self, ticket, comment, author, old_values):
48 message = "(%s)\n" % ticket['summary']
49 for field in ticket.fields:
50 name = field['name']
51 if name not in old_values:
52 pass
53 elif field['type'] == 'textarea':
54 message += "%s changed %s to:\n%s\n" % (author, name, self.format_text(ticket[name]))
55 elif ticket[name] and old_values[name]:
56 message += "%s changed %s from %s to %s.\n" % (author, name, old_values[name], ticket[name])
57 elif ticket[name]:
58 message += "%s set %s to %s.\n" % (author, name, ticket[name])
59 elif old_values[name]:
60 message += "%s deleted %s.\n" % (author, name)
61 else:
62 message += "%s changed %s.\n" % (author, name)
63 if comment:
64 message += "%s commented:\n%s\n" % (author, self.format_text(comment))
65 self.zwrite(ticket.id, message)
66
67 def ticket_deleted(self, ticket):
68 message = "%s deleted ticket %d" % (author, ticket.id)
69 self.zwrite(ticket.id, message)