public static void toInProgress(JiraRestClient restClient, Issue issue) { Iterable<Transition> transitions = null; Transition assignToRunTransition = null, inProgressTransition = null, reRunTransition = null; String statusName = issue.getStatus().getName(); System.out.println("status is " + statusName + "/" + issue.getTransitionsUri().toString()); if (statusName.equals("Create") || statusName.equals("Open") || statusName.equals("Reopened")) { // do nothing transitions = restClient.getIssueClient().getTransitions(issue.getTransitionsUri()).claim(); inProgressTransition = getTransitionByName(transitions, "Start Progress"); restClient .getIssueClient() .transition( issue.getTransitionsUri(), new TransitionInput(4 /*inProgressTransition.getId()*/)) .claim(); } else if (statusName.equals("In Progress")) { /*只能在这里获得, 因为状态不同, transition不同*/ } else if (statusName.equals("Resolved")) { transitions = restClient.getIssueClient().getTransitions(issue.getTransitionsUri()).claim(); inProgressTransition = getTransitionByName(transitions, "Reopen Issue"); restClient .getIssueClient() .transition(issue.getTransitionsUri(), new TransitionInput(inProgressTransition.getId())) .claim(); transitions = restClient.getIssueClient().getTransitions(issue.getTransitionsUri()).claim(); assignToRunTransition = getTransitionByName(transitions, "Start Progress"); restClient .getIssueClient() .transition(issue.getTransitionsUri(), new TransitionInput(assignToRunTransition.getId())) .claim(); } else if (statusName.equals("Closed")) { System.out.println(issue.getId() + ", " + statusName + ", closed, do nothing."); } else { /*状态修改过, 程序没有改动.*/ throw new RuntimeException("status error."); } }
public static Transition getTransitionByName( Iterable<Transition> transitions, String transitionName) { for (Transition transition : transitions) { String tname = transition.getName(); if (tname.equals(transitionName)) { return transition; } } return null; }
public static void toPassed(JiraRestClient restClient, Issue issue) { toInProgress(restClient, issue); /*必须在这里获得, 因为上面转换了状态*/ final Iterable<Transition> transitions = restClient.getIssueClient().getTransitions(issue.getTransitionsUri()).claim(); final Transition passTransition = getTransitionByName(transitions, "Resolve Issue"); restClient .getIssueClient() .transition(issue.getTransitionsUri(), new TransitionInput(passTransition.getId())) .claim(); }
/* * (non-Javadoc) * @see org.opennms.api.integration.ticketing.Plugin#saveOrUpdate(org.opennms.api.integration.ticketing.Ticket) */ @Override public void saveOrUpdate(Ticket ticket) throws PluginException { JiraRestClient jira = getConnection(); 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( getProperties().getProperty("jira.project"), Long.valueOf(getProperties().getProperty("jira.type").trim())); builder.setReporterName(getProperties().getProperty("jira.username")); builder.setSummary(ticket.getSummary()); builder.setDescription(ticket.getDetails()); builder.setDueDate(new DateTime(Calendar.getInstance())); 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 (getProperties().getProperty("jira.resolve").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(), getProperties().getProperty("jira.resolve")); } else if (Ticket.State.OPEN.equals(ticket.getState())) { Comment comment = Comment.valueOf("Issue reopened by OpenNMS."); for (Transition transition : transitions) { if (getProperties().getProperty("jira.reopen").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(), getProperties().getProperty("jira.reopen")); } } }