jira1.addComment | Adds a comment to an issue |
jira1.createIssue | Creates an issue in JIRA from a Hashtable object |
jira1.getComments | Returns all comments associated with the issue |
jira1.getComponents | Returns all components available in the specified project |
jira1.getIssue | Gets an issue from a given issue key. |
jira1.getIssuesFromFilter | Executes a saved filter |
jira1.getIssuesFromTextSearch | Find issues using a free text search |
jira1.getIssuesFromTextSearchWithProject | Find issues using a free text search, limited to certain projects |
jira1.getIssueTypes | Returns all visible issue types in the system |
jira1.getPriorities | Returns all priorities in the system |
jira1.getProjectsNoSchemes | Returns a list of projects available to the user |
jira1.getResolutions | Returns all resolutions in the system |
jira1.getSavedFilters | Gets all saved filters available for the currently logged in user |
jira1.getServerInfo | Returns the Server information such as baseUrl, version, edition, buildDate, buildNumber |
jira1.getStatuses | Returns all statuses in the system |
jira1.getSubTaskIssueTypes | Returns all visible subtask issue types in the system |
jira1.getUser username | Returns user’s information given a username |
jira1.getVersions project-key | Returns all versions available in the specified project |
jira1.updateIssue issue-key field-values | Updates an issue in JIRA from a Hashtable object |
package com.kika.JIRACodeReviewDriver;
import org.apache.xmlrpc.client.XmlRpcClient;
import org.apache.xmlrpc.client.XmlRpcClientConfigImpl;
import org.apache.xmlrpc.XmlRpcException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Map;
import java.util.Vector;
public class Test
{
private static final String JIRA_URI = "https://jira.kikamedical.net";
private static final String RPC_PATH = "/rpc/xmlrpc";
private static String STATUS_OPEN = "1";
private static String STATUS_IN_PROGRESS = "3";
private static String STATUS_REOPENED = "4";
private static String STATUS_RESOLVED = "5";
private static String STATUS_CLOSED = "6";
private static String STATUS_SUBMIT = "10000";
private static String STATUS_REJECTED = "10001";
private static final String FILTER_ID = "10347"; // ID of the filter used to list the requests
// 10344 -> K3 3.9
// 10347 -> test K3 Proto
private static final String FIELD_ID_FIXED_IN_BUILD = "customfield_10017";
private static final String FIELD_ID_BUILD = "customfield_10026";
private static final String FIELD_ID_COMMITERS_TESTS = "customfield_10091";
private static final String CODE_REVIEW_COMMENT = "this request has been coded reviewed";
private String a_loginToken;
private XmlRpcClient a_rpcClient;
public static void main(String[] args) {
Test test = new Test(args[0],args[1]);
test.execute(args[2]);
}
public Test(String login,
String password) {
{
// build that has been reviewed
//final String reviewedBuild = args[0];
try
{
// Initialise RPC Client
XmlRpcClientConfigImpl config = new XmlRpcClientConfigImpl();
config.setServerURL(new URL(JIRA_URI + RPC_PATH));
a_rpcClient = new XmlRpcClient();
a_rpcClient.setConfig(config);
// Login and retrieve logon token
Vector<String> loginParams = new Vector<String>(2);
loginParams.add(login);
loginParams.add(password);
a_loginToken = (String) a_rpcClient.execute("jira1.login", loginParams);
}
catch (MalformedURLException e)
{
e.printStackTrace();
}
catch (XmlRpcException e)
{
e.printStackTrace();
}
}
}
public void execute(String reviewedBuild) {
try {
// Retrieve list of issues
Vector<String> parameters = new Vector<String>(2);
parameters.add(a_loginToken);
parameters.add(FILTER_ID);
Object[] issues = (Object[])a_rpcClient.execute("jira1.getIssuesFromFilter", parameters);
// Print projects
for (Object o: issues)
{
Map issue = (Map) o;
final String key = (String)issue.get("key");
final String status = (String)issue.get("status");
try {
if ( isImpactingCode(issue) &&
( status.equals(STATUS_RESOLVED) || status.equals(STATUS_CLOSED) ) ) {
String resolvedOn = getResolvedOn(issue);
if ( resolvedOn != null && compareVersion(reviewedBuild,resolvedOn)>=0 ) {
if ( !isAlreadyFlaggedAsCodeReviewed(key)) {
flagAsCodeReviewed(key,reviewedBuild);
}
}
}
} catch (IllegalArgumentException e) {
System.err.println("IllegalArgumentException while handling " + key + ", this request is ignored");
e.printStackTrace();
}
}
System.out.println();
// Log out
parameters = new Vector<String>(1);
parameters.add(a_loginToken);
Boolean bool = (Boolean) a_rpcClient.execute("jira1.logout", parameters);
System.out.println("Logout successful: " + bool);
}
catch (XmlRpcException e)
{
e.printStackTrace();
}
}
// definition of the JIRA components that impacts code
static final String[] s_comp = { "Functional", "IT", "Performance", "Refactoring", "User Interface" };
static final HashSet<String> s_componentsImpactingCode = new HashSet<String>(Arrays.asList(s_comp));
private static boolean isImpactingCode(Map answer) {
Object[] components = (Object[])answer.get("components");
for (Object component: components){
final Map entry = (Map) component;
// each map has the following entries
// - id
// - name
if ( s_componentsImpactingCode.contains(entry.get("name")) ) {
return true;
}
}
return false;
}
private static String getResolvedOn(Map answer) {
String resolvedOn = null;
Object[] customFieldValues = (Object[])answer.get("customFieldValues");
for (Object customFieldValue: customFieldValues){
final Map entry = (Map) customFieldValue;
// each map has the following entries
// - customfieldId
// - values
// - key (this one is optional and I don’t know it use)
if ( entry.get("customfieldId").equals(FIELD_ID_FIXED_IN_BUILD) ) {
resolvedOn = (String)entry.get("values");
break;
}
}
return resolvedOn;
}
private boolean isAlreadyFlaggedAsCodeReviewed(String requestID) {
boolean isAlreadyFlaggedAsCodeReview = false;
try {
Vector<String> parameters = new Vector<String>(2);
parameters.add(a_loginToken);
parameters.add(requestID);
Object[] comments = (Object[])a_rpcClient.execute("jira1.getComments", parameters);
for (Object o: comments)
{
Map comment = (Map) o;
// each map has the following entries
// - id
// - author
// - body
// - created
// - updated
// - updateAuthor
String body = (String)comment.get("body");
if ( body.startsWith(CODE_REVIEW_COMMENT) ) {
isAlreadyFlaggedAsCodeReview = true;
break;
}
}
}
catch (XmlRpcException e)
{
System.err.println("failed to get comments of " + requestID);
e.printStackTrace();
}
return isAlreadyFlaggedAsCodeReview;
}
private void flagAsCodeReviewed(String requestID,
String reviewedBuild) {
try {
Vector<String> parameters = new Vector<String>(3);
parameters.add(a_loginToken);
parameters.add(requestID);
parameters.add(CODE_REVIEW_COMMENT + " on " + reviewedBuild);
Boolean bool = (Boolean)a_rpcClient.execute("jira1.addComment", parameters);
if ( bool.booleanValue() ) {
System.out.println(requestID+" is flagged as reviewed");
} else {
System.err.println("failed to comment " + requestID + " as reviewed");
}
}
catch (XmlRpcException e)
{
System.err.println("failed to comment " + requestID + " as reviewed");
e.printStackTrace();
}
}
private static int compareVersion(String versionA,
String versionB) {
String[] verA = versionA.split("\\.");
if ( verA.length != 4 ) throw new IllegalArgumentException(versionA + " is an illegal version number");
String[] verB = versionB.split("\\.");
if ( verB.length != 4 ) throw new IllegalArgumentException(versionB + " is an illegal version number");
for (int i=0; i<4; i++) {
int a = Integer.parseInt(verA[i]);
int b = Integer.parseInt(verB[i]);
if (a<b) return -1;
if (a>b) return 1;
}
return 0;
}
} |