Rally Pull Defect Suite从工作区

时间:2017-01-03 16:28:31

标签: rally

我从论坛获得了向缺陷套件添加缺陷的代码片段。 但是,我的要求是从缺陷套件中提取缺陷,我不知道缺陷套件信息。

有没有办法从中收集缺陷套件和缺陷。

以下是向缺陷套件添加缺陷的示例代码段。

> import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.rallydev.rest.RallyRestApi;
import com.rallydev.rest.request.CreateRequest;
import com.rallydev.rest.request.QueryRequest;
import com.rallydev.rest.request.UpdateRequest;
import com.rallydev.rest.response.CreateResponse;
import com.rallydev.rest.response.QueryResponse;
import com.rallydev.rest.response.UpdateResponse;
import com.rallydev.rest.util.Fetch;
import com.rallydev.rest.util.QueryFilter;
import com.rallydev.rest.util.Ref;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class addDefectToSuite {

    public static void main(String[] args) throws URISyntaxException, IOException {

        String host = "https://rally1.rallydev.com";
            String username = "user@company.com";
            String password = "secret";
            String wsapiVersion = "v2.0";
            String projectRef = "/project/12352608219";      
            String workspaceRef = "/workspace/12352608129"; 
            String applicationName = "Create defect, add to a defectsuite";


        RallyRestApi restApi = new RallyRestApi(
                new URI(host),
                username,
                password);
        restApi.setWsapiVersion(wsapiVersion);
        restApi.setApplicationName(applicationName);   

        QueryRequest defectSuiteRequest = new QueryRequest("DefectSuite");
        defectSuiteRequest.setFetch(new Fetch("FormattedID","Name", "Defects"));
        defectSuiteRequest.setWorkspace(workspaceRef);
        defectSuiteRequest.setQueryFilter(new QueryFilter("FormattedID", "=", "DS1"));
        QueryResponse defectSuiteQueryResponse = restApi.query(defectSuiteRequest);
        JsonObject defectSuiteJsonObject = defectSuiteQueryResponse.getResults().get(0).getAsJsonObject();
        System.out.println("defectSuiteJsonObject" + defectSuiteJsonObject);
        String defectSuiteRef = defectSuiteJsonObject.get("_ref").getAsString(); 
        int numberOfDefects = defectSuiteJsonObject.getAsJsonObject("Defects").get("Count").getAsInt();
        System.out.println(defectSuiteJsonObject.get("Name") + " ref: " + defectSuiteRef + "number of defects: " + numberOfDefects + " " + defectSuiteJsonObject.get("Defects"));


        try {
            JsonObject defect = new JsonObject();
            defect.addProperty("Name", "bad defect 668");

            CreateRequest createRequest = new CreateRequest("defect", defect);
            CreateResponse createResponse = restApi.create(createRequest);
            if (createResponse.wasSuccessful()) {
                JsonObject defectJsonObject = createResponse.getObject();
                String defectRef = Ref.getRelativeRef(createResponse.getObject().get("_ref").getAsString());
                System.out.println(String.format("Created %s", defectRef));  
                JsonObject defectSuitesOfThisDefect = (JsonObject) defectJsonObject.get("DefectSuites");
                int numberOfSuites = defectSuitesOfThisDefect.get("Count").getAsInt();
                System.out.println("number of defect suites this defect is part of: " + numberOfSuites);
                QueryRequest defectSuitesOfThisDefectRequest = new QueryRequest(defectSuitesOfThisDefect);
                JsonArray suites = restApi.query(defectSuitesOfThisDefectRequest).getResults();
                System.out.println("suites: " + suites);
                suites.add(defectSuiteJsonObject);
                System.out.println("suites after add: " + suites);
                //Update defect: add to defectsutites collection
                JsonObject defectUpdate = new JsonObject();
                defectUpdate.add("DefectSuites", suites);
                UpdateRequest updateDefectRequest = new UpdateRequest(defectRef,defectUpdate);
                UpdateResponse updateResponse = restApi.update(updateDefectRequest);
                if (updateResponse.wasSuccessful()) {
                    System.out.println("Successfully updated defect: " + defectJsonObject.get("FormattedID"));
                }
                else {
                    String[] updateErrors;
                    updateErrors = createResponse.getErrors();
                    System.out.println("Error");
                    for (int i=0; i<updateErrors.length;i++) {
                        System.out.println(updateErrors[i]);
                    }
                }

            } else {
                System.out.println("error");
            }

        } finally {
            restApi.close();
        }   

    } 
}

1 个答案:

答案 0 :(得分:0)

Defect对象有一个DefectSuites集合,因此您只需查询DefectSuites包含指定的DefectSuite的所有缺陷:

QueryRequest defectsRequest = new QueryRequest("Defect");
defectsRequest.setFetch(new Fetch("FormattedID","Name"));
defectsRequest.setQueryFilter(new QueryFilter("DefectSuites", "contains", defectSuiteRef));
QueryResponse defectsResponse = restApi.query(defectsRequest);

或者,您可以通过在上面的原始代码中直接在DefectSuite上查询Defects集合来从另一个方面来看它:

JsonObject defectsCollection = defectSuiteJsonObject.getAsJsonObject("Defects");
QueryRequest defectsRequest = new QueryRequest(defectsCollection);
defectSuiteRequest.setFetch(new Fetch("FormattedID","Name")); 
QueryResponse defectsResponse = restApi.query(defectsRequest);