1
0
Fork 0

minor clean up and retab

master
Bob Carroll 2015-01-25 19:17:10 -08:00
parent c6c9f7ec5d
commit 9095944c98
6 changed files with 128 additions and 147 deletions

View File

@ -63,11 +63,11 @@ public class BasicCredentials implements ICredentials {
return username; return username;
} }
@Override @Override
public void initialize(RestClient client) throws JiraException { public void initialize(RestClient client) throws JiraException {
} }
@Override @Override
public void logout(RestClient client) throws JiraException { public void logout(RestClient client) throws JiraException {
} }

View File

@ -143,24 +143,13 @@ public class Issue extends Resource {
} }
/** /**
* Search for issues with the given query and specify which fields to * count issues with the given query.
* retrieve.
* *
* @param restclient REST client instance * @param restclient REST client instance
* *
* @param jql JQL statement * @param jql JQL statement
* *
* @param includedFields Specifies which issue fields will be included in * @return the count
* the result.
* <br>Some examples how this parameter works:
* <ul>
* <li>*all - include all fields</li>
* <li>*navigable - include just navigable fields</li>
* <li>summary,comment - include just the summary and comments</li>
* <li>*all,-comment - include all fields</li>
* </ul>
*
* @return a search result structure with results
* *
* @throws JiraException when the search fails * @throws JiraException when the search fails
*/ */
@ -173,7 +162,7 @@ public class Issue extends Resource {
put("jql", j); put("jql", j);
} }
}; };
queryParams.put("maxResults", "1"); queryParams.put("maxResults", "1");
URI searchUri = restclient.buildURI(getBaseUri() + "search", queryParams); URI searchUri = restclient.buildURI(getBaseUri() + "search", queryParams);
result = restclient.get(searchUri); result = restclient.get(searchUri);
} catch (Exception ex) { } catch (Exception ex) {

View File

@ -12,12 +12,12 @@ import net.sf.json.JSONObject;
public class IssueHistory extends Resource { public class IssueHistory extends Resource {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private User user; private User user;
private ArrayList<IssueHistoryItem> changes; private ArrayList<IssueHistoryItem> changes;
private Date created; private Date created;
/** /**
* Creates a priority from a JSON payload. * Creates an issue history record from a JSON payload.
* *
* @param restclient REST client instance * @param restclient REST client instance
* @param json JSON payload * @param json JSON payload
@ -29,23 +29,17 @@ public class IssueHistory extends Resource {
deserialise(restclient,json); deserialise(restclient,json);
} }
} }
/*
{"id":"150478","author":{"self":"http://jira.devops.northplains.com:8080/rest/api/2/user?username=rbagga","name":"rbagga","emailAddress":"rbagga@northplains.com","avatarUrls":{"16x16":"http://jira.devops.northplains.com:8080/secure/useravatar?size=small&avatarId=10113","48x48":"http://jira.devops.northplains.com:8080/secure/useravatar?avatarId=10113"},
"displayName":"Rajpaul Bagga","active":true},
"created":"2013-09-25T08:44:19.250-0400",
"items":[...]}
*/
public IssueHistory(IssueHistory record, ArrayList<IssueHistoryItem> changes) { public IssueHistory(IssueHistory record, ArrayList<IssueHistoryItem> changes) {
super(record.restclient); super(record.restclient);
user = record.user; user = record.user;
id = record.id; id = record.id;
self = record.self; self = record.self;
created = record.created; created = record.created;
this.changes = changes; this.changes = changes;
} }
private void deserialise(RestClient restclient, JSONObject json) { private void deserialise(RestClient restclient, JSONObject json) {
Map map = json; Map map = json;
self = Field.getString(map.get("self")); self = Field.getString(map.get("self"));
id = Field.getString(map.get("id")); id = Field.getString(map.get("id"));
@ -59,16 +53,16 @@ public class IssueHistory extends Resource {
} }
} }
public User getUser() { public User getUser() {
return user; return user;
} }
public ArrayList<IssueHistoryItem> getChanges() { public ArrayList<IssueHistoryItem> getChanges() {
return changes; return changes;
} }
public Date getCreated() { public Date getCreated() {
return created; return created;
} }
} }

View File

@ -6,21 +6,18 @@ import net.sf.json.JSONObject;
public class IssueHistoryItem extends Resource { public class IssueHistoryItem extends Resource {
private String field; private String field;
private String from; private String from;
private String to; private String to;
private String fromStr; private String fromStr;
private String toStr; private String toStr;
/* public IssueHistoryItem(RestClient restclient) {
* {"field":"assignee","fieldtype":"jira","from":"rbagga","fromString":"Rajpaul Bagga","to":"GGadyatskiy","toString":"Gleb Gadyatskiy"} super(restclient);
*/
public IssueHistoryItem(RestClient restclient) {
super(restclient);
} }
public IssueHistoryItem(RestClient restclient, JSONObject json) { public IssueHistoryItem(RestClient restclient, JSONObject json) {
this(restclient); this(restclient);
if (json != null) { if (json != null) {
deserialise(restclient,json); deserialise(restclient,json);
} }
@ -37,24 +34,24 @@ public class IssueHistoryItem extends Resource {
toStr = Field.getString(map.get("toString")); toStr = Field.getString(map.get("toString"));
} }
public String getField() { public String getField() {
return field; return field;
} }
public String getFrom() { public String getFrom() {
return from; return from;
} }
public String getTo() { public String getTo() {
return to; return to;
} }
public String getFromStr() { public String getFromStr() {
return fromStr; return fromStr;
} }
public String getToStr() { public String getToStr() {
return toStr; return toStr;
} }
} }

View File

@ -148,10 +148,12 @@ public class JiraClient {
* *
* @param jql JQL statement * @param jql JQL statement
* *
* @return the count
*
* @throws JiraException when the search fails * @throws JiraException when the search fails
*/ */
public int countIssues(String jql) throws JiraException { public int countIssues(String jql) throws JiraException {
return Issue.count(restclient, jql); return Issue.count(restclient, jql);
} }
/** /**
@ -341,7 +343,8 @@ public class JiraClient {
return Issue.search(restclient, jql, includedFields, expandFields, maxResults, return Issue.search(restclient, jql, includedFields, expandFields, maxResults,
startAt); startAt);
} }
/**
/**
* *
* @return a list of all priorities available in the Jira installation * @return a list of all priorities available in the Jira installation
* @throws JiraException * @throws JiraException
@ -502,71 +505,69 @@ public class JiraClient {
return Component.get(restclient, id); return Component.get(restclient, id);
} }
/** public ArrayList<IssueHistory> filterChangeLog(List<IssueHistory> histoy,String fields) {
* ArrayList<IssueHistory> result = new ArrayList<IssueHistory>(histoy.size());
* @return a list of all priorities available in the Jira installation fields = "," + fields + ",";
* @throws JiraException
*/
public ArrayList<IssueHistory> filterChangeLog(List<IssueHistory> histoy,String fields) {
ArrayList<IssueHistory> result = new ArrayList<IssueHistory>(histoy.size());
fields = ","+fields+",";
for (IssueHistory record : histoy) {
ArrayList<IssueHistoryItem> list = new ArrayList<IssueHistoryItem>(record.getChanges().size());
for (IssueHistoryItem item : record.getChanges()) {
if (fields.contains(item.getField())) {
list.add(item);
}
}
if (list.size()>0) {
result.add(new IssueHistory(record,list));
}
}
return result;
}
/**
*
* @return a list of all priorities available in the Jira installation
* @throws JiraException
*/
public ArrayList<IssueHistory> getIssueChangeLog(Issue issue) throws JiraException {
try {
ArrayList<IssueHistory> changes = null;
JSON response = getNextPortion(issue,0);
while (true) {
JSONObject object = JSONObject.fromObject(response);
Object opers = object.get("changelog");
object = JSONObject.fromObject(opers);
Integer totalObj = (Integer)object.get("total");
JSONArray histories = JSONArray.fromObject(object.get("histories"));
if (changes == null) {
changes = new ArrayList<IssueHistory>(totalObj);
}
for (int i = 0; i < histories.size(); i++) {
JSONObject p = histories.getJSONObject(i);
changes.add(new IssueHistory(restclient, p));
}
if (changes.size()>=totalObj) {
break;
} else {
response = getNextPortion(issue,changes.size());
}
}
return changes;
} catch (Exception ex) {
throw new JiraException(ex.getMessage(), ex);
}
}
private JSON getNextPortion(Issue issue,Integer startAt) throws URISyntaxException, RestException, IOException { for (IssueHistory record : histoy) {
Map<String,String> params = new HashMap<String, String>(); ArrayList<IssueHistoryItem> list = new ArrayList<IssueHistoryItem>(record.getChanges().size());
if(startAt != null){ for (IssueHistoryItem item : record.getChanges()) {
params.put("startAt", String.valueOf(startAt)); if (fields.contains(item.getField())) {
} list.add(item);
params.put("expand","changelog.fields"); }
URI uri = restclient.buildURI(Issue.getBaseUri()+"issue/"+issue.id,params); }
return restclient.get(uri);
if (list.size() > 0) {
result.add(new IssueHistory(record,list));
}
}
return result;
} }
public ArrayList<IssueHistory> getIssueChangeLog(Issue issue) throws JiraException {
try {
ArrayList<IssueHistory> changes = null;
JSON response = getNextPortion(issue, 0);
while (true) {
JSONObject object = JSONObject.fromObject(response);
Object opers = object.get("changelog");
object = JSONObject.fromObject(opers);
Integer totalObj = (Integer)object.get("total");
JSONArray histories = JSONArray.fromObject(object.get("histories"));
if (changes == null) {
changes = new ArrayList<IssueHistory>(totalObj);
}
for (int i = 0; i < histories.size(); i++) {
JSONObject p = histories.getJSONObject(i);
changes.add(new IssueHistory(restclient, p));
}
if (changes.size() >= totalObj) {
break;
} else {
response = getNextPortion(issue,changes.size());
}
}
return changes;
} catch (Exception ex) {
throw new JiraException(ex.getMessage(), ex);
}
}
private JSON getNextPortion(Issue issue, Integer startAt)
throws URISyntaxException, RestException, IOException {
Map<String, String> params = new HashMap<String, String>();
if (startAt != null) {
params.put("startAt", String.valueOf(startAt));
}
params.put("expand","changelog.fields");
URI uri = restclient.buildURI(Issue.getBaseUri() + "issue/" + issue.id, params);
return restclient.get(uri);
}
} }

View File

@ -45,18 +45,18 @@ public class TokenCredentials implements ICredentials {
} }
public TokenCredentials(String jsessionId) { public TokenCredentials(String jsessionId) {
token = jsessionId; token = jsessionId;
} }
/** /**
* Sets the Authorization header for the given request. * Sets the Authorization header for the given request.
* *
* @param req HTTP request to authenticate * @param req HTTP request to authenticate
*/ */
public void authenticate(HttpRequest req) { public void authenticate(HttpRequest req) {
if (token != null) { if (token != null) {
req.addHeader("Cookie","JSESSIONID="+token+";"); req.addHeader("Cookie","JSESSIONID="+token+";");
} }
} }
/** /**
@ -68,34 +68,34 @@ public class TokenCredentials implements ICredentials {
return username; return username;
} }
@Override @Override
public void initialize(RestClient client) throws JiraException { public void initialize(RestClient client) throws JiraException {
if (token==null) { if (token==null) {
try { try {
JSONObject req = new JSONObject(); JSONObject req = new JSONObject();
req.put("username", username); req.put("username", username);
req.put("password", password); req.put("password", password);
JSON json = client.post(Resource.getAuthUri() + "session", req); JSON json = client.post(Resource.getAuthUri() + "session", req);
System.out.println(json.toString()); System.out.println(json.toString());
} catch (Exception ex) { } catch (Exception ex) {
throw new JiraException("Failed to login", ex); throw new JiraException("Failed to login", ex);
} }
} }
} }
@Override @Override
public void logout(RestClient client) throws JiraException { public void logout(RestClient client) throws JiraException {
if (token != null) { if (token != null) {
try { try {
client.delete(Resource.getAuthUri() + "session"); client.delete(Resource.getAuthUri() + "session");
} catch (Exception e) { } catch (Exception e) {
throw new JiraException("Failed to logout", e); throw new JiraException("Failed to logout", e);
} }
} }
} }
public String getToken() { public String getToken() {
return token; return token;
} }
} }