Skip to content

Instantly share code, notes, and snippets.

@ivanelson
Last active January 1, 2016 01:39
Show Gist options
  • Save ivanelson/8074704 to your computer and use it in GitHub Desktop.
Save ivanelson/8074704 to your computer and use it in GitHub Desktop.
Just close the ticket(Trac), if you insert a comment. http://trac.edgewall.org/
#-*- encoding: utf-8 -*-
from trac.core import Component, implements
from trac.ticket.api import ITicketManipulator, ITicketChangeListener
from trac.ticket.model import Ticket
class ValidComment(Component):
implements(ITicketManipulator, ITicketChangeListener)
def prepare_ticket(self, req, ticket, fields, actions):
pass
def validate_ticket(self, req, ticket):
action = req.args.get('action')
comment = ticket._old.get('comment', ticket['comment'])
self.log.info('ValidComment Action in validate_ticket: %s' % action)
self.log.info('ValidComment Comment in validate_ticket: %s' % comment)
if action in ('resolve', 'reopen'):
if not comment:
return [(None, "Before closing the Ticket #%s, Enter a comment." % ticket.id)]
return []
def ticket_created(self, ticket):
pass
def ticket_changed(self, ticket, comment, author, old_values):
pass
@hasienda
Copy link

Note that the actual ticket change has not happened, when 'validate_ticket' is executed. So your db query will yield last change's comment. The current one is in ticket.['comment']. You can even check for attempted state transition by comparing ticket[value] to ticket._old[value], so there should normally be no need for db access from an ITicketManipulator, you see?

Besides, 'env.get_db_cnx()' is the old method (0.11 compatible), that will not work much longer. At least it has been depreciated and announced to get dropped before Trac 1.2.

@ivanelson
Copy link
Author

Yes, the solution was more elegant. But it gets better.

I want a single click:
Check if there comment if yes close the ticket, otherwise do nothing.

Any suggestions?

@hasienda
Copy link

Writing the test for comment slightly different shall do the trick (return values from input text field should never return None btw.):

 if not comment:

@ivanelson
Copy link
Author

Hasienda,

Using the 'validate_ticket' method I need 02 actions. The first action is to write the comment and the second is closing the ticket.

How do I check if the user typed the comment in the 'form' and is also closing the ticket?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment