Skip to content

Instantly share code, notes, and snippets.

@al-the-x
Last active August 29, 2015 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save al-the-x/017c91be24bcf470307c to your computer and use it in GitHub Desktop.
Save al-the-x/017c91be24bcf470307c to your computer and use it in GitHub Desktop.
Trac Plugin for assigning oldest open Ticket to currently authenticated User, even if the `authuser` doesn't have `TICKET_UPDATE` permissions... all for my peeps at @WordPress
from trac.core import *
from trac.env import open_environment
from tracrpc.api import IXMLRPCHandler, expose_rpc
from tracrpc.util import to_timestamp
import trac.ticket.model as model
class WPGimmeTicketRPC(Component):
implements(IXMLRPCHandler)
def xmlrpc_namespace(self):
return 'gimme'
def xmlrpc_methods(self):
yield ('TICKET_CREATE', ((bool, str), (bool, str, int)), self.ticket)
def ticket(self, req):
env = open_environment() ## omitting the path uses $ENV[TRAC_ENV]
with env.db_transaction as db:
cursor = db.cursor()
cursor.execute("""
SELECT id FROM ticket
WHERE owner = %s LIMIT 1
""", (req.authuser,))
if len(cursor): return (False, 'You already have a ticket!')
## Honestly, I'm not 100% certain about the value of `ticket_enum.name`...
cursor.execute("""
SELECT id FROM ticket
JOIN ticket_enum ON ticket.type = ticket_enum.id
WHERE (owner IS NULL OR owner = '')
AND ticket_enum.name = 'open'
ORDER BY time ASC
LIMIT 1
""")
ticket_id = cursor.fetchone()[0]
ticket = model.Ticket(env, ticket_id)
ticket['owner'] = req.authuser
ticket.save_changes(comment=("Assigned to %s automatically via Magic Button" % req.authuser))
return (True, 'Assigned you ticket %s' % ticket_id, ticket_id)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment