通过Rally REST API在UserStory中进行讨论

时间:2013-08-22 13:45:05

标签: java rally

我尝试阅读用户故事中的讨论

    RallyRestApi restApi = new RallyRestApi(new URI(rallyURL), userName, userPassword);
    restApi.setApplicationName("QueryExample");

    try
    {

        QueryRequest storyRequest = new QueryRequest("HierarchicalRequirement");
        storyRequest.setFetch(new Fetch("Name", "Owner", "UserName", "EmailAddress", "Discussion"));
        storyRequest.setLimit(1000);
        storyRequest.setScopedDown(false);
        storyRequest.setScopedUp(false);
        storyRequest.setWorkspace(workspaceRef);
        storyRequest.setProject(projectRef);
        storyRequest.setQueryFilter(new QueryFilter("FormattedID", "=", "US4"));

        QueryResponse storyQueryResponse = restApi.query(storyRequest);
        JsonObject storyJsonObject = storyQueryResponse.getResults().get(0).getAsJsonObject();

        System.out.println("Name: " + storyJsonObject.get("Name"));
        JsonObject userObject = storyJsonObject.get("Owner").getAsJsonObject().getAsJsonObject();
        System.out.println(userObject.get("UserName"));
        System.out.println(userObject.get("EmailAddress"));
        System.out.println(userObject.get("Discussion"));

    }
    finally
    {
        //Release resources
        restApi.close();
    }

讨论是NULL。

如何通过REST API阅读和编写用户故事讨论?

WBR,Slava

1 个答案:

答案 0 :(得分:0)

只要您了解故事,就可以在ConversationPost上将其遍历到Text属性,这是Discussion集合的一个元素,如下所示:

int numberOfPosts = storyJsonObject.get("Discussion").getAsJsonArray().size();
            System.out.println(numberOfPosts);
            if(numberOfPosts>0){
                for (int i=0;i<numberOfPosts;i++){
                    System.out.println(storyJsonObject.get("Discussion").getAsJsonArray().get(i).getAsJsonObject().get("Text"));
                }
            }

此外,还需要获取这些字段:

storyRequest.setFetch(new Fetch("Name","Discussion","ConversationPost","Text"));

以下是整个代码:

import com.google.gson.JsonObject;
import com.rallydev.rest.RallyRestApi;
import com.rallydev.rest.request.QueryRequest;
import com.rallydev.rest.response.QueryResponse;
import com.rallydev.rest.util.Fetch;
import com.rallydev.rest.util.QueryFilter;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;

public class aRESTstories {

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


            String host = "https://rally1.rallydev.com";
            String username = "user@co.com";
            String password = "1984";
        String workspaceRef = "/workspace/1111";
            String projectRef = "/project/2222";            

            String applicationName = "RESTExample: find story, print discussions";
            String wsapiVersion = "1.43";

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

        try{
            QueryRequest storyRequest = new QueryRequest("HierarchicalRequirement");
            storyRequest.setFetch(new Fetch("Name","Owner","UserName","PlanEstimate","Discussion","ConversationPost","Text"));
            storyRequest.setLimit(1000);
            storyRequest.setScopedDown(false);
            storyRequest.setScopedUp(false);
            storyRequest.setWorkspace(workspaceRef);
            storyRequest.setProject(projectRef);
            storyRequest.setQueryFilter(new QueryFilter("FormattedID", "=", "US359"));

            QueryResponse storyQueryResponse = restApi.query(storyRequest);
            JsonObject storyJsonObject = storyQueryResponse.getResults().get(0).getAsJsonObject();
            String storyRef = storyJsonObject.get("_ref").toString();
            System.out.println("Name: " + storyJsonObject.get("Name"));

            int numberOfPosts = storyJsonObject.get("Discussion").getAsJsonArray().size();
            System.out.println(numberOfPosts);
            if(numberOfPosts>0){
                for (int i=0;i<numberOfPosts;i++){
                    System.out.println(storyJsonObject.get("Discussion").getAsJsonArray().get(i).getAsJsonObject().get("Text"));
                }
            }

        }catch(Exception e){
            System.out.println("Exception occurred....");
            e.printStackTrace();
        }
        finally{
            restApi.close();
        }
    }
}