Example: Query script

This Python script provides an example of a script to use with the query service.

Notice that because queries are done in the context of a Rational Team Concert™ project, you must include an authentication step.

You can modify this script to work with your site configuration.
jts_location = "https://your.domain.net:9443/jts/" #your jazz team server url
user = "username" #user must have access to gitAdapter on clm
password = "password"
project = "sample.git" # Git project's name *in gitweb*
adapted_url = "https://your.domian.net:9443/gitAdapter/commit/0/0_0/sample.git/" 
#You will need to replace your.domain, the /0/ with the registered Git server id, and 0_0/sample.git with your registered Git project name and preceding forward slash encoding.
#Refer to an existing link on one of your Rational Team Concert work items for the proper value for adapted_url, being sure to omit the last two segments starting with the commit hash or change id 
import urllib2
import sys
import json

# the default urllib2 url-opener doesn't store cookies, which are required
# for authentication with RTC, so we need to build our own opener:
opener = urllib2.build_opener(urllib2.BaseHandler(),
                              urllib2.HTTPSHandler(),
                              urllib2.HTTPRedirectHandler(),# CCM May redirect us
                              urllib2.HTTPCookieProcessor())

def auth_with_rtc(host, user, password):
  # First, we need to get a cookie, so RTC can identify us on future requests:
  res = opener.open(host+"authenticated/identity")

  # Next, send RTC our username and password:
  res = opener.open(host +
      "authenticated/j_security_check?j_username=" + user +
      "&j_password=" + password)  

# That's all! Now any responses with our session cookie will be authenticated. 
def find_associated_work_items(host, adapted_project_url, adapted_project, commit):   
	try:     
		opener.addheaders = [('Accept', 'application/json'),('OSLC-Core-Version', '2.0')]     
		res =opener.open(adapted_project_url+commit)    

	except urllib2.HTTPError as e:         
		# Misconfiguration, mistyping, or network error         
		print "Could not find commit. Is the script configured with the proper"         
		print "logical adapter? Also note that truncated commit hashes are"         
		print "not accepted as inputs."         
		return 1   
	try:         
		query_response = json.load(res)   
	except ValueError:         
		# This probably means there's no JSON object in the response         
		print "There are no work items associated with this commit."         
		return 1    
	# The query service's response is a JSON array containing project areas.   
	for project_area in query_response:      

		# The result contains a second level of stringified JSON.      
		result =project_area['result']       

		# Inside this dictionary, all we care about are the oslc:results      
		# oslc:results is an array of the matched work items in this project.      
		# In a given work item, the info is contained in the rdf:about key.      
		for o_result in json.loads(result)[u'oslc:results']:                
			# oslc:results is an array of the matched work items in this project.                
			# In a given work item, the info is contained in the rdf:about key.                
			print o_result[u'rdf:about']  

if __name__ == "__main__":   
	auth_with_rtc(jts_location, user, password)   
	for arg in sys.argv[1:]:     
			find_associated_work_items(jts_location,adapted_url, project, arg)

Feedback