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."); } }
@Test public void testGetIssue() throws Exception { final Issue issue = client.getIssueClient().getIssue("TST-1").claim(); assertEquals("TST-1", issue.getKey()); assertEquals(Long.valueOf(10000), issue.getId()); assertTrue(issue.getSelf().toString().startsWith(jiraUri.toString())); assertEqualsNoUri(IntegrationTestUtil.USER_ADMIN, issue.getReporter()); assertEqualsNoUri(IntegrationTestUtil.USER_ADMIN, issue.getAssignee()); assertThat(issue.getLabels(), containsInAnyOrder("a", "bcds")); assertEquals(3, Iterables.size(issue.getComments())); final Iterable<String> expectedExpandos = getExpectedExpands(); assertThat( ImmutableList.copyOf(issue.getExpandos()), containsInAnyOrder(toArray(expectedExpandos, String.class))); assertEquals(new TimeTracking(null, 0, 190), issue.getTimeTracking()); assertTrue(Iterables.size(issue.getFields()) > 0); assertEquals( IntegrationTestUtil.START_PROGRESS_TRANSITION_ID, Iterables.size(issue.getAttachments())); final Iterable<Attachment> items = issue.getAttachments(); assertNotNull(items); Attachment attachment1 = new Attachment( IntegrationTestUtil.concat( IntegrationTestUtil.TESTING_JIRA_5_OR_NEWER ? UriBuilder.fromUri(jiraUri).path("/rest/api/2/").build() : jiraRestRootUri, "/attachment/10040"), "dla Paw\u0142a.txt", IntegrationTestUtil.USER_ADMIN, dateTime, 643, "text/plain", IntegrationTestUtil.concat(jiraUri, "/secure/attachment/10040/dla+Paw%C5%82a.txt"), null); assertEquals(attachment1, items.iterator().next()); // test for changelog assertNull(issue.getChangelog()); final Issue issueWithChangelogAndOperations = client.getIssueClient().getIssue("TST-2", EnumSet.of(CHANGELOG, OPERATIONS)).claim(); final Iterable<ChangelogGroup> changelog = issueWithChangelogAndOperations.getChangelog(); if (isJira5xOrNewer()) { assertNotNull(changelog); final ChangelogGroup chg1 = Iterables.get(changelog, 18); assertEquals("admin", chg1.getAuthor().getName()); assertEquals("Administrator", chg1.getAuthor().getDisplayName()); assertEquals( new DateTime(2010, 8, 17, 16, 40, 34, 924).toInstant(), chg1.getCreated().toInstant()); assertEquals( Collections.singletonList( new ChangelogItem(FieldType.JIRA, "status", "1", "Open", "3", "In Progress")), chg1.getItems()); final ChangelogGroup chg2 = Iterables.get(changelog, 20); assertEquals("admin", chg2.getAuthor().getName()); assertEquals("Administrator", chg2.getAuthor().getDisplayName()); assertEquals( new DateTime(2010, 8, 24, 16, 10, 23, 468).toInstant(), chg2.getCreated().toInstant()); final List<ChangelogItem> expected = ImmutableList.of( new ChangelogItem(FieldType.JIRA, "timeoriginalestimate", null, null, "0", "0"), new ChangelogItem(FieldType.CUSTOM, "My Radio buttons", null, null, null, "Another"), new ChangelogItem(FieldType.CUSTOM, "project3", null, null, "10000", "Test Project"), new ChangelogItem(FieldType.CUSTOM, "My Number Field New", null, null, null, "1.45")); assertEquals(expected, chg2.getItems()); } final Operations operations = issueWithChangelogAndOperations.getOperations(); if (isJira5xOrNewer()) { assertThat(operations, notNullValue()); assertThat( operations.getOperationById("log-work"), allOf(instanceOf(OperationLink.class), hasProperty("id", is("log-work")))); } }
/* * (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")); } } }