在Rally . net API中查询特定任务的所有者



我试图查询用户附加到一个任务在Rally,使用。net API。我成功地查询了用户故事以及相应的任务。

using System;
using System.Collections.Generic;
namespace RallyIntegration
{
    using Microsoft.CSharp.RuntimeBinder;
    using Rally.RestApi;
    using Rally.RestApi.Response;
    class UserStoryTask
    {
        static void Main(string[] args)
        {
            ///<summary>
            ///Will reuturn the user stories, tasks and the estimates per each task
            /// </summary>
            //instantiate Rally Object
            RallyRestApi api = new RallyRestApi();
            //user-credentials
            const string username = "username@x.com"; //use get methods
            const string password = "secret"; //use get methods;
            const string serverUrl = "https://rally1.rallydev.com";
            //authenitcate
            api.Authenticate(username, password, serverUrl, null, false);

            string workspaceReferenceID = "/workspace/secret";
            string projectReferenceID = "/project/secret";
            bool projectScopingUp = false; //will not include all the projects above the default project
            bool projectScopingdown = true; //will include child projects
            //setup the userStoryRequest
            Request userStoryRequest = new Request("HierarchicalRequirement");
            userStoryRequest.Workspace = workspaceReferenceID;
            userStoryRequest.Project = projectReferenceID;
            userStoryRequest.ProjectScopeUp = projectScopingUp;
            userStoryRequest.ProjectScopeDown = projectScopingdown;
            //fetch data from the story request
            userStoryRequest.Fetch = new List<string>()
            {
                "FormattedID", "Name", "Tasks", "Estimate", "State", "Owner", "UserName"
            };
            //Userstory Query
            userStoryRequest.Query = (new Query("LastUpdateDate", Query.Operator.GreaterThan, "2016-01-01"));
            QueryResult userStoryResult = api.Query(userStoryRequest);
            //iterate through the query results
            foreach (var userStory in userStoryResult.Results)
            {
                Console.WriteLine(userStory["FormattedID"] + ":" + userStory["Name"]);
                //Task Request
                Request taskRequest = new Request(userStory["Tasks"]);
                QueryResult taskResult = api.Query(taskRequest);
                if (taskResult.TotalResultCount > 0)
                {
                    foreach (var task in taskResult.Results)
                    {
                        var taskName = task["Name"];
                        var taskEstimate = task["Estimate"];
                        var taskState = task["State"];
                        Console.WriteLine("Task Name: " + taskName + Environment.NewLine + "Estimate: " + taskEstimate + Environment.NewLine + "State: "+taskState);
                        try
                        { 
                            //how can I say only return if the task has an owner attached to it
                            //http://stackoverflow.com/questions/32621290/what-is-the-proper-way-to-fetch-the-owner-name-of-a-rally-task
                            Request ownerRequest = new Request(userStory["Owner"]);
                            QueryResult ownerResult = api.Query(ownerRequest);
                            foreach (var owner in ownerResult.Results)
                            {
                                var ownerName = owner["Owner"];
                                Console.WriteLine("Owner: " + ownerName);
                            }
                        }
                        catch(NullReferenceException)
                        {
                            Console.WriteLine("Null Reference Hit");
                        }
                        catch(RuntimeBinderException)
                        {
                            Console.WriteLine("Binder Exception");
                        }
                        finally
                        {
                            //api.close();
                        }
                    }
                }
                else
                {
                    Console.WriteLine("Tasks Not Found...");
                }
            }
            Console.ReadLine();
        }
    }
}

//我们需要提取特定任务的用户或所有者

只要您在查询任务集合时获取Owner:

taskRequest.setFetch("Owner");

您应该能够查看每个任务的所有者:

foreach (var task in taskResult.Results)
{
    var owner = task["Owner"];
    if (owner != null) 
    {
        var ownerName = owner["_refObjectName"];                    
        //todo: do something here
    }

}

最新更新