在Microsoft Team Foundation Server 2018中通过API通过模板创建任务

时间:2018-11-16 00:15:55

标签: c# tfs tfs-workitem tfs-sdk

我正在使用C#创建从我们的测试框架(Selenium)到Team Foundation Server(TFS)2018的集成(我们的测试已经编写在其中)-集成将基于测试结果在TFS中生成工作项。我想从模板创建工作项,但似乎找不到任何文档。我正在使用Microsoft发现的here的TFS客户端库。

我可以从TFS中提取模板:

var client = new HttpClient()
// Client auth stuff removed
var method = new HttpMethod("GET");
var httpRequestMessage = new HttpRequestMessage(method, "http://server:port/tfs/collection/team/project/_api/wit/templates/12345abc");
var httpResponseMessage = client.SendAsync(httpRequestMessage).Result;
WorkItemTemplate tfs_template = httpResponseMessage.Content.ReadAsAsync<WorkItemTemplate>().Result;

用于创建新工作项here的API看起来非常简单,但是我找不到任何方法来连接这两个操作,也找不到使用此调用来应用模板的方法。是否可以通过带有模板的API创建工作项,如果可以,是否有任何文档?

1 个答案:

答案 0 :(得分:1)

您无法通过工作项目模板创建工作项目。您可以使用工作项模板来查看哪些字段包含一些默认值。此示例用于带有REST API的模板中的新工作项:

        using Microsoft.TeamFoundation.Core.WebApi;
        using Microsoft.TeamFoundation.Core.WebApi.Types;
        using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
        using Microsoft.VisualStudio.Services.Common;
        using Microsoft.VisualStudio.Services.WebApi;
        using Microsoft.VisualStudio.Services.WebApi.Patch;
        using Microsoft.VisualStudio.Services.WebApi.Patch.Json;
        using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Text;
        using System.Threading.Tasks;

        namespace ConsoleApp1
        {
            class Program
            {
                static string ServiceURL = "https://your_name.visualstudio.com";
                static string PAT = "your pat";
                static void Main(string[] args)
                {
                    string projectName = "your project";
                    string templateName = "Critical bug";

                    /*connect to service. I use VSTS. In your case: 
                    VssConnection connection = new VssConnection(new Uri(ServiceURL), new VssCredential());
                    https://docs.microsoft.com/ru-ru/azure/devops/integrate/get-started/client-libraries/samples?view=vsts
                    */

                    VssConnection connection = new VssConnection(new Uri(ServiceURL), new VssBasicCredential(string.Empty, PAT));
                    //get clients
                    var WitClient = connection.GetClient<WorkItemTrackingHttpClient>();
                    var ProjectClient = connection.GetClient<ProjectHttpClient>();

                    var project = ProjectClient.GetProject(projectName).Result;

                    //get context for default project team
                    TeamContext tmcntx = new TeamContext(project.Id, project.DefaultTeam.Id);

                    //get all template for team
                    var templates = WitClient.GetTemplatesAsync(tmcntx).Result;

                    //get tempate through its name
                    var id = (from tm in templates where tm.Name == templateName select tm.Id).FirstOrDefault();

                    if (id != null)
                    {
                        var template = WitClient.GetTemplateAsync(tmcntx, id).Result;

                        JsonPatchDocument patchDocument = new JsonPatchDocument();

                        foreach (var key in template.Fields.Keys) //set default fields from template
                            patchDocument.Add(new JsonPatchOperation()
                            {

                                Operation = Operation.Add,
                                Path = "/fields/" + key,
                                Value = template.Fields[key]

                            });

                        //add any additional fields
                        patchDocument.Add(new JsonPatchOperation()
                        {

                            Operation = Operation.Add,
                            Path = "/fields/System.Title",
                            Value = "My critical bug"

                        });

                        var newWorkItem = WitClient.CreateWorkItemAsync(patchDocument, projectName, template.WorkItemTypeName).Result;
                    }            

                }
            }
        }

我的模板: enter image description here

相关问题