]> sipb.mit.edu Git - snippets/.git/blobdiff - TracZephyrPlugin/ZephyrPlugin.py
Apparently shlex.split doesn’t work on unicode.
[snippets/.git] / TracZephyrPlugin / ZephyrPlugin.py
index ad9186582125f96d693ca119e12715bc3203f358..87c8762d7756c48a1a41ca637aab29bb6279b266 100644 (file)
@@ -2,6 +2,7 @@ from trac.core import *
 from trac.ticket import ITicketChangeListener
 import subprocess
 import textwrap
+import shlex
 
 class ZephyrPlugin(Component):
     implements(ITicketChangeListener)
@@ -10,32 +11,49 @@ class ZephyrPlugin(Component):
         zclass = self.config.get('ZephyrPlugin', 'class')
         if zclass == '':
             return
-        command = self.config.get('ZephyrPlugin', 'command')
+        command = shlex.split(self.config.get('ZephyrPlugin', 'command').encode('utf-8'))
         if not command:
-            command = 'zwrite'
-        p = subprocess.Popen([command, '-q', '-l', '-d',
-                              '-c', zclass,
+            command = ['zwrite', '-q', '-l', '-d']
+        p = subprocess.Popen(command +
+                             ['-c', zclass,
                               '-i', 'trac-#%s' % id],
                              stdin=subprocess.PIPE)
-        p.stdin.write(message.encode('utf-8', 'replace'))
+        p.stdin.write(message.replace('@', '@@').encode('utf-8', 'replace'))
         p.stdin.close()
         p.wait()
+
+    def format_text(self, text):
+        lines = textwrap.fill(text).split('\n')
+        if len(lines) > 5:
+            lines = lines[:5] + [u'[…]']
+        return '\n'.join(lines)
     
     def ticket_created(self, ticket):
         message = "%s filed a new ticket:\n%s\n\n%s" % (ticket['reporter'],
                                                         ticket['summary'],
-                                                        textwrap.fill(ticket['description'][:255]))
+                                                        self.format_text(ticket['description']))
         self.zwrite(ticket.id, message)
     
     def ticket_changed(self, ticket, comment, author, old_values):
-        if old_values.has_key('status'):
-            if ticket['status'] == 'closed':
-                message = "%s closed ticket as %s\n(%s)" % (author, ticket['resolution'], ticket['summary'])
+        message = ''
+        for field in ticket.fields:
+            name = field['name']
+            if name not in old_values:
+                pass
+            elif field['type'] == 'textarea':
+                message += "%s changed %s to:\n%s\n" % (author, name, self.format_text(ticket[name]))
+            elif ticket[name] and old_values[name]:
+                message += "%s changed %s from %s to %s.\n" % (author, name, old_values[name], ticket[name])
+            elif ticket[name]:
+                message += "%s set %s to %s.\n" % (author, name, ticket[name])
+            elif old_values[name]:
+                message += "%s deleted %s.\n" % (author, name)
             else:
-                message = "%s set status to %s\n(%s)" % (author, ticket['status'], ticket['summary'])
-        else:
-            message = "%s updated this ticket\n(%s)" % (author, ticket['summary'])
+                message += "%s changed %s.\n" % (author, name)
+        if comment:
+            message += "%s commented:\n%s\n" % (author, self.format_text(comment))
         self.zwrite(ticket.id, message)
     
     def ticket_deleted(self, ticket):
-        pass
+        message = "%s deleted ticket %d" % (author, ticket.id)
+        self.zwrite(ticket.id, message)