1

我正在使用以下代码来查询工作区。但显然,如果工作区未设置为默认值,则此查询将失败。如何成功查询不是我的默认工作区的工作区?谢谢。

try{
        QueryRequest proj1 = new QueryRequest("Workspace");
        proj1.setFetch(new Fetch("Name"));
        proj1.setQueryFilter(new QueryFilter("Name", "=",args[2]));
        QueryResponse proj2 = batman.query(proj1);
        Wspace_ref = proj2.getResults().get(0).getAsJsonObject().get("_ref").getAsString();
        System.out.println("Workspace found.");
    }catch (IndexOutOfBoundsException e){
        System.out.println("Workspace Not Found.");
        System.exit(1);
    }
4

1 回答 1

1

由于默认情况下 Rally REST 查询是工作区范围的,因此要获取工作区的枚举,您需要查询订阅对象,然后拉取其工作区集合。

这是一个简单的例子:

    public class RestExample_QueryWorkspacesAndProjects {

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

                    // Create and configure a new instance of RallyRestApi
                    // Connection parameters
                    String rallyURL = "https://rally1.rallydev.com";
                    String wsapiVersion = "1.43";
                    String applicationName = "RestExample_QueryWorkspacesAndProjects";

                    // Credentials
                    String userName = "user@company.com";
                    String userPassword = "topsecret";

                    RallyRestApi restApi = new RallyRestApi(
                            new URI(rallyURL),
                            userName,
                            userPassword);
                    restApi.setWsapiVersion(wsapiVersion);
                    restApi.setApplicationName(applicationName);      

                    //Read Subscription
                    QueryRequest subscriptionRequest = new QueryRequest("Subscriptions");
                    subscriptionRequest.setFetch(new Fetch("Name", "SubscriptionID", "Workspaces", "Name"));

                    QueryResponse subscriptionQueryResponse = restApi.query(subscriptionRequest);
                    JsonArray subscriptionQueryResults = subscriptionQueryResponse.getResults();
                    JsonElement subscriptionQueryElement = subscriptionQueryResults.get(0);
                    JsonObject subscriptionQueryObject = subscriptionQueryElement.getAsJsonObject();
                    String subID = subscriptionQueryObject.get("SubscriptionID").toString();

                    System.out.println("Read Subscription: " + subID);

                    // Grab Workspaces Collection
                    JsonArray myWorkspaces = subscriptionQueryObject.get("Workspaces").getAsJsonArray();

                    // Initialize Project counter
                    int numberProjects = 0;

                    for (int i=0; i<myWorkspaces.size(); i++) {
                            JsonObject workspaceObject = myWorkspaces.get(i).getAsJsonObject();
                            String workspaceRef = workspaceObject.get("_ref").getAsString();                

                        GetRequest workspaceRequest = new GetRequest(workspaceRef);
                        workspaceRequest.setFetch(new Fetch("Name", "Projects"));
                        GetResponse workspaceResponse = restApi.get(workspaceRequest);
                        JsonObject workspaceObj = workspaceResponse.getObject();

                        String workspaceName = workspaceObj.get("Name").getAsString();
                        System.out.printf("Workspace %d ==> %s\n", i, workspaceName);

                        JsonArray myProjects = workspaceObj.get("Projects").getAsJsonArray();

                        for (int j=0; j<myProjects.size(); j++)
                        {
                            JsonObject projectObject = myProjects.get(j).getAsJsonObject();
                            String projectRef = projectObject.get("_ref").getAsString();
                            GetRequest projectRequest = new GetRequest(projectRef);
                            projectRequest.setFetch(new Fetch("Name"));
                            GetResponse projectResponse = restApi.get(projectRequest);              

                            JsonObject projectObj = projectResponse.getObject();
                            String projectName = projectObj.get("Name").getAsString();
                            System.out.printf("==> Project %d ==> %s\n", j, projectName);

                            numberProjects++;
                        }
                    }

                    String numberWorkspacesStr =  String.valueOf(myWorkspaces.size());
                    String numberProjectsStr = String.valueOf(numberProjects);

                    System.out.println("Total Workspaces: " + numberWorkspacesStr);
                    System.out.println("Total Projects: " + numberProjectsStr);

                    restApi.close();
            }
于 2013-07-06T15:31:02.263 回答