我想将 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}
如何以正确的方式添加工作项?
编辑:我找到了 old post其中提到了 TFS 命名空间中的一个 dll,它具有与上面的调用相同的功能。不幸的是,MSDN 中没有提及它。同样的问题:无法添加工作项。
传播该问题并将其解决给MS:Patrick has created a post on uservoice
更新:
我设法链接工作项中的构建。这是 c# 中的一种方法:
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)));
根据工作项中给出的消息,我假设我使用的是错误的 linktype 。有人可以给我引用吗?我能够并且应该使用哪些类型?
次要解决方案:
我必须将名称“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() }
}
}
}
};
最佳答案
您可以通过更新工作项以通过 Rest API 将关系链接添加到构建,从而将工作项添加到构建中。详情请引用此链接:Add a 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;
}
}
}
}
}
关于c# - 通过 REST API 附加要构建的工作项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38849455/