1

我找不到通过 C# 向用户故事添加标签值的语法。

使用最新的 Rally API 二进制文件 2.0 Beta。

我能找到的最接近的示例是通过 Java(如何使用 Rally 的 JAVA API 在 Rally 中添加标签到 TestCase?),这看起来很有帮助,但是从示例中我需要访问 JSONArray 和 JSONElement,但我的 C# 部署DynamicJSONObject通过 RestAPI使用根据 Rally 示例,但既不JSONArray也不DynamicJSONArray(也不JSONElement)通过 RestAPI 参考公开。

我可以DynamicJSONArray通过添加 Microsoft 的System.Web.Helpers参考来启用访问,但 MicrosoftDynamicJSonArray参考与 RestAPI 变体竞争......

我可以使用全名来限定 restapi 变体来解决这个问题,但我真的不想在没有明确观点的情况下开始这条道路,如果它会导致某些功能。如果我将 Microsoft 传递给DyamicJSONArrayRally DyamicJSONObject,我我怀疑它会产生任何功能。

有没有人有任何 C# 代码做一些简单的事情,比如创建标签并将其分配给用户故事或测试用例?

4

2 回答 2

1

这是一个简单的示例,它使用现有标签和新创建的标签更新故事上的标签集合:

namespace RestExample_UpdateStoryTags
{
    class Program
    {
        static void Main(string[] args)
        {

            //Initialize the REST API
            RallyRestApi restApi;
            String rallyUserName = "user@company.com";
            String rallyPassword = "topsecret";
            String rallyURL = "https://rally1.rallydev.com";
            String wsapiVersion = "1.41";
            String myWorkspaceName = "My Workspace";

            restApi = new RallyRestApi(
                rallyUserName,
                rallyPassword,
                rallyURL,
                wsapiVersion
            );

            // Get a Reference to Target Workspace
            Request workspaceRequest = new Request("workspace");
            workspaceRequest.Fetch = new List<string>()
                {
                    "Name",
                    "ObjectID"
                };

            workspaceRequest.Query = new Query("Name", Query.Operator.Equals, myWorkspaceName);
            QueryResult workspaceQueryResults = restApi.Query(workspaceRequest);

            var targetWorkspace = workspaceQueryResults.Results.First();
            Console.WriteLine("Found Target Workspace: " + targetWorkspace["Name"]);

            String workspaceRef = targetWorkspace["_ref"];

            //Query for Target Tag
            Request tagRequest = new Request("tag");
            tagRequest.Fetch = new List<string>()
                {
                    "Name",
                    "ObjectID"
                };

            // Query all Tags for a tag named "Montane"
            tagRequest.Query = new Query("Name", Query.Operator.Equals, "Tundra");
            QueryResult queryTagResults = restApi.Query(tagRequest);

            var targetTagResult = queryTagResults.Results.First();
            long tagOID = targetTagResult["ObjectID"];

            DynamicJsonObject targetTag = restApi.GetByReference("tag", tagOID, "Name", "ObjectID");

            // Query for User Story
            // FormattedID of target story
            String targetStoryFormattedID = "US5";

            Request storyRequest = new Request("hierarchicalrequirement");
            storyRequest.Fetch = new List<string>()
                {
                    "Name",
                    "ObjectID",
                    "Iteration",
                    "FormattedID",
                    "Tags"
            };


            storyRequest.Query = new Query("FormattedID", Query.Operator.Equals, targetStoryFormattedID);
            QueryResult queryStoryResults = restApi.Query(storyRequest);

            var targetUserStory = queryStoryResults.Results.First();
            Console.WriteLine("Found Target User Story: " + targetUserStory["Name"]);

            // Grab collection of existing Tags
            var existingTags = targetUserStory["Tags"];

            // Summarize Existing Tags
            Console.WriteLine("Existing Tags for Story" + targetStoryFormattedID + ": ");
            foreach (var tag in existingTags)
            {
                Console.WriteLine(tag["Name"]);
            }

            long targetOID = targetUserStory["ObjectID"];

            // Now do update of the User Story

            // Tags collection on object is expected to be a System.Collections.ArrayList
            var targetTagArray = existingTags;
            targetTagArray.Add(targetTag);

            DynamicJsonObject toUpdate = new DynamicJsonObject();
            toUpdate["Tags"] = targetTagArray;

            OperationResult updateResult = restApi.Update("HierarchicalRequirement", targetOID, toUpdate);
            foreach (var error in updateResult.Errors)
            {
                Console.WriteLine(error.ToString());
            }

            // Re-read target Story
            DynamicJsonObject updatedStory = restApi.GetByReference(targetUserStory["_ref"], "Tags,Name");
            var updatedTags = updatedStory["Tags"];

            // Summarize Updated Tags
            Console.WriteLine("Updated Tags for Story" + targetStoryFormattedID + ": ");
            foreach (var tag in updatedTags)
            {
                Console.WriteLine(tag["Name"]);
            }

            // Create a New Tag, and add New Tag to Story
            DynamicJsonObject newTag = new DynamicJsonObject();
            newTag["Name"] = "Boreal";

            CreateResult createResult = restApi.Create(workspaceRef, "Tag", newTag);

            // Get the ref of the created Tag
            String newTagRef = createResult.Reference;

            // Read the Target Tag
            DynamicJsonObject newTagRead = restApi.GetByReference(newTagRef, "Name");

            // Add the newly-created Tag to the Story
            targetTagArray.Add(newTagRead);
            toUpdate["Tags"] = targetTagArray;

            updateResult = restApi.Update("HierarchicalRequirement", targetOID, toUpdate);

            // Re-read target Story
            updatedStory = restApi.GetByReference(targetUserStory["_ref"], "Tags,Name");
            updatedTags = updatedStory["Tags"];

            // Summarize Updated Tags
            Console.WriteLine("Updated Tags (with newly-created Tag for Story" + targetStoryFormattedID + ": ");
            foreach (var tag in updatedTags)
            {
                Console.WriteLine(tag["Name"]);
            }
            Console.ReadKey();
        }
    }
}
于 2013-04-03T15:43:06.057 回答
0
//Based on Mark's code, below comes close... but updates still failing
//Maybe I need to try an earlier version?

String strTeam = "xxx";

String workspaceRef = "/workspace/4914063320";
String projectRef = "/project/7412041414";

//get user story
Request reqUserStory = new Request("hierarchicalrequirement");
reqUserStory.Fetch = new List<string>() { "ObjectID", "Release", "Iteration", "PlanEstimate", "Description", "Tags" };

reqUserStory.Query = new Query("Project", Query.Operator.Equals, projectRef)
    .And(new Query("Name", Query.Operator.Equals, "mystoryname"));

QueryResult queryResultUS = restApi.Query(reqUserStory);
DynamicJsonObject myUserStory = new DynamicJsonObject();
myUserStory = queryResultUS.Results.FirstOrDefault();
if (null != myUserStory)
{
//get tags for user story
    var existingTags = myUserStory["Tags"];
    if (null != existingTags)
    {
        bool bFound = false;

        var tagList = existingTags["_tagsNameArray"];
        //look for tag
        foreach (var tag in tagList)
        {
            String strValue = tag["Name"];
            if (strValue == strTeam)
                bFound = true;
        }

        if (!bFound)
        {
            //replace tags array

            var targetTagArray = existingTags;
            var tagList2 = targetTagArray["_tagsNameArray"];
            tagList2.Add(targetTag);//even if i comment this out, update below fails...
            targetTagArray["_tagsNameArray"] = tagList2;

            DynamicJsonObject toUpdate = new DynamicJsonObject();
            toUpdate["Tags"] = targetTagArray;
            long oid = Convert.ToInt64(myUserStory["ObjectID"]);
            opResultRelease = restApi.Update("hierarchicalrequirement", oid, toUpdate);
            // Fails with Error: ["Could not read: Could not read referenced object 2"]
        }

    }
}
于 2013-04-04T12:47:35.820 回答