예제 #1
0
  public static void test1() throws URISyntaxException, IOException {
    User user = User.getInstance();
    // Create and configure a new instance of RallyRestApi
    RallyRestApi restApi =
        new RallyRestApi(
            new URI("https://rally1.rallydev.com"), user.getUser(), user.getPassword());
    restApi.setApplicationName("QueryExample");

    try {

      System.out.println("Querying for unaccepted stories for current user...");

      QueryRequest defects = new QueryRequest("HierarchicalRequirement");

      defects.setFetch(new Fetch("FormattedID", "Name", "ScheduleState"));
      QueryFilter qf1 = new QueryFilter("Owner.Name", "=", user.getUser());
      QueryFilter qf2 = new QueryFilter("ScheduleState", "<", "Accepted");
      defects.setQueryFilter(qf1.and(qf2));
      defects.setOrder("FormattedID DESC");

      // Return up to 20, 1 per page
      defects.setPageSize(1);
      defects.setLimit(20);

      QueryResponse queryResponse = restApi.query(defects);
      if (queryResponse.wasSuccessful()) {
        System.out.println(
            String.format("\nTotal results: %d", queryResponse.getTotalResultCount()));
        System.out.println("unaccept stories:");
        for (JsonElement result : queryResponse.getResults()) {
          JsonObject defect = result.getAsJsonObject();
          System.out.println(
              String.format(
                  "\t%s - %s - ScheduleState:%s",
                  defect.get("FormattedID").getAsString(),
                  defect.get("Name").getAsString(),
                  defect.get("ScheduleState").getAsString()));
        }
      } else {
        System.err.println("The following errors occurred: ");
        for (String err : queryResponse.getErrors()) {
          System.err.println("\t" + err);
        }
      }

    } finally {
      // Release resources
      restApi.close();
    }
  }
  public static void main(String[] args) throws URISyntaxException, IOException {

    String host = "https://rally1.rallydev.com";
    String apiKey = "_abc123";
    String workspaceRef = "/workspace/12352608129";
    String applicationName = "bulkUpdateTagsOnDefects";

    RallyRestApi restApi = new RallyRestApi(new URI(host), apiKey);
    restApi.setApplicationName(applicationName);

    try {
      String tagToRemove = "tag1";
      String tagToAdd = "new tag";

      // find all defects with "tag1" with ScheduleState < Completed
      QueryRequest defectRequest = new QueryRequest("Defect");
      defectRequest.setWorkspace(workspaceRef);
      defectRequest.setFetch(new Fetch(new String[] {"Name", "FormattedID", "Tags"}));
      defectRequest.setLimit(1000);

      defectRequest.setQueryFilter(
          (new QueryFilter("Tags.Name", "contains", tagToRemove))
              .and(new QueryFilter("ScheduleState", "<", "Completed")));

      QueryResponse defectQueryResponse = restApi.query(defectRequest);
      System.out.println("Size: " + defectQueryResponse.getTotalResultCount());

      /*
      QueryRequest  oldTagRequest = new QueryRequest("Tag");
      oldTagRequest.setWorkspace(workspaceRef);
      oldTagRequest.setQueryFilter(new QueryFilter("Name", "=", tagToRemove));
      QueryResponse oldTagQueryResponse = restApi.query(oldTagRequest);
         if(oldTagQueryResponse.getTotalResultCount() == 0){
         	System.out.println("Cannot find tag: " + tagToRemove);
             return;
         }
         JsonObject oldTagJsonObject = oldTagQueryResponse.getResults().get(0).getAsJsonObject();
       String oldTagRef = oldTagJsonObject.get("_ref").getAsString();
       System.out.println(oldTagRef);
      */
      QueryRequest newTagRequest = new QueryRequest("Tag");
      newTagRequest.setWorkspace(workspaceRef);
      newTagRequest.setQueryFilter(new QueryFilter("Name", "=", tagToAdd));
      QueryResponse newTagQueryResponse = restApi.query(newTagRequest);
      if (newTagQueryResponse.getTotalResultCount() == 0) {
        System.out.println("Cannot find tag: " + tagToAdd);
        return;
      }
      JsonObject newTagJsonObject = newTagQueryResponse.getResults().get(0).getAsJsonObject();
      String newTagRef = newTagJsonObject.get("_ref").getAsString();
      System.out.println(newTagRef);

      if (defectQueryResponse.getTotalResultCount() == 0) {
        System.out.println("Cannot find defects tagged : " + tagToRemove);
        return;
      } else {
        for (int i = 0; i < defectQueryResponse.getResults().size(); i++) {
          JsonObject defectJsonObject = defectQueryResponse.getResults().get(i).getAsJsonObject();
          String defectRef = defectJsonObject.get("_ref").getAsString();
          System.out.println(
              "Name: "
                  + defectJsonObject.get("Name")
                  + " FormattedID: "
                  + defectJsonObject.get("FormattedID"));
          int numberOfTags = defectJsonObject.getAsJsonObject("Tags").get("Count").getAsInt();
          QueryRequest tagCollectionRequest =
              new QueryRequest(defectJsonObject.getAsJsonObject("Tags"));
          tagCollectionRequest.setFetch(new Fetch("Name", "ObjectID"));
          JsonArray tags = restApi.query(tagCollectionRequest).getResults();
          for (int j = 0; j < numberOfTags; j++) {
            System.out.println("Tag Name: " + tags.get(j).getAsJsonObject().get("Name"));
          }
          tags.add(newTagJsonObject);
          JsonObject defectUpdate = new JsonObject();
          defectUpdate.add("Tags", tags);
          UpdateRequest updateDefectRequest = new UpdateRequest(defectRef, defectUpdate);
          UpdateResponse updateDefectResponse = restApi.update(updateDefectRequest);
          if (updateDefectResponse.wasSuccessful()) {
            System.out.println(
                "Successfully updated : "
                    + defectJsonObject.get("FormattedID")
                    + " Tags after update: ");
            QueryRequest tagCollectionRequest2 =
                new QueryRequest(defectJsonObject.getAsJsonObject("Tags"));
            tagCollectionRequest2.setFetch(new Fetch("Name", "ObjectID"));
            JsonArray tagsAfterUpdate = restApi.query(tagCollectionRequest2).getResults();
            int numberOfTagsAfterUpdate = restApi.query(tagCollectionRequest2).getResults().size();
            for (int j = 0; j < numberOfTagsAfterUpdate; j++) {
              System.out.println(
                  "Tag Name: " + tagsAfterUpdate.get(j).getAsJsonObject().get("Name"));
            }
          }
        }
      }

    } finally {
      restApi.close();
    }
  }