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;
}
@Override
@Override
public void initialize(RestClient client) throws JiraException {
}
@Override
@Override
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
* retrieve.
* count issues with the given query.
*
* @param restclient REST client instance
*
* @param jql JQL statement
*
* @param includedFields Specifies which issue fields will be included in
* 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
* @return the count
*
* @throws JiraException when the search fails
*/
@ -173,7 +162,7 @@ public class Issue extends Resource {
put("jql", j);
}
};
queryParams.put("maxResults", "1");
queryParams.put("maxResults", "1");
URI searchUri = restclient.buildURI(getBaseUri() + "search", queryParams);
result = restclient.get(searchUri);
} catch (Exception ex) {

View File

@ -12,12 +12,12 @@ import net.sf.json.JSONObject;
public class IssueHistory extends Resource {
private static final long serialVersionUID = 1L;
private User user;
private ArrayList<IssueHistoryItem> changes;
private Date created;
private User user;
private ArrayList<IssueHistoryItem> changes;
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 json JSON payload
@ -29,23 +29,17 @@ public class IssueHistory extends Resource {
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) {
super(record.restclient);
user = record.user;
id = record.id;
self = record.self;
created = record.created;
this.changes = changes;
super(record.restclient);
user = record.user;
id = record.id;
self = record.self;
created = record.created;
this.changes = changes;
}
private void deserialise(RestClient restclient, JSONObject json) {
private void deserialise(RestClient restclient, JSONObject json) {
Map map = json;
self = Field.getString(map.get("self"));
id = Field.getString(map.get("id"));
@ -59,16 +53,16 @@ public class IssueHistory extends Resource {
}
}
public User getUser() {
return user;
public User getUser() {
return user;
}
public ArrayList<IssueHistoryItem> getChanges() {
return changes;
public ArrayList<IssueHistoryItem> getChanges() {
return changes;
}
public Date getCreated() {
return created;
public Date getCreated() {
return created;
}
}

View File

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

View File

@ -148,10 +148,12 @@ public class JiraClient {
*
* @param jql JQL statement
*
* @return the count
*
* @throws JiraException when the search fails
*/
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,
startAt);
}
/**
/**
*
* @return a list of all priorities available in the Jira installation
* @throws JiraException
@ -502,71 +505,69 @@ public class JiraClient {
return Component.get(restclient, id);
}
/**
*
* @return a list of all priorities available in the Jira installation
* @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);
}
}
public ArrayList<IssueHistory> filterChangeLog(List<IssueHistory> histoy,String fields) {
ArrayList<IssueHistory> result = new ArrayList<IssueHistory>(histoy.size());
fields = "," + fields + ",";
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);
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;
}
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) {
token = jsessionId;
token = jsessionId;
}
/**
/**
* Sets the Authorization header for the given request.
*
* @param req HTTP request to authenticate
*/
public void authenticate(HttpRequest req) {
if (token != null) {
if (token != null) {
req.addHeader("Cookie","JSESSIONID="+token+";");
}
}
}
/**
@ -68,34 +68,34 @@ public class TokenCredentials implements ICredentials {
return username;
}
@Override
@Override
public void initialize(RestClient client) throws JiraException {
if (token==null) {
if (token==null) {
try {
JSONObject req = new JSONObject();
req.put("username", username);
req.put("password", password);
JSON json = client.post(Resource.getAuthUri() + "session", req);
System.out.println(json.toString());
JSONObject req = new JSONObject();
req.put("username", username);
req.put("password", password);
JSON json = client.post(Resource.getAuthUri() + "session", req);
System.out.println(json.toString());
} catch (Exception ex) {
throw new JiraException("Failed to login", ex);
}
}
}
}
@Override
@Override
public void logout(RestClient client) throws JiraException {
if (token != null) {
try {
client.delete(Resource.getAuthUri() + "session");
if (token != null) {
try {
client.delete(Resource.getAuthUri() + "session");
} catch (Exception e) {
throw new JiraException("Failed to logout", e);
}
}
}
}
}
public String getToken() {
return token;
public String getToken() {
return token;
}
}