-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On 29.09.2012 07:36, radelphe wrote:
> Hi all,
>  
> I'm new in Trac system, but I like it.
>  
> I would like to call an external script when a ticket is created or
> closed or reopened.
> On that script I'd like to use ticket summary, status, and reporter
> (maybe other field too) as arguments.
> I searched on the archive, and I found this topic (snip)

> Anyone can help me please?

Well writing entire plugins is a bit over the top, even more as you seem
to rely on ready-made solutions.

In fact, the linked mailing-list thread contains plenty of valuable
hints, and building the solution is merely copy-paste and combine the
pieces. Look at the bare-bone ITicketChangeListener implementation
written by Chris Heller:


from trac.core import *
from trac.ticket import ITicketChangeListener

class TicketListenerPluginExample(Component):
    """ The 'Hello World' of using the ITicketChangeListener interface
    """

    implements(ITicketChangeListener)

    # ITicketChangeListener Interface

    def ticket_created(self, ticket):
        self.log.info('Ticket created: %r' % ticket)

    def ticket_changed(self, ticket, comment, author, old_values):
        self.log.info('Ticket modified: %r' % ticket)

    def ticket_deleted(self, ticket):
        self.log.info('Ticket deleted: %r' % ticket)


Put this into a file, and save the file as
 <env>/plugins/MyTicketChangeScriptCaller.py
to get it recognized by Trac. It's that simple.

Instead of logging you want to call out to a script. Fine, yoheeb's code
is right above:

Popen(["/bin/mycmd", "myarg"], env={"PATH": "/usr/bin"})

Putting it all together:


from subprocess import Popen

from trac.core import *
from trac.config  import Option
from trac.ticket import ITicketChangeListener

class TicketChangeScript(Component):
    """ Calling a script file on ticket changes."""

    implements(ITicketChangeListener)

    script = Option('ticket_change_script', 'call', '',
                    doc="File with path and common arguments")

    # ITicketChangeListener Interface

    def ticket_created(self, ticket):
        self._call(ticket, 'created')

    def ticket_changed(self, ticket, comment, author, old_values):
        self._call(ticket, 'modified')

#    def ticket_deleted(self, ticket):
#        # Beware: We may not have all ticket properties here,
#        # just ticket number.
#        self._call(ticket, 'deleted')

    def _call(self, tkt, tkt_change):
        Popen([' '.join([script, tkt.id,
                         tkt['summary'], tkt_change])],
              shell=True)


Additional pieces are
 * additional option to read script from configruation instead of
hard-coding into the plugin
 * factor general call out into a private method to prevent code duplication
 * signal Popen to use the shell, note some differences between Unix and
Windows here: http://stackoverflow.com/questions/1253122
 * extracting properties from Ticket object, that is given to each
change listener in the ticket argument, see other available properties
in the source: Ticket class in trac/ticket/model.py

In your `trac.ini` create a new section:


[ticket_change_script]
call = /usr/local/bin/my_script.sh -my_common_args


Your (BASH) script will get called effectively like so:
$> /bin/sh /usr/local/bin/my_script.sh -my_common_args <ticket.id>
<summary> <created|modified|deleted>

This is completely untested, but I hope it's clearer now.?

Steffen Hoffmann
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAlBm8XQACgkQ31DJeiZFuHf60ACgqPcRtCZdr1gCEXkNKQbpt3wT
k+IAnA9JC4LQjmSpsHS/H1oWfR03ylP5
=M0Ke
-----END PGP SIGNATURE-----

-- 
You received this message because you are subscribed to the Google Groups "Trac 
Users" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/trac-users?hl=en.

Reply via email to