Rally Api - 如何寻找GetByReference结果

时间:2015-09-11 12:13:44

标签: c# rally

我有一些看起来像这样的代码。

var userStory = restApi.GetByReference (userStoryRef, "Name", "FormattedID");
for (int i = 0; i < (int) userStory["TotalResultCount"]; i++)
{
    var name = userStory["Results"][i]["Name"];

...

当然,如果GetByReference的结果超过20(即TotalResultCount> 20),则会失败,因为默认页面大小为20.

所以我需要对此进行分页,但我无法确定您如何设置页面大小,或者从GetByReference请求第二页。

我尝试将?pagesize = 100添加到参考中,但它似乎不会影响结果集。

e.g。

var userStory = restApi.GetByReference (userStoryRef + "?pagesize=100", "Name", "FormattedID");

我也尝试完全放弃这一点,并将其作为查询重新措辞(我 知道如何翻页。)

e.g。

var request = new Request("HierarchicalRequirement")
{
    Fetch = new List<string>()
    {
        "Name", "FormattedID"
    }, Query = new Query("Parent.FormattedID", Query.Operator.Equals, featureId)
};

var result = myRestApi.Query(request);       

但是这个查询对我来说也不起作用(featureId是一个带有相关用户故事的有效格式化ID)。但该查询不返回任何结果。

(我很欣赏这完全是一个不同的问题 - 很高兴能够开始工作,但理解两者都会很棒!)

感谢任何帮助。

1 个答案:

答案 0 :(得分:1)

见下面的代码。注意storiesRequest.Limit = 1000当此数字高于默认值20时,默认为200,因此无需将storiesRequest.PageSize设置为200

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Rally.RestApi;
using Rally.RestApi.Response;
using Rally.RestApi.Json;

namespace GetByRefExample
{
    class Program
    {
        static void Main(string[] args)
        {
            RallyRestApi restApi = new RallyRestApi(webServiceVersion: "v2.0");
            String apiKey = "_abc123";
            restApi.Authenticate(apiKey, "https://rally1.rallydev.com", allowSSO: false);
            String workspaceRef = "/workspace/123";
            String projectRef = "/project/456";  

            Request request = new Request("PortfolioItem/Feature");
            request.Fetch = new List<string>() { "Name", "FormattedID" };
            request.Query = new Query("FormattedID", Query.Operator.Equals, "F2356");
            QueryResult result = restApi.Query(request);

            String featureRef = result.Results.First()._ref;
            Console.WriteLine("found" + featureRef);

            //create stories
            try
            {
                for (int i = 1; i <= 25; i++)
                {
                    DynamicJsonObject story = new DynamicJsonObject();
                    story["Name"] = "story " + i;
                    story["PlanEstimate"] = new Random().Next(2,10);
                    story["PortfolioItem"] = featureRef;
                    story["Project"] = projectRef;
                    CreateResult createResult = restApi.Create(workspaceRef, "HierarchicalRequirement", story);
                    story = restApi.GetByReference(createResult.Reference, "FormattedID");
                    Console.WriteLine("creating..." + story["FormattedID"]);
                }
            //read stories
                DynamicJsonObject feature = restApi.GetByReference(featureRef, "UserStories");
                Request storiesRequest = new Request(feature["UserStories"]);
                storiesRequest.Fetch = new List<string>()
                {
                    "FormattedID",
                    "PlanEstimate"
                };
                storiesRequest.Limit = 1000;
                QueryResult storiesResult = restApi.Query(storiesRequest);
                int storyCount = 0;
                foreach (var userStory in storiesResult.Results)
                {
                    Console.WriteLine(userStory["FormattedID"] + " " + userStory["PlanEstimate"]);
                    storyCount++;
                }
                Console.WriteLine(storyCount);
            } 
            catch (Exception e)
            {
                Console.WriteLine(e);
            }
        }
    }
}