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);
}
}
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();
}
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());
}
}
}
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();
}
Aggregations