use of com.atlassian.jira.rest.client.api.domain.CimFieldInfo in project opennms by OpenNMS.
the class JiraTicketerPlugin method populateFields.
/**
* Convenient method to populate additional fields in the {@link IssueInputBuilder}.
* The fields are read from {@link Ticket#getAttributes()}.
*
* @param ticket The ticket to read the attributes from.
* @param builder The builder to set additional fields.
*/
private void populateFields(Ticket ticket, IssueInputBuilder builder) {
// Only convert additional attributes to field values, if available
if (!ticket.hasAttributes()) {
return;
}
// List of fields already populated
final List<String> populatedFields = Lists.newArrayList();
final Collection<CimFieldInfo> fields = getFields();
for (Entry<String, String> eachEntry : ticket.getAttributes().entrySet()) {
if (!Strings.isNullOrEmpty(eachEntry.getValue())) {
// Find a field representation in jira
for (CimFieldInfo eachField : fields) {
if (eachEntry.getKey().equals(eachField.getId())) {
try {
final String attributeValue = eachEntry.getValue();
final Object mappedFieldValue = fieldMapFunctionCache.get(eachField.getSchema()).mapToFieldValue(eachField.getId(), eachField.getSchema(), attributeValue);
builder.setFieldValue(eachField.getId(), mappedFieldValue);
populatedFields.add(eachField.getId());
// we found a representation, now continue with next attribute
break;
} catch (Exception ex) {
LOG.error("Could not convert attribute (id={}, value={}) to jira field value. Ignoring attribute.", eachField.getId(), eachEntry.getValue(), ex);
}
}
}
}
}
// Inform about not found attributes
if (populatedFields.size() != ticket.getAttributes().size()) {
for (String eachKey : ticket.getAttributes().keySet()) {
if (!populatedFields.contains(eachKey)) {
LOG.warn("Ticket attribute '{}' is defined, but was not mapped to a (custom) field in JIRA. Attribute is skipped.", eachKey);
}
}
}
// Inform if required attribute has not been set
final List<CimFieldInfo> requiredFieldsNotSet = fields.stream().filter(CimFieldInfo::isRequired).filter(f -> !populatedFields.contains(f)).collect(Collectors.toList());
if (!requiredFieldsNotSet.isEmpty()) {
final String missingFields = requiredFieldsNotSet.stream().map(f -> String.format("id: %s, name: %s", f.getId(), f.getName())).collect(Collectors.joining(", "));
LOG.warn("Not all required (custom) jira fields have been set. The following are unset: {}", missingFields);
}
}
use of com.atlassian.jira.rest.client.api.domain.CimFieldInfo in project opennms by OpenNMS.
the class ListFieldsCommand method doExecute.
@Override
protected void doExecute(JiraRestClient jiraRestClient) throws Exception {
final StringBuilder info = new StringBuilder();
info.append("Fetching ");
info.append(showAll ? "custom" : "all");
info.append(" fields for ");
info.append(Strings.isNullOrEmpty(projectKey) ? "all projects" : "project with key '" + projectKey + "'");
info.append(Strings.isNullOrEmpty(issueTypeName) ? " and all issue types" : " and issue type with name '" + issueTypeName + "'");
info.append(".");
System.out.println(info.toString());
System.out.println();
// Fetch data
final Iterable<CimProject> cimProjects = JiraClientUtils.getIssueMetaData(jiraRestClient, "projects.issuetypes.fields", issueTypeName, projectKey);
// If we have fields, show them, otherwise bail out
final Iterator<CimProject> iterator = cimProjects.iterator();
if (!iterator.hasNext()) {
System.out.println("No fields found. The user making the ReST call may not have sufficient permissions.");
return;
}
// We found fields, so show them
final String ISSUE_ROW_FORMAT = "%s fields for issue type %s (id: %s)";
final String FIELD_ROW_FORMAT = "%-30s %-20s %-10s %-10s %s";
while (iterator.hasNext()) {
CimProject project = iterator.next();
System.out.println("Project " + project.getName() + " (" + project.getKey() + ")");
System.out.println(LINE);
// No types found
Iterator<CimIssueType> issueIt = project.getIssueTypes().iterator();
if (!issueIt.hasNext()) {
System.out.println("No issue types found");
continue;
}
// List each issue type
while (issueIt.hasNext()) {
final CimIssueType issue = issueIt.next();
final Map<String, CimFieldInfo> fieldsMap = filter(issue.getFields(), showAll);
System.out.println();
System.out.println(String.format(ISSUE_ROW_FORMAT, showAll ? "All" : "Custom", issue.getName(), issue.getId()));
System.out.println(LINE + LINE);
if (fieldsMap.isEmpty()) {
System.out.println("No fields found");
continue;
}
// Sort by name
final List<CimFieldInfo> fields = fieldsMap.values().stream().sorted(Comparator.comparing(CimFieldInfo::getName)).collect(Collectors.toList());
System.out.println(String.format(FIELD_ROW_FORMAT, "Name", "Id", "Required", "Custom", "Type"));
for (CimFieldInfo eachField : fields) {
System.out.println(String.format(FIELD_ROW_FORMAT, eachField.getName(), eachField.getId(), eachField.isRequired(), isCustom(eachField), eachField.getSchema().getType()));
}
}
}
}
Aggregations