Search in sources :

Example 1 with CimFieldInfo

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);
    }
}
Also used : CimFieldInfo(com.atlassian.jira.rest.client.api.domain.CimFieldInfo) LoadingCache(com.google.common.cache.LoadingCache) Issue(com.atlassian.jira.rest.client.api.domain.Issue) LoggerFactory(org.slf4j.LoggerFactory) Plugin(org.opennms.api.integration.ticketing.Plugin) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Ticket(org.opennms.api.integration.ticketing.Ticket) Strings(com.google.common.base.Strings) PluginException(org.opennms.api.integration.ticketing.PluginException) Lists(com.google.common.collect.Lists) FieldMapper(org.opennms.netmgt.ticketer.jira.fieldmapper.FieldMapper) Map(java.util.Map) Transition(com.atlassian.jira.rest.client.api.domain.Transition) FieldSchema(com.atlassian.jira.rest.client.api.domain.FieldSchema) Properties(java.util.Properties) Logger(org.slf4j.Logger) BasicIssue(com.atlassian.jira.rest.client.api.domain.BasicIssue) JiraRestClient(com.atlassian.jira.rest.client.api.JiraRestClient) Collection(java.util.Collection) Set(java.util.Set) IOException(java.io.IOException) IssueInputBuilder(com.atlassian.jira.rest.client.api.domain.input.IssueInputBuilder) TransitionInput(com.atlassian.jira.rest.client.api.domain.input.TransitionInput) FileInputStream(java.io.FileInputStream) Collectors(java.util.stream.Collectors) File(java.io.File) Sets(com.google.common.collect.Sets) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) CacheLoader(com.google.common.cache.CacheLoader) List(java.util.List) TimeoutRefreshPolicy(org.opennms.netmgt.ticketer.jira.cache.TimeoutRefreshPolicy) Entry(java.util.Map.Entry) Comment(com.atlassian.jira.rest.client.api.domain.Comment) Cache(org.opennms.netmgt.ticketer.jira.cache.Cache) CimProject(com.atlassian.jira.rest.client.api.domain.CimProject) CacheBuilder(com.google.common.cache.CacheBuilder) InputStream(java.io.InputStream) CimFieldInfo(com.atlassian.jira.rest.client.api.domain.CimFieldInfo) PluginException(org.opennms.api.integration.ticketing.PluginException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 2 with CimFieldInfo

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()));
            }
        }
    }
}
Also used : CimProject(com.atlassian.jira.rest.client.api.domain.CimProject) CimFieldInfo(com.atlassian.jira.rest.client.api.domain.CimFieldInfo) CimIssueType(com.atlassian.jira.rest.client.api.domain.CimIssueType)

Aggregations

CimFieldInfo (com.atlassian.jira.rest.client.api.domain.CimFieldInfo)2 CimProject (com.atlassian.jira.rest.client.api.domain.CimProject)2 JiraRestClient (com.atlassian.jira.rest.client.api.JiraRestClient)1 BasicIssue (com.atlassian.jira.rest.client.api.domain.BasicIssue)1 CimIssueType (com.atlassian.jira.rest.client.api.domain.CimIssueType)1 Comment (com.atlassian.jira.rest.client.api.domain.Comment)1 FieldSchema (com.atlassian.jira.rest.client.api.domain.FieldSchema)1 Issue (com.atlassian.jira.rest.client.api.domain.Issue)1 Transition (com.atlassian.jira.rest.client.api.domain.Transition)1 IssueInputBuilder (com.atlassian.jira.rest.client.api.domain.input.IssueInputBuilder)1 TransitionInput (com.atlassian.jira.rest.client.api.domain.input.TransitionInput)1 Strings (com.google.common.base.Strings)1 CacheBuilder (com.google.common.cache.CacheBuilder)1 CacheLoader (com.google.common.cache.CacheLoader)1 LoadingCache (com.google.common.cache.LoadingCache)1 Lists (com.google.common.collect.Lists)1 Sets (com.google.common.collect.Sets)1 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 IOException (java.io.IOException)1