Creating the python scriptThe Python script for bug system integration is called review_action.py. This script needs to
Optionally, you can also
Example: Creating a connection to BugzillaRequirement: In order to fully run this example, you will need to include an external python JSON library in order to de-serialize and parse the JSON object. You can find a working implementation of the library used in our example here: http://sourceforge.net/projects/json-py/ Alternatively, you can use a different third-party python JSON library. In this example, the Python script specifies a connection to the open-source bug tracking system Bugzilla when the user clicks the 'Send to Bugzilla' button in Static Code Analysis. As is typical for the review_action.py script, it defines the issue data that should be sent to Bugzilla, such as the issue ID, the checker that found the issue, the checker message, and the issue's URL. (For a list of the predefined variables for the script, see Using predefined variables.) The script also includes definition #ui.name to define the custom name for the button, Send to Bugzilla, instead of the default value (Create a ticket). The set_bug_id method specifies a link to Bugzilla, which will become a hyperlink to the issue's corresponding Bugzilla bug, and the success_msg method defines a diagnostic message. Note: The variable below for current_path is dependent on where you unpack your python library.
import os.path, urllib, urllib2, getpass, sys, inspect, os, re import sys current_path = "<path to unpacked json script>" sys.path.append(current_path) import json #Button name #ui.name:Send to Bugzilla # Parse the project name based on the issue URL def getProjectName(): #Retrieve project name m = re.search('project=(\w*),', issue.url) project_name = m.group(1) return project_name # Send a request to Bugzilla REST API def bugzillaAPIRequest(proj_name): # URL with authentication information host = "https://api-dev.bugzilla.mozilla.org/test/latest/bug?username=prezioso@gmail.com&password=klocwork" # Bugzilla attributes product = "FoodReplicator" component = "Salt" version = "1.0" target = "---" summary = "%s | %s | %s | %s | %s | %s | %s" % (issue.id, issue.code, issue.severity, issue.severityCode, issue.status, issue.url, proj_name) op_sys = "Linux" platform = "All" # JSON object jdata = '''{"product": "%s", "component": "%s", "version": "%s", "target": "%s", "summary": "%s", "opsys": "%s", "platform": "%s"}''' % (product, component, version, target, summary, op_sys, platform) clen = len(jdata) # HTTP Request req = urllib2.Request(host, jdata, {'Content-Type': 'application/json', 'Accept': 'application/json', 'Content-Length': clen}) try: resp = urllib2.urlopen(req) except urllib2.URLError, e: success(e.code) # Expecting 201, resource created status code if (e.code is 201): content = e.read() string = str(content) obj = json.read(string) # Retrieving ref URL and the id ref=obj['ref'] id=obj['id'] set_bug_id(id) return "%s" % (ref) return "fail" proj_name = getProjectName() successmsg = bugzillaAPIRequest(proj_name) if (successmsg is "fail"): fail("\nERROR - HTTP POST REQUEST") successmsg = successmsg.decode('string_escape') success(successmsg) After the script has been installed, the issue window looks like the following display, with the BUG ID field showing the hyperlink to the issue's Bugzilla entry.
Example: Integrating with Rational Team ConcertThe following example provides a walkthrough of the steps you need to do in order to integrate your Klocwork environment with Rational Team Concert. How to navigate RTC xml documentsTo create a work item in RTC you must first identify the relevant URL and project key by requesting a series of xml documents from the RTC server . The steps will be similar to:
How to handle authenticationBy default the RTC server is setup to use forms based authentication so it is necessary to look for an authentication response request when posting anything to the RTC server. For example: req = urllib2.Request(targetUrl, data,{'Content-Type': 'application/json', 'Accept' : "application/xml"}) try : response = urllib2.urlopen(req) if 'X-com-ibm-team-repository-web-auth-msg' in response.info() and response.info()['X-com-ibm-team-repository-web-auth-msg'] == 'authrequired' : reqLogon = urllib2.Request(hostbase+"/j_security_check",urllib.urlencode({'j_username': str(rtc_username), 'j_password': str(rtc_password)}), {'Content-Type': 'application/x-www-form-urlencoded', 'Accept' : "application/json"}) response = urllib2.urlopen(reqLogon) if 'X-com-ibm-team-repository-web-auth-msg' in response.info() and response.info()['X-com-ibm-team-repository-web-auth-msg'] == 'authfailed' : fail("RTC logon failed") else : if data is not None: # if we posted data then we need to repost it response = urllib2.urlopen(req) except HTTPError, e: eData = str(e.read()) if hasattr(e, 'code'): if e.code == 201 :# 201 indicates success with a RTC response # process response elif e.code == 400 : # 400 indicated RTC rejected the request # process error message How to handle field valuesField value enumerators (e.g. “oslc_cm:priority”) are defined on a per project basis and can be customized by the RTC administrator. To discover the configured values for a particular project, use your browser and the project’s unique project key to query the server. For example: http(s):// yourRTCserver:port/ccm/oslc/enumerations/_Day3sOHaEeO1tdkD6QbZUQ/priority Need help? Contact the Rogue Wave Professional Services team.The Rogue Wave professional services team has experience creating basic scripts for Bugzilla, JIRA, and IBM Rational Team Concert. The scripts can be customized for your installation. Contact the Rogue Wave Professional Services Team for help.
Customizing the 'Create a ticket' buttonPlacing the review_action.py script in the projects_root/config directory causes Static Code Analysis to display the 'Create a ticket' button on the issue details pages in Static Code Analysis for the project. (You may need to refresh the browser to see the link.) If you want to specify a different name for the button, add the following line to your script: #ui.name:<my custom button> For example: #ui.name:Send to Bugzilla After the script has been installed, the issue window shows the new button:
You can put Japanese characters in the button name by including UTF-8 encoding in the review_action.py script.
Including diagnostic messagesIf the bug report is filed successfully when the 'Create a ticket' button is clicked, the 'Ticket created' message pops up on the Static Code Analysis window. If it fails, 'Bug reporting failed' appears. To provide custom diagnostic messages for success and failure, use these methods in the Python script:
If you call the fail() method in the script, it will interrupt further script execution.
Placing the script in projects_rootWhen you've completed your review_action.py script, place it in the projects_root/config directory for the project.
Using predefined variablesFor the review_action.py script, the following variables have been predefined:
|