Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use IssueType object instead of IssueType name #221

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 44 additions & 8 deletions src/main/java/net/rcarz/jiraclient/Issue.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> params = new HashMap<String, String>();
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);
Expand All @@ -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();
Expand Down Expand Up @@ -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.
*
Expand Down
46 changes: 41 additions & 5 deletions src/main/java/net/rcarz/jiraclient/JiraClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -412,19 +428,39 @@ public List<Priority> 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<CustomFieldOption> getCustomFieldAllowedValues(String field, String project, String issueType) throws JiraException {
public List<CustomFieldOption> 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<CustomFieldOption> getCustomFieldAllowedValues(String field, String project, IssueType issueType) throws JiraException {
JSONObject createMetadata = (JSONObject) Issue.getCreateMetadata(restclient, project, issueType);
return convertCreateMetadataToCustomFieldOptions(field, createMetadata);
}

private List<CustomFieldOption> convertCreateMetadataToCustomFieldOptions(String field, JSONObject createMetadata) {
JSONObject fieldMetadata = (JSONObject) createMetadata.get(field);
List<CustomFieldOption> customFieldOptions = Field.getResourceArray(
CustomFieldOption.class,
fieldMetadata.get("allowedValues"),
restclient
restclient
);
return customFieldOptions;
}
Expand Down