附加工作项以通过 REST 构建 API

attach workitem to build via REST API

我想将 tfs 上的工作项附加到构建中。我正在阅读包含工作项编号的 SVN 日志,并尝试更新实际构建以附加它们。

workitems.Add(workItemStore.GetWorkItem(workitemid));
buildDetail.Information.AddAssociatedWorkItems(workitems.ToArray());

当我尝试点击 buildDetail.Information.Save();buildDetail.Save(); 时,我得到一个 AccessDeniedException.

参见 another post

所以我想尝试使用 REST... 在 MSDN 上加载页面后,我得出结论,没有负责构建的 .NET 客户端库。看起来我唯一的选择是将 json 修补到 TFS:

PATCH https://{instance}/DefaultCollection/{project}/_apis/build/builds/{buildId}?api-version={version}

如何正确添加工作项?

编辑:我发现了一个 ,它在 TFS 命名空间中提到了一个 dll,它具有与上面的调用相同的功能。不幸的是,它没有在 MSDN 中引用。同样的问题:无法添加工作项。

传播问题并将其解决给 MS:Patrick has created a post on uservoice

更新:

我设法 link 构建了工作项。这是 中的一种方法:

var json = new JsonPatchDocument
            {
                new JsonPatchOperation()
                {
                    Operation = Operation.Add,
                    Path = "/relations/-",
                    Value = new WorkItemRelation()
                            {
                                Rel = "ArtifactLink",
                                Url = build.Uri.ToString()
                            }
                }
            };

var client = new WebClient { UseDefaultCredentials = true };
client.Headers.Add(HttpRequestHeader.ContentType, "application/json-patch+json");
client.UploadData(
    options.CollectionUri + "/_apis/wit/workitems/" + workitemid + "?api-version=1.0",
    "PATCH",
    Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(json)));

但是构建中仍然没有直接绑定:

并且工作项显示 unknown executable linktype

根据工作项中给出的消息,我假设我使用的是错误的 linktype。有没有人给我提供参考,我能够并且应该使用什么类型?

URI 更新: 我已经在使用提到的 uri:

次要解决方案:

我必须将名称 "Build" 添加到补丁的属性中。我仍然无法在构建本身中识别它,但现在,我可以使用 link 作为构建类型。

var json = new JsonPatchDocument
            {
                new JsonPatchOperation()
                {
                    Operation = Operation.Add,
                    Path = "/relations/-",
                    Value = new WorkItemRelation()
                            {
                                Rel = "ArtifactLink",
                                Url = build.Uri.ToString()
                                Attributes = new Dictionary<string, object>()
                                {
                                    { "name", "Build" },
                                    { "comment", build.Result.ToString() }
                                }
                            }
                }
            };

更新

正如 Eddie 回答的那样,您可以通过更新工作项将工作项添加到构建中,以通过 Rest API 向构建添加关系 link ].

关于 LinkTypeEddie 的 回答中有一个清晰的演示。您需要使用 build uri.

格式需要vstfs:///Build/Build/8320

TFS 构建任务的 BuildUri 是一个 属性,需要对其进行设置,以便这些任务可以与服务器就它们正在为其执行操作的构建进行通信。


您还可以在 powershell 脚本中使用 $env:BUILD_BUILDURI,更多详细信息和方法您还可以从 MSDN 参考此博客:Build association with work Items in vNext 最后将得到:

您可以将工作项添加到构建中,方法是更新工作项以通过 Rest API 将关系 link 添加到构建中。详情参考这个link:Add a Link.

将 link 添加到工作项中的构建后,工作项将显示在构建摘要中。

正文内容示例如下,

[
    {
        "op": "test",
        "path": "/rev",
        "value": "2"
    },
    {
        "op": "add",
        "path": "/relations/-",
        "value":
        {
            "rel": "ArtifactLink",
            "url": "vstfs:///Build/Build/12351"
        }
    }
]

添加代码示例:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;
using System.Net.Http;
using System.Net.Http.Headers;

namespace AssociateWorkItemToBuild
{
    class Program
    {
        static void Main(string[] args)
        {
            using (HttpClient client = new HttpClient())
            {
                client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", Convert.ToBase64String(
                     ASCIIEncoding.ASCII.GetBytes(
                        string.Format("{0}:{1}", "username", "password"))));

                string Url = "https://xxxxxx.visualstudio.com/_apis/wit/workitems/149?api-version=1.0";
                StringBuilder body = new StringBuilder();
                body.Append("[");
                body.Append("{");
                body.Append("\"op\":\"add\",");
                body.Append(" \"path\":\"/relations/-\",");
                body.Append("\"value\":");
                body.Append("{");
                body.Append("\"rel\": \"ArtifactLink\",");
                body.Append("\"url\": \"vstfs:///Build/Build/12596\"");
                body.Append("}");
                body.Append("}");
                body.Append("]");

                var method = new HttpMethod("PATCH");
                var request = new HttpRequestMessage(method, Url)
                {
                    Content = new StringContent(body.ToString(), Encoding.UTF8,
                                    "application/json-patch+json")
                };

                using (HttpResponseMessage response = client.SendAsync(request).Result)
                {
                    response.EnsureSuccessStatusCode();
                    string responseBody = response.Content.ReadAsStringAsync().Result;
                }
            }
        }
    }
}