3

我正在使用 REST 工具包,有很多很酷的东西,但我看不到 API 文档中提到的从集合中删除项目的方法:

可以通过向集合引用 url 发送 POST 请求来从 Modifiable 集合中删除项目,该请求的正文包含要删除的项目数组。通过对https://rally1.rallydev.com/slm/webservice/v2.0/defect/49103/tags/remove的 POST 请求从集合中删除标签的示例有效负载:

有人做过这个吗?有代码吗?

4

2 回答 2

1

该工具包的 v3.1.1 添加了对添加到集合和从集合中移除的支持。一探究竟!

http://rallytools.github.io/RallyRestToolkitFor.NET/html/efed9f73-559a-3ef8-5cd7-e3039040c87d.htm

于 2016-07-14T18:51:14.150 回答
0

由于工具包中还没有从集合中删除集合元素的便捷方法,因此为了从标签集合中删除标签,您必须用该集合的副本替换现有集合减去您打算进行的更改到原始集合,然后使用该新标签集合更新故事。

此代码从故事中删除标签。在这个例子中,我去掉了集合的最后一个标签。例如,如果我有 2 个标签的集合,则代码留下一个标签,当再次运行此代码时,将删除最后一个标签,不再标记故事。作为替代方案,注释掉的条件会根据标签名称跳过不需要的标签。

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

namespace aRESTremoveTagFromStory
{
    class Program
    {
        static void Main(string[] args)
        {
            //Initialize the REST API
            RallyRestApi restApi;
            restApi = new RallyRestApi("user@co.com", "secret", "https://rally1.rallydev.com", "v2.0");

            //Set our Workspace and Project scopings
            String workspaceRef = "/workspace/1111"; //replace this OID with an OID of your workspace
            String projectRef = "/project/2222";     //replace this OID with an OID of your project
            bool projectScopingUp = false;
            bool projectScopingDown = true;

            Request storyRequest = new Request("HierarchicalRequirement");

            storyRequest.Workspace = workspaceRef;
            storyRequest.Project = projectRef;
            storyRequest.ProjectScopeUp = projectScopingUp;
            storyRequest.ProjectScopeDown = projectScopingDown;

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

            storyRequest.Query = new Query("FormattedID", Query.Operator.Equals, "US123"); //replace this FormattedID with FormattedID valid in your workspace
            QueryResult queryStoryResults = restApi.Query(storyRequest);

           //create an array to contain modified list of tags for this story. 
            ArrayList tagList = new ArrayList();

            foreach (var s in queryStoryResults.Results)
            {

                Console.WriteLine("FormattedID: " + s["FormattedID"] + " Name: " + s["Name"]);
                Console.WriteLine("----------");
        ///query for Tags collection on this story    
                Request tagRequest = new Request(s["Tags"]);
                QueryResult queryTagResult = restApi.Query(tagRequest);
                int count = 0;
                if (queryTagResult.TotalResultCount > 0)
                {
                    foreach (var t in queryTagResult.Results)
                    {
                        var tagName = t["Name"];
                        var tagRef = t["_ref"];
                        Console.WriteLine(tagName + "  " + tagRef + " " + tagOID);

                        if (count < queryTagResult.TotalResultCount - 1) //skips the last tag
                        //if (tagName != "tag2") //skips the tag with name "tag2"
                        {
                            DynamicJsonObject newTag = new DynamicJsonObject();
                            newTag["_ref"] = tagRef;
                            tagList.Add(newTag);
                        }
                        count++;
                    }
                }
                else
                {
                    Console.WriteLine("no tags found");
                }

                DynamicJsonObject newTagCollection = new DynamicJsonObject();
                newTagCollection["Tags"] = tagList;

                OperationResult updateResult = restApi.Update(s["_ref"], newTagCollection);
            }
        }
    }
}
于 2013-09-06T19:40:43.790 回答