Search in sources :

Example 1 with IssueInputBuilder

use of com.atlassian.jira.rest.client.api.domain.input.IssueInputBuilder 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 IssueInputBuilder

use of com.atlassian.jira.rest.client.api.domain.input.IssueInputBuilder in project tutorials by eugenp.

the class MyJiraClient method createIssue.

private String createIssue(String projectKey, Long issueType, String issueSummary) {
    IssueRestClient issueClient = restClient.getIssueClient();
    IssueInput newIssue = new IssueInputBuilder(projectKey, issueType, issueSummary).build();
    return issueClient.createIssue(newIssue).claim().getKey();
}
Also used : IssueInput(com.atlassian.jira.rest.client.api.domain.input.IssueInput) IssueRestClient(com.atlassian.jira.rest.client.api.IssueRestClient) IssueInputBuilder(com.atlassian.jira.rest.client.api.domain.input.IssueInputBuilder)

Example 3 with IssueInputBuilder

use of com.atlassian.jira.rest.client.api.domain.input.IssueInputBuilder in project opennms by OpenNMS.

the class JiraTicketerPlugin method saveOrUpdateInternal.

private void saveOrUpdateInternal(Ticket ticket, JiraRestClient jira) throws PluginException {
    Config config = getConfig();
    if (ticket.getId() == null || ticket.getId().equals("")) {
        // If we can't find a ticket with the specified ID then create one.
        IssueInputBuilder builder = new IssueInputBuilder(config.getProjectKey(), config.getIssueTypeId());
        builder.setReporterName(config.getUsername());
        builder.setSummary(ticket.getSummary());
        builder.setDescription(ticket.getDetails());
        populateFields(ticket, builder);
        BasicIssue createdIssue;
        try {
            createdIssue = jira.getIssueClient().createIssue(builder.build()).get();
        } catch (InterruptedException | ExecutionException e) {
            throw new PluginException("Failed to create issue.", e);
        }
        LOG.info("created ticket " + createdIssue);
        ticket.setId(createdIssue.getKey());
    } else {
        // Otherwise update the existing ticket
        LOG.info("Received ticket: {}", ticket.getId());
        Issue issue;
        try {
            issue = jira.getIssueClient().getIssue(ticket.getId()).get();
        } catch (InterruptedException | ExecutionException e) {
            throw new PluginException("Failed to get issue with id:" + ticket.getId(), e);
        }
        Iterable<Transition> transitions;
        try {
            transitions = jira.getIssueClient().getTransitions(issue).get();
        } catch (InterruptedException | ExecutionException e) {
            throw new PluginException("Failed to get transitions for issue with id:" + issue.getId(), e);
        }
        if (Ticket.State.CLOSED.equals(ticket.getState())) {
            Comment comment = Comment.valueOf("Issue resolved by OpenNMS.");
            for (Transition transition : transitions) {
                if (config.getResolveTransitionName().equals(transition.getName())) {
                    LOG.info("Resolving ticket {}", ticket.getId());
                    // Resolve the issue
                    try {
                        jira.getIssueClient().transition(issue, new TransitionInput(transition.getId(), comment)).get();
                    } catch (InterruptedException | ExecutionException e) {
                        throw new PluginException("Failed to get resolve issue with id:" + issue.getId(), e);
                    }
                    return;
                }
            }
            LOG.warn("Could not resolve ticket {}, no '{}' operation available.", ticket.getId(), getConfig().getResolveTransitionName());
        } else if (Ticket.State.OPEN.equals(ticket.getState())) {
            Comment comment = Comment.valueOf("Issue reopened by OpenNMS.");
            for (Transition transition : transitions) {
                if (getConfig().getReopentransitionName().equals(transition.getName())) {
                    LOG.info("Reopening ticket {}", ticket.getId());
                    // Resolve the issue
                    try {
                        jira.getIssueClient().transition(issue, new TransitionInput(transition.getId(), comment)).get();
                    } catch (InterruptedException | ExecutionException e) {
                        throw new PluginException("Failed to reopen issue with id:" + issue.getId(), e);
                    }
                    return;
                }
            }
            LOG.warn("Could not reopen ticket {}, no '{}' operation available.", ticket.getId(), getConfig().getReopentransitionName());
        }
    }
}
Also used : Comment(com.atlassian.jira.rest.client.api.domain.Comment) TransitionInput(com.atlassian.jira.rest.client.api.domain.input.TransitionInput) Issue(com.atlassian.jira.rest.client.api.domain.Issue) BasicIssue(com.atlassian.jira.rest.client.api.domain.BasicIssue) BasicIssue(com.atlassian.jira.rest.client.api.domain.BasicIssue) PluginException(org.opennms.api.integration.ticketing.PluginException) Transition(com.atlassian.jira.rest.client.api.domain.Transition) IssueInputBuilder(com.atlassian.jira.rest.client.api.domain.input.IssueInputBuilder) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with IssueInputBuilder

use of com.atlassian.jira.rest.client.api.domain.input.IssueInputBuilder in project tutorials by eugenp.

the class MyJiraClient method updateIssueDescription.

private void updateIssueDescription(String issueKey, String newDescription) {
    IssueInput input = new IssueInputBuilder().setDescription(newDescription).build();
    restClient.getIssueClient().updateIssue(issueKey, input).claim();
}
Also used : IssueInput(com.atlassian.jira.rest.client.api.domain.input.IssueInput) IssueInputBuilder(com.atlassian.jira.rest.client.api.domain.input.IssueInputBuilder)

Aggregations

IssueInputBuilder (com.atlassian.jira.rest.client.api.domain.input.IssueInputBuilder)4 BasicIssue (com.atlassian.jira.rest.client.api.domain.BasicIssue)2 Comment (com.atlassian.jira.rest.client.api.domain.Comment)2 Issue (com.atlassian.jira.rest.client.api.domain.Issue)2 Transition (com.atlassian.jira.rest.client.api.domain.Transition)2 IssueInput (com.atlassian.jira.rest.client.api.domain.input.IssueInput)2 TransitionInput (com.atlassian.jira.rest.client.api.domain.input.TransitionInput)2 ExecutionException (java.util.concurrent.ExecutionException)2 PluginException (org.opennms.api.integration.ticketing.PluginException)2 IssueRestClient (com.atlassian.jira.rest.client.api.IssueRestClient)1 JiraRestClient (com.atlassian.jira.rest.client.api.JiraRestClient)1 CimFieldInfo (com.atlassian.jira.rest.client.api.domain.CimFieldInfo)1 CimProject (com.atlassian.jira.rest.client.api.domain.CimProject)1 FieldSchema (com.atlassian.jira.rest.client.api.domain.FieldSchema)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