Ejemplo n.º 1
1
 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 testGetVoter() {
   final Issue issue = client.getIssueClient().getIssue("TST-1").claim();
   final Votes votes = client.getIssueClient().getVotes(issue.getVotes().getSelf()).claim();
   assertFalse(votes.hasVoted());
   assertThat(votes.getUsers(), containsInAnyOrder(USER1));
 }
 @Test
 public void testGetIssueWithAnonymouslyCreatedWorklogEntry() {
   setAnonymousMode();
   final Issue issue = client.getIssueClient().getIssue("ANONEDIT-2").claim();
   final Iterator<Worklog> worklogIterator = issue.getWorklogs().iterator();
   assertTrue(worklogIterator.hasNext());
   assertNull(worklogIterator.next().getAuthor());
 }
 @Test
 public void testGetIssueWithAnonymouslyCreatedAttachment() {
   setAnonymousMode();
   final Issue issue = client.getIssueClient().getIssue("ANONEDIT-1").claim();
   final Iterator<Attachment> attachmentIterator = issue.getAttachments().iterator();
   assertTrue(attachmentIterator.hasNext());
   assertNull(attachmentIterator.next().getAuthor());
 }
 @Test
 public void testGetWatchers() throws Exception {
   final Issue issue = client.getIssueClient().getIssue("TST-1").claim();
   final Watchers watchers =
       client.getIssueClient().getWatchers(issue.getWatchers().getSelf()).claim();
   assertEquals(1, watchers.getNumWatchers());
   assertFalse(watchers.isWatching());
   assertThat(watchers.getUsers(), containsInAnyOrder(USER1));
 }
 @Test
 public void testFetchingIssueWithCommentWhenAuthorIsDeleted() {
   final Issue issue = client.getIssueClient().getIssue(ISSUE_KEY_WITH_REMOVED_USER_DATA).claim();
   final Comment comment = issue.getComments().iterator().next();
   assertNotNull(comment);
   final BasicUser author = comment.getAuthor();
   assertNotNull(author);
   assertEquals(getUserUri(REMOVED_USER_NAME), author.getSelf());
 }
 private void assertNumVotesAndNoVotersDetails(final String issueKey, final int numVotes) {
   final Issue issue = client.getIssueClient().getIssue(issueKey).claim();
   assertEquals(numVotes, issue.getVotes().getVotes());
   assertFalse(issue.getVotes().hasVoted());
   final Votes votes = client.getIssueClient().getVotes(issue.getVotes().getSelf()).claim();
   assertFalse(votes.hasVoted());
   assertEquals(numVotes, votes.getVotes());
   assertTrue(Iterables.isEmpty(votes.getUsers()));
 }
 @Test
 public void testFetchingIssueWithWorklogWhenAuthorIsDeleted() {
   final Issue issue = client.getIssueClient().getIssue(ISSUE_KEY_WITH_REMOVED_USER_DATA).claim();
   final Worklog worklog = issue.getWorklogs().iterator().next();
   assertNotNull(worklog);
   final BasicUser author = worklog.getAuthor();
   assertNotNull(author);
   assertThat(author.getName(), equalTo(REMOVED_USER_NAME));
   assertTrue("expected incomplete self uri", author.isSelfUriIncomplete());
 }
  @Test
  public void testGetIssueWithNonTrivialComments() {
    final Issue issue = client.getIssueClient().getIssue("TST-2").claim();
    final Iterable<Comment> comments = issue.getComments();
    assertEquals(3, Iterables.size(comments));
    final Comment c1 = Iterables.get(comments, 0);
    assertEquals(Visibility.role("Administrators"), c1.getVisibility());

    final Comment c3 = Iterables.get(comments, 2);
    assertEquals(Visibility.group("jira-users"), c3.getVisibility());
  }
Ejemplo n.º 10
0
 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();
 }
 @Test
 public void testGetWatcherForAnonymouslyAccessibleIssue() {
   setAnonymousMode();
   final Issue issue = client.getIssueClient().getIssue("ANNON-1").claim();
   final Watchers watchers =
       client.getIssueClient().getWatchers(issue.getWatchers().getSelf()).claim();
   assertEquals(1, watchers.getNumWatchers());
   assertFalse(watchers.isWatching());
   assertTrue("JRADEV-3594 bug!!!", Iterables.isEmpty(watchers.getUsers()));
   // to save time
   assertEquals(new TimeTracking(2700, 2400, null), issue.getTimeTracking());
 }
Ejemplo n.º 12
0
  /**
   * Implementation of TicketerPlugin API call to retrieve a Jira trouble ticket.
   *
   * @return an OpenNMS
   * @throws PluginException
   */
  @Override
  public Ticket get(String ticketId) throws PluginException {
    JiraRestClient jira = getConnection();
    if (jira == null) {
      return null;
    }

    // w00t
    Issue issue;
    try {
      issue = jira.getIssueClient().getIssue(ticketId).get();
    } catch (InterruptedException | ExecutionException e) {
      throw new PluginException("Failed to get issue with id: " + ticketId, e);
    }

    if (issue != null) {
      Ticket ticket = new Ticket();

      ticket.setId(issue.getKey());
      ticket.setModificationTimestamp(String.valueOf(issue.getUpdateDate().toDate().getTime()));
      ticket.setSummary(issue.getSummary());
      ticket.setDetails(issue.getDescription());
      ticket.setState(getStateFromStatusName(issue.getStatus().getName()));

      return ticket;
    } else {
      return null;
    }
  }
  @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"))));
    }
  }
Ejemplo n.º 14
0
  /*
   * (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"));
      }
    }
  }