Tools: Python, Trac, XML-RPC Trac plug-in, Komodo Edit or command line
While I love Trac, creating new tickets can be annoying. Web interfaces will never be as fast as direct text input and I don’t usually need all of the options the interface provides. I do, however, wish to use Trac’s system instead of directly editing the database, as I have things tied into ticket creation that would be bypassed otherwise (crossposting to remember the milk, for one). Enter XML-RPC. With this handy plug-in one can interact with a Trac environment from outside of the web interface. My new_ticket.py code will work from within Komodo or on the command line and assumes a multiple project Trac setup. It goes something like this (feel free to use this hack, free as in beer, etc):
import xmlrpclib
import sys
print "Issue to submit:"
issue = sys.stdin.readline()
#get default project from passed arg
project = sys.argv[1]
project = project.split('/');
curproject = project[len(project)-1]
print "Project ["+curproject+"]"
project = sys.stdin.readline()
#if they didn't enter a project, use default
if(project.strip()==''):
project = curproject
print "Milestone [milestone1]:"
milestone = sys.stdin.readline()
if(milestone.strip()==''):
milestone = "milestone1"
#for example only, remove if you don't use timingandestimation plugin
print "Estimated Hours [1]:"
estimatedhours = sys.stdin.readline()
if(estimatedhours.strip()==''):
estimatedhours = '1'
attr = {}
attr['milestone'] = milestone
attr['reporter'] = 'Jaciss'
attr['owner']= 'Jaciss'
attr['status'] = 'assigned'
attr['type'] = 'enhancement'
attr['priority'] = 'major'
attr['estimatedhours'] = estimatedhours
print attr
#notice the /login - if anon access is disabled for xmlrpc
#you'll need extra code in this section
server = xmlrpclib.ServerProxy("http://YOUR.TRAC.SERVER/trac/"+project+"/login/xmlrpc")
newticket = server.ticket.create(issue, '', attr)
#the below will print the new ticket number
print newticket
Obviously this will need to be modified with your own server domain and user name, at the very least. You may also need additional code to handle permissions. It should be fairly self-explanatory otherwise, excepting the project variable.
From the command line I type something like:
python new_ticket.py [project_name]
In Komodo Edit I created a command that opens in a new console:
python /path/to/new_ticket.py %p
The extra project code involves the above command, where %p is Komodo’s substitution variable for the path to the active project. Assuming project directories and Trac environments are named the same way, the python tidbit above which pulls the last part of the path would provide the name of the current project/desired environment, able to be used in the script and neatly skipping a step.
In summary: This is awesome because I can run the command, type the issue, and hit enter until it exits, getting back into the flow of writing code before my brain realizes it’s no longer in “the zone”. My employer and myself use the wonderful grid modify plugin and regularly go over and modify lists of tickets, anyway. It’s a simple case of doing the right thing at the right time.
Updated 12-12-10: formatting, adding comments to the code, general cleanup
Filed under: how-to, commandline, komodoedit, python, trac