Getting data out of Jira using a REST interface and Python

It is a lot easier than I expected. Have fun with it! Example code:

#!/usr/bin/python
# Author: J. Baten
# Date: 2012-04-10
import urllib, urllib2, cookielib, json
# set up cookiejar for handling URLs
cookiejar = cookielib.CookieJar()
myopener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cookiejar))
jira_serverurl="http://server:8080/jira"
# Search using JQL
queryJQL = "updated >= -1d"
IssuesQuery = {"jql" : queryJQL,"startAt" : 0,"maxResults" : 1000 }
queryURL = jira_serverurl + "/rest/api/latest/search"
req = urllib2.Request(queryURL)
req.add_data(json.dumps(IssuesQuery))
req.add_header("Content-type", "application/json")
req.add_header("Accept", "application/json")
fp = myopener.open(req)
data = json.load(fp)
#print json.dumps(data,sort_keys=True, indent=2)
#print data["issues"]
for k in  data["issues"]:
print k["key"]
#just take the last one for testing
a=k["key"]
#now how can I get their worklogs?
# /api/2.0.alpha1/issue/{issueKey}
queryURL = jira_serverurl + "/rest/api/latest/issue/" + a
# override for testing purposes
queryURL = jira_serverurl + "/rest/api/latest/issue/ZANDBAK-15"
print queryURL
req = urllib2.Request(queryURL)
#req.add_data(json.dumps(IssuesQuery))
#req.add_header("Content-type", "application/json")
req.add_header("Accept", "application/json")
fp2 = myopener.open(req)
data2 = json.load(fp2)
print json.dumps(data2,sort_keys=True, indent=2)
print "Original estimate :" + str(data2["fields"]["timetracking"]["value"]["timeoriginalestimate"])
print "Current estimate :" + str(data2["fields"]["timetracking"]["value"]["timeestimate"])
# /api/2.0.alpha1/serverInfo
fp2.close()
fp.close()

This entry was posted in Geen categorie. Bookmark the permalink.