diff --git a/src/main/java/net/rcarz/jiraclient/Issue.java b/src/main/java/net/rcarz/jiraclient/Issue.java index f3dfc4a1..8e1bb4f8 100644 --- a/src/main/java/net/rcarz/jiraclient/Issue.java +++ b/src/main/java/net/rcarz/jiraclient/Issue.java @@ -858,17 +858,31 @@ private static String getRestUri(String key) { } public static JSONObject getCreateMetadata( - RestClient restclient, String project, String issueType) throws JiraException { + RestClient restclient, String project, final String issueTypeName) throws JiraException { + return getCreateMetadata(restclient, project, null, issueTypeName); + } + + public static JSONObject getCreateMetadata( + RestClient restclient, String project, final IssueType issueType) throws JiraException { + return getCreateMetadata(restclient, project, issueType.getId(), null); + } + + private static JSONObject getCreateMetadata( + RestClient restclient, String project, final String issueTypeId, final String issueTypeName) throws JiraException { final String pval = project; - final String itval = issueType; JSON result = null; try { Map params = new HashMap(); params.put("expand", "projects.issuetypes.fields"); params.put("projectKeys", pval); - params.put("issuetypeNames", itval); + if (issueTypeId != null) { + params.put("issuetypeIds", issueTypeId); + } + if (issueTypeName != null) { + params.put("issuetypeNames", issueTypeName); + } URI createuri = restclient.buildURI( getBaseUri() + "issue/createmeta", params); @@ -892,7 +906,7 @@ public static JSONObject getCreateMetadata( restclient); if (projects.isEmpty() || projects.get(0).getIssueTypes().isEmpty()) - throw new JiraException("Project '"+ project + "' or issue type '" + issueType + + throw new JiraException("Project '"+ project + "' or issue type '" + issueTypeName + "' missing from create metadata. Do you have enough permissions?"); return projects.get(0).getIssueTypes().get(0).getFields(); @@ -1192,24 +1206,46 @@ public void link(String issue, String type, String body, String visType, String * * @param restclient REST client instance * @param project Key of the project to create the issue in - * @param issueType Name of the issue type to create + * @param issueTypeName Name of the issue type to create * * @return a fluent create instance * * @throws JiraException when the client fails to retrieve issue metadata */ - public static FluentCreate create(RestClient restclient, String project, String issueType) + public static FluentCreate create(RestClient restclient, String project, String issueTypeName) throws JiraException { FluentCreate fc = new FluentCreate( restclient, - getCreateMetadata(restclient, project, issueType)); + getCreateMetadata(restclient, project, issueTypeName)); return fc .field(Field.PROJECT, project) - .field(Field.ISSUE_TYPE, issueType); + .field(Field.ISSUE_TYPE, issueTypeName); } + /** + * Creates a new JIRA issue. + * + * @param restclient REST client instance + * @param project Key of the project to create the issue in + * @param issueType The type of issue to create + * + * @return a fluent create instance + * + * @throws JiraException when the client fails to retrieve issue metadata + */ + public static FluentCreate create(RestClient restclient, String project, IssueType issueType) + throws JiraException { + + FluentCreate fc = new FluentCreate( + restclient, + getCreateMetadata(restclient, project, issueType)); + + return fc + .field(Field.PROJECT, project) + .field(Field.ISSUE_TYPE, issueType); + } /** * Creates a new sub-task. * diff --git a/src/main/java/net/rcarz/jiraclient/JiraClient.java b/src/main/java/net/rcarz/jiraclient/JiraClient.java index 276b8d7c..c4e04371 100644 --- a/src/main/java/net/rcarz/jiraclient/JiraClient.java +++ b/src/main/java/net/rcarz/jiraclient/JiraClient.java @@ -93,13 +93,29 @@ public JiraClient(HttpClient httpClient, String uri, ICredentials creds) throws * Creates a new issue in the given project. * * @param project Key of the project to create in - * @param issueType Name of the issue type to create + * @param issueTypeName Name of the issue type to create * * @return a fluent create instance * * @throws JiraException when something goes wrong */ - public Issue.FluentCreate createIssue(String project, String issueType) + public Issue.FluentCreate createIssue(String project, String issueTypeName) + throws JiraException { + + return Issue.create(restclient, project, issueTypeName); + } + + /** + * Creates a new issue in the given project. + * + * @param project Key of the project to create in + * @param issueType Issue type to create + * + * @return a fluent create instance + * + * @throws JiraException when something goes wrong + */ + public Issue.FluentCreate createIssue(String project, IssueType issueType) throws JiraException { return Issue.create(restclient, project, issueType); @@ -412,19 +428,39 @@ public List getPriorities() throws JiraException { * * @param field field id * @param project Key of the project context - * @param issueType Name of the issue type + * @param issueTypeName Name of the issue type * * @return a search result structure with results * * @throws JiraException when the search fails */ - public List getCustomFieldAllowedValues(String field, String project, String issueType) throws JiraException { + public List getCustomFieldAllowedValues(String field, String project, String issueTypeName) throws JiraException { + JSONObject createMetadata = (JSONObject) Issue.getCreateMetadata(restclient, project, issueTypeName); + return convertCreateMetadataToCustomFieldOptions(field, createMetadata); + } + + /** + * Get a list of options for a custom field + * + * @param field field id + * @param project Key of the project context + * @param issueType Issue type + * + * @return a search result structure with results + * + * @throws JiraException when the search fails + */ + public List getCustomFieldAllowedValues(String field, String project, IssueType issueType) throws JiraException { JSONObject createMetadata = (JSONObject) Issue.getCreateMetadata(restclient, project, issueType); + return convertCreateMetadataToCustomFieldOptions(field, createMetadata); + } + + private List convertCreateMetadataToCustomFieldOptions(String field, JSONObject createMetadata) { JSONObject fieldMetadata = (JSONObject) createMetadata.get(field); List customFieldOptions = Field.getResourceArray( CustomFieldOption.class, fieldMetadata.get("allowedValues"), - restclient + restclient ); return customFieldOptions; }