Thursday, March 11, 2010

Using the XMLRPC interface of Jira in Python



I just got a Jira project enabled from Computer Services. I wanted to try the XMLRPC interface for this software, which is much like it is for Confluence. After a little experimentation, I was successful creating an issue from a Python script. The trickiness comes from the fact that CS requires some extra fields to be filled in, and figuring out the format was a pain. But the code is prety simple.

Include the xmlrpc Python library


from xmlrpclib import Server

First, you set up the server.


s = Server('https://server.example.com/rpc/xmlrpc')


Then you get your authentication token.

auth = s.jira1.login('username', 'password')
There are a lot of different functions available. You can see some possibilities by browsing the documentation for the plugin here. I will show how to create a new issue in our installation.

newissue = s.jira1.createIssue(auth, {'project': 'ProjectName', 'type': '2',
'summary':'Issue Created via XML-RPC',
'description':'Created with Python client',
'environment':'Python 2.6',
'components':[{'name': 'A Component in the Project', 'id': 'ddddd'}],
'customFieldValues': [{'customfieldId': 'customfield_10012', 'values': ['ddddd']},
{'customfieldId': 'customfield_10012', 'values': ['ddddd'],  'key': '1'},
{'customfieldId': 'customfield_10018', 'values': ['Staff']},
{'customfieldId': 'customfield_10001', 'values': ['John McMellen']}],
})
The custom fields are various ones required by our setup. The correct values can be determined by looking at the source html of the New Issue page in Jira. The 'ddddd' values are 5 digit numbers that represent various things set up in the Jira installation.
This will create a new issue of type 2, a New Feature. Type 1 is a Bug, 3 is a Ticket, 4 is an Improvement, 5 is a User Contact, and 6 is a Task. It returns a hashtable object that represents the Issue, and you can further add comments or modify properties using the other functions of the interface.

No comments: