From 69733230a8ea8bd6e415ef1ea00c1125e8d6aad1 Mon Sep 17 00:00:00 2001 From: gropp_volker Date: Fri, 28 Sep 2018 16:23:37 +0200 Subject: [PATCH] add filter for jira --- .../java/net/rcarz/jiraclient/Filter.java | 37 ++++- src/main/java/net/rcarz/jiraclient/Issue.java | 136 ++++++++++-------- .../java/net/rcarz/jiraclient/JiraClient.java | 51 ++++--- 3 files changed, 140 insertions(+), 84 deletions(-) diff --git a/src/main/java/net/rcarz/jiraclient/Filter.java b/src/main/java/net/rcarz/jiraclient/Filter.java index f2756f92..32eaecbc 100644 --- a/src/main/java/net/rcarz/jiraclient/Filter.java +++ b/src/main/java/net/rcarz/jiraclient/Filter.java @@ -1,11 +1,14 @@ package net.rcarz.jiraclient; -import net.sf.json.JSON; -import net.sf.json.JSONObject; - import java.net.URI; +import java.util.ArrayList; +import java.util.List; import java.util.Map; +import net.sf.json.JSON; +import net.sf.json.JSONArray; +import net.sf.json.JSONObject; + /** * Represens a Jira filter. */ @@ -18,8 +21,9 @@ public class Filter extends Resource { public Filter(RestClient restclient, JSONObject json) { super(restclient); - if (json != null) - deserialise(json); + if (json != null) { + deserialise(json); + } } private void deserialise(JSONObject json) { @@ -44,6 +48,29 @@ public String getName() { return name; } + public static List getAll(final RestClient restclient) throws JiraException { + JSON result = null; + + try { + URI uri = restclient.buildURI(getBaseUri() + "filter/"); + result = restclient.get(uri); + } catch (Exception ex) { + throw new JiraException("Failed to retrieve filters", ex); + } + + if (!(result instanceof JSONArray)) { + throw new JiraException("JSON payload is malformed"); + } + List filters = new ArrayList(); + for (int i = 0; i < ((JSONArray)result).size(); i++) { + if (!(((JSONArray)result).get(i) instanceof JSONObject)) { + throw new JiraException("JSON payload is malformed"); + } + filters.add(new Filter(restclient, (JSONObject) ((JSONArray)result).get(i))); + } + return filters; + } + public static Filter get(final RestClient restclient, final String id) throws JiraException { JSON result = null; diff --git a/src/main/java/net/rcarz/jiraclient/Issue.java b/src/main/java/net/rcarz/jiraclient/Issue.java index f3dfc4a1..8ae60e2d 100644 --- a/src/main/java/net/rcarz/jiraclient/Issue.java +++ b/src/main/java/net/rcarz/jiraclient/Issue.java @@ -23,16 +23,22 @@ import java.io.InputStream; import java.net.URI; import java.net.URISyntaxException; -import java.text.SimpleDateFormat; -import java.util.*; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.NoSuchElementException; + +import org.joda.time.DateTime; +import org.joda.time.format.DateTimeFormat; import net.rcarz.utils.WorklogUtils; import net.sf.json.JSON; import net.sf.json.JSONArray; import net.sf.json.JSONObject; -import org.joda.time.DateTime; -import org.joda.time.format.DateTimeFormat; -import org.joda.time.format.DateTimeFormatter; /** * Represents a JIRA issue. @@ -353,8 +359,9 @@ public void execute() throws JiraException { JSONObject fieldmap = new JSONObject(); JSONObject updatemap = new JSONObject(); - if (fields.size() == 0 && fieldOpers.size() == 0) - throw new JiraException("No fields were given for update"); + if (fields.size() == 0 && fieldOpers.size() == 0) { + throw new JiraException("No fields were given for update"); + } for (Map.Entry ent : fields.entrySet()) { Object newval = Field.toJson(ent.getKey(), ent.getValue(), editmeta); @@ -368,11 +375,13 @@ public void execute() throws JiraException { JSONObject req = new JSONObject(); - if (fieldmap.size() > 0) - req.put("fields", fieldmap); + if (fieldmap.size() > 0) { + req.put("fields", fieldmap); + } - if (updatemap.size() > 0) - req.put("update", updatemap); + if (updatemap.size() > 0) { + req.put("update", updatemap); + } try { restclient.put(getRestUri(key), req); @@ -395,8 +404,9 @@ public FluentUpdate field(String name, Object value) { } private FluentUpdate fieldOperation(String oper, String name, Object value) { - if (!fieldOpers.containsKey(name)) - fieldOpers.put(name, new ArrayList()); + if (!fieldOpers.containsKey(name)) { + fieldOpers.put(name, new ArrayList()); + } fieldOpers.get(name).add(new Field.Operation(oper, value)); return this; @@ -459,8 +469,9 @@ private Transition getTransition(String id, boolean isName) throws JiraException private void realExecute(Transition trans) throws JiraException { - if (trans == null || trans.getFields() == null) - throw new JiraException("Transition is missing fields"); + if (trans == null || trans.getFields() == null) { + throw new JiraException("Transition is missing fields"); + } JSONObject fieldmap = new JSONObject(); @@ -470,8 +481,9 @@ private void realExecute(Transition trans) throws JiraException { JSONObject req = new JSONObject(); - if (fieldmap.size() > 0) - req.put("fields", fieldmap); + if (fieldmap.size() > 0) { + req.put("fields", fieldmap); + } JSONObject t = new JSONObject(); t.put("id", Field.getString(trans.getId())); @@ -548,7 +560,7 @@ private static class IssueIterator implements Iterator { private Integer startAt; private List issues; private int total; - + public IssueIterator(RestClient restclient, String jql, String includedFields, String expandFields, Integer maxResults, Integer startAt) throws JiraException { @@ -559,8 +571,7 @@ public IssueIterator(RestClient restclient, String jql, String includedFields, this.maxResults = maxResults; this.startAt = startAt; } - - @Override + public boolean hasNext() { if (nextIssue != null) { return true; @@ -573,7 +584,6 @@ public boolean hasNext() { return nextIssue != null; } - @Override public Issue next() { if (! hasNext()) { throw new NoSuchElementException(); @@ -583,7 +593,6 @@ public Issue next() { return result; } - @Override public void remove() { throw new UnsupportedOperationException("Method remove() not support for class " + this.getClass().getName()); @@ -593,7 +602,7 @@ public void remove() { * Gets the next issue, returning null if none more available * Will ask the next set of issues from the server if the end * of the current list of issues is reached. - * + * * @return the next issue, null if none more available * @throws JiraException */ @@ -607,7 +616,7 @@ private Issue getNextIssue() throws JiraException { return currentPage.next(); } } - + // check if we need to get the next set of issues if (! currentPage.hasNext()) { currentPage = getNextIssues().iterator(); @@ -625,7 +634,7 @@ private Issue getNextIssue() throws JiraException { * Execute the query to get the next set of issues. * Also sets the startAt, maxMresults, total and issues fields, * so that the SearchResult can access them. - * + * * @return the next set of issues. * @throws JiraException */ @@ -650,9 +659,9 @@ private List getNextIssues() throws JiraException { throw new JiraException("JSON payload is malformed"); } - + Map map = (Map) result; - + this.startAt = Field.getInteger(map.get("startAt")); this.maxResults = Field.getInteger(map.get("maxResults")); this.total = Field.getInteger(map.get("total")); @@ -660,7 +669,7 @@ private List getNextIssues() throws JiraException { return issues; } } - + /** * Issue search results structure. * @@ -682,7 +691,7 @@ public static class SearchResult { public List issues = null; private IssueIterator issueIterator; - public SearchResult(RestClient restclient, String jql, String includedFields, + public SearchResult(RestClient restclient, String jql, String includedFields, String expandFields, Integer maxResults, Integer startAt) throws JiraException { this.issueIterator = new IssueIterator( @@ -703,7 +712,7 @@ public SearchResult(RestClient restclient, String jql, String includedFields, /** * All issues found. - * + * * @return All issues found. */ public Iterator iterator() { @@ -806,8 +815,9 @@ private static Object requireContent(Object content) { protected Issue(RestClient restclient, JSONObject json) { super(restclient); - if (json != null) - deserialise(json); + if (json != null) { + deserialise(json); + } } private void deserialise(JSONObject json) { @@ -818,8 +828,9 @@ private void deserialise(JSONObject json) { key = Field.getString(map.get("key")); fields = (Map)map.get("fields"); - if (fields == null) - return; + if (fields == null) { + return; + } assignee = Field.getResource(User.class, fields.get(Field.ASSIGNEE), restclient); attachments = Field.getResourceArray(Attachment.class, fields.get(Field.ATTACHMENT), restclient); @@ -877,23 +888,26 @@ public static JSONObject getCreateMetadata( throw new JiraException("Failed to retrieve issue metadata", ex); } - if (!(result instanceof JSONObject)) - throw new JiraException("JSON payload is malformed"); + if (!(result instanceof JSONObject)) { + throw new JiraException("JSON payload is malformed"); + } JSONObject jo = (JSONObject)result; if (jo.isNullObject() || !jo.containsKey("projects") || - !(jo.get("projects") instanceof JSONArray)) - throw new JiraException("Create metadata is malformed"); + !(jo.get("projects") instanceof JSONArray)) { + throw new JiraException("Create metadata is malformed"); + } List projects = Field.getResourceArray( Project.class, - (JSONArray)jo.get("projects"), + jo.get("projects"), restclient); - if (projects.isEmpty() || projects.get(0).getIssueTypes().isEmpty()) - throw new JiraException("Project '"+ project + "' or issue type '" + issueType + - "' missing from create metadata. Do you have enough permissions?"); + if (projects.isEmpty() || projects.get(0).getIssueTypes().isEmpty()) { + throw new JiraException("Project '"+ project + "' or issue type '" + issueType + + "' missing from create metadata. Do you have enough permissions?"); + } return projects.get(0).getIssueTypes().get(0).getFields(); } @@ -907,14 +921,16 @@ private JSONObject getEditMetadata() throws JiraException { throw new JiraException("Failed to retrieve issue metadata", ex); } - if (!(result instanceof JSONObject)) - throw new JiraException("JSON payload is malformed"); + if (!(result instanceof JSONObject)) { + throw new JiraException("JSON payload is malformed"); + } JSONObject jo = (JSONObject)result; if (jo.isNullObject() || !jo.containsKey("fields") || - !(jo.get("fields") instanceof JSONObject)) - throw new JiraException("Edit metadata is malformed"); + !(jo.get("fields") instanceof JSONObject)) { + throw new JiraException("Edit metadata is malformed"); + } return (JSONObject)jo.get("fields"); } @@ -935,8 +951,9 @@ public List getTransitions() throws JiraException { JSONObject jo = (JSONObject)result; if (jo.isNullObject() || !jo.containsKey("transitions") || - !(jo.get("transitions") instanceof JSONArray)) - throw new JiraException("Transition metadata is missing."); + !(jo.get("transitions") instanceof JSONArray)) { + throw new JiraException("Transition metadata is missing."); + } JSONArray transitions = (JSONArray) jo.get("transitions"); @@ -1021,11 +1038,11 @@ public void addAttachments(NewAttachment... attachments) throws JiraException { * @throws JiraException when the attachment removal fails */ public void removeAttachment(String attachmentId) throws JiraException { - + if (attachmentId == null) { throw new NullPointerException("attachmentId may not be null"); } - + try { restclient.delete(getBaseUri() + "attachment/" + attachmentId); } catch (Exception ex) { @@ -1092,12 +1109,15 @@ public Comment addComment(String body, String visType, String visName) */ public WorkLog addWorkLog(String comment, DateTime startDate, long timeSpentSeconds) throws JiraException { try { - if (comment == null) - throw new IllegalArgumentException("Invalid comment."); - if (startDate == null) - throw new IllegalArgumentException("Invalid start time."); - if (timeSpentSeconds < 60) // We do not add a worklog that duration is below a minute - throw new IllegalArgumentException("Time spent cannot be lower than 1 minute."); + if (comment == null) { + throw new IllegalArgumentException("Invalid comment."); + } + if (startDate == null) { + throw new IllegalArgumentException("Invalid start time."); + } + if (timeSpentSeconds < 60) { + throw new IllegalArgumentException("Time spent cannot be lower than 1 minute."); + } JSONObject req = new JSONObject(); req.put("comment", comment); @@ -1341,7 +1361,7 @@ public static Issue get(RestClient restclient, String key, final String included * * * @param maxResults if non-null, defines the maximum number of - * results that can be returned + * results that can be returned * * @param startAt if non-null, defines the first issue to * return @@ -1368,7 +1388,7 @@ public static SearchResult search(RestClient restclient, String jql, /** * Creates the URI to execute a jql search. - * + * * @param restclient * @param jql * @param includedFields diff --git a/src/main/java/net/rcarz/jiraclient/JiraClient.java b/src/main/java/net/rcarz/jiraclient/JiraClient.java index 276b8d7c..2758c2ed 100644 --- a/src/main/java/net/rcarz/jiraclient/JiraClient.java +++ b/src/main/java/net/rcarz/jiraclient/JiraClient.java @@ -47,7 +47,7 @@ public class JiraClient { * Creates a JIRA client. * * @param uri Base URI of the JIRA server - * @throws JiraException + * @throws JiraException */ public JiraClient(String uri) throws JiraException { this(null, uri, null); @@ -58,19 +58,19 @@ public JiraClient(String uri) throws JiraException { * * @param uri Base URI of the JIRA server * @param creds Credentials to authenticate with - * @throws JiraException + * @throws JiraException */ public JiraClient(String uri, ICredentials creds) throws JiraException { this(null, uri, creds); } - + /** * Creates an authenticated JIRA client with custom HttpClient. * * @param httpClient Custom HttpClient to be used * @param uri Base URI of the JIRA server * @param creds Credentials to authenticate with - * @throws JiraException + * @throws JiraException */ public JiraClient(HttpClient httpClient, String uri, ICredentials creds) throws JiraException { if (httpClient == null) { @@ -155,7 +155,7 @@ public Issue getIssue(String key, String includedFields) throws JiraException { *
  • summary,comment - include just the summary and comments
  • *
  • *all,-comment - include all fields
  • * - * + * * @param expand issue fields to expand when getting issue data * * @return an issue instance @@ -267,7 +267,7 @@ public Issue.SearchResult searchIssues(String jql, String includedFields, return searchIssues(jql, includedFields, expandFields, null, null); } - + /** * Search for issues with the given query and specify which fields to * retrieve. @@ -313,10 +313,10 @@ public Issue.SearchResult searchIssues(String jql, String includedFields, Intege *
  • summary,comment - include just the summary and comments
  • *
  • *all,-comment - include all fields
  • * - * + * * @param maxResults if non-null, defines the maximum number of - * results that can be returned - * + * results that can be returned + * * @param startAt if non-null, defines the first issue to * return * @@ -384,6 +384,15 @@ public Filter getFilter(final String id) throws JiraException { return Filter.get(restclient, id); } + /** + * Retrieve the jira filters + * @return The Jira filters + * @throws JiraException + */ + public List getFilters() throws JiraException { + return Filter.getAll(restclient); + } + /** * * @return a list of all priorities available in the Jira installation @@ -419,7 +428,7 @@ public List getPriorities() throws JiraException { * @throws JiraException when the search fails */ public List getCustomFieldAllowedValues(String field, String project, String issueType) throws JiraException { - JSONObject createMetadata = (JSONObject) Issue.getCreateMetadata(restclient, project, issueType); + JSONObject createMetadata = Issue.getCreateMetadata(restclient, project, issueType); JSONObject fieldMetadata = (JSONObject) createMetadata.get(field); List customFieldOptions = Field.getResourceArray( CustomFieldOption.class, @@ -440,7 +449,7 @@ public List getCustomFieldAllowedValues(String field, String * @throws JiraException when the search fails */ public List getComponentsAllowedValues(String project, String issueType) throws JiraException { - JSONObject createMetadata = (JSONObject) Issue.getCreateMetadata(restclient, project, issueType); + JSONObject createMetadata = Issue.getCreateMetadata(restclient, project, issueType); JSONObject fieldMetadata = (JSONObject) createMetadata.get(Field.COMPONENTS); List componentOptions = Field.getResourceArray( Component.class, @@ -481,7 +490,7 @@ public List getProjects() throws JiraException { throw new JiraException(ex.getMessage(), ex); } } - + /** * Obtains information about a project, given its project key. * @param key the project key @@ -497,7 +506,7 @@ public Project getProject(String key) throws JiraException { throw new JiraException(ex.getMessage(), ex); } } - + /** * Obtains the list of all issue types in Jira. * @return all issue types @@ -520,7 +529,7 @@ public List getIssueTypes() throws JiraException { throw new JiraException(ex.getMessage(), ex); } } - + /** * Creates a new component in the given project. * @@ -531,20 +540,20 @@ public List getIssueTypes() throws JiraException { public Component.FluentCreate createComponent(String project) { return Component.create(restclient, project); } - + /** * Obtains a component given its ID. - * + * * @param id the component ID - * + * * @return the component - * + * * @throws JiraException failed to obtain the component */ public Component getComponent(String id) throws JiraException { return Component.get(restclient, id); } - + public ArrayList filterChangeLog(List histoy,String fields) { ArrayList result = new ArrayList(histoy.size()); fields = "," + fields + ","; @@ -590,8 +599,8 @@ public ArrayList getIssueChangeLog(Issue issue) throws JiraExcepti } else { response = getNextPortion(issue,changes.size()); } - } - + } + return changes; } catch (Exception ex) { throw new JiraException(ex.getMessage(), ex);