Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 通过RESTAPI将workitem附加到构建_C#_Tfs_Tfsbuild - Fatal编程技术网

C# 通过RESTAPI将workitem附加到构建

C# 通过RESTAPI将workitem附加到构建,c#,tfs,tfsbuild,C#,Tfs,Tfsbuild,我想将tfs上的工作项附加到生成。我正在阅读包含工作项编号的SVN日志,并尝试更新实际构建以附加它们 workitems.Add(workItemStore.GetWorkItem(workitemid)); buildDetail.Information.AddAssociatedWorkItems(workitems.ToArray()); 当我尝试点击buildDetail.Information.Save()时或buildDetail.Save(),我得到一个AccessDeniedE

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

workitems.Add(workItemStore.GetWorkItem(workitemid));
buildDetail.Information.AddAssociatedWorkItems(workitems.ToArray());
当我尝试点击
buildDetail.Information.Save()时
buildDetail.Save(),我得到一个
AccessDeniedException

所以我想试着休息一下。。。 在MSDN上加载了大量页面后,我得出结论,没有.NET客户端库负责构建。看起来我唯一的选择是将json修补到TFS中:

补丁https://{instance}/DefaultCollection/{project}/_-api/build/builds/{buildId}?api版本={version}

如何以正确的方式添加工作项

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

要传播该问题并将其提交给MS:

更新:

我设法在工作项中链接了一个构建。以下是一种方法:

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

工作项显示
未知可执行链接类型

根据workitem中给出的消息,我假设我使用的是false。有没有人能给我一个参考,我能用什么类型的,应该用什么类型的

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

次要解决方案:

我不得不将名称“Build”添加到补丁的属性中。我仍然无法在构建本身中识别它,但是现在,我可以使用链接作为构建类型

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() }
                                }
                            }
                }
            };
var json=new JsonPatchDocument
{
新的JSONPATCH操作()
{
操作=操作。添加,
Path=“/relations/-”,
值=新工作项关系()
{
Rel=“ArtifactLink”,
Url=build.Uri.ToString()
属性=新字典()
{
{“名称”,“构建”},
{“comment”,build.Result.ToString()}
}
}
}
};

更新

正如Eddie回答的那样,您可以通过更新工作项,通过RESTAPI将关系链接添加到构建中,从而将工作项添加到构建中

关于链接类型,Eddie的回答中有一个清晰的演示。您需要使用构建uri

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

TFS生成任务的BuildUri是一个需要设置的属性,以便这些任务可以与服务器就其执行操作的生成进行通信


您还可以在powershell脚本中使用
$env:BUILD\u BUILDURI
,更多详细信息以及您可以从MSDN引用此博客的方式:最终将获得:


您可以通过更新工作项,通过Rest API将关系链接添加到构建中,从而将工作项添加到构建中。有关详细信息,请参阅此链接:

在工作项中添加指向生成的链接后,工作项将显示在生成摘要中

以下是正文的内容示例

[
    {
        "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;
                }
            }
        }
    }
}

你的体型是什么?XAML或vNext?有关详细信息,请参阅我的答案。您需要使用构建uri,也可以在powershell脚本中使用
$env:build\u BUILDURI
。更多详细信息,请查看我的更新答案。嗨@Patrick MSFT,我已经在使用此uri。我已经为您更新了帖子。我可以在工作项中绑定链接,但仍然存在一个小问题。我更新我的问题。谢谢大家!仍然无法将其识别为链接的构建这不起作用,我得到了“工件链接必须指定有效名称”https://***.visualstudio.com/_-api/wit/workitems/49469?api版本=5.0[{“op”:“test”,“path”:“/rev”,“value”:“3”},{“op”:“add”,“path”:“/关系/-,“值”:{“rel”:“ArtifactLink”,“url”:vstfs:///Build/Build/105702" } } ]
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;
                }
            }
        }
    }
}