获取通过测试与用户故事相关联的类型的链接

时间:2018-07-22 11:34:30

标签: c#-4.0 tfs2017

如何使用c#获取与tfs中的用户案例关联的“测试者”类型的链接。

2 个答案:

答案 0 :(得分:1)

要获取工作项的链接,您需要:

TfsTeamProjectCollection col = new TfsTeamProjectCollection("https://myorg.visualstudio.com");
WorkItemStore store = new WorkItemStore(col);
WorkItem wi = store.GetWorkItem(1234);
foreach (Link item in wi.Links)
{
Trace.WriteLine(string.Format("link for {0} of type {1}", wi.Id, item.GetType().Name), "LinkMigrationContext");
}

如果您加载具有所需链接类型的工作项,您将能够看到所需的值和数据。

答案 1 :(得分:0)

您可以使用下面的C#示例代码来检索与特定工作项关联的Tested By链接:

安装Nuget软件包Microsoft.TeamFoundationServer.ExtendedClien t

using System;
using Microsoft.VisualStudio.Services.Client;
using Microsoft.VisualStudio.Services.WebApi;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi;
using Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models;

namespace RetrieveTestedByLinks
{
    class Program
    {
        static void Main(string[] args)
        {
            int id = 1; //specify the work item ID here

            var myCredentials = new VssClientCredentials();
            var connection = new VssConnection(new Uri(@"http://tfs2017-5044:8080/tfs/DefaultCollection"), myCredentials);
            WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient<WorkItemTrackingHttpClient>();

            WorkItem workitem = workItemTrackingClient.GetWorkItemAsync(id, expand: WorkItemExpand.Relations).Result;

            Console.WriteLine("WorkItem ID: {0}", workitem.Id);

            Console.WriteLine("TestedBy Links: ");
            foreach (var relation in workitem.Relations)
            {

                if (relation.Rel == "Microsoft.VSTS.Common.TestedBy-Forward")
                    Console.WriteLine(relation.Url);

            }
            Console.ReadLine();
        }
    }
}

enter image description here

此外,如果可以使用PowerShell,则可以将下面的示例与REST API结合使用,以获取与工作项关联的类型“ Tested By”的链接:

Param(
   [string]$collectionurl = "http://server:8080/tfs/DefaultCollection",
   [string]$workitemid = "74",
   [string]$user = "username",
   [string]$token = "password"
)

# Base64-encodes the Personal Access Token (PAT) appropriately
$base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))

#Get workitem relateions
$baseUrl = "$collectionurl/_apis/wit/workitems/$($workitemid)?"+"$"+"expand=all"            
$response = Invoke-RestMethod -Uri $baseUrl -Method Get -UseDefaultCredential -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}

$relations = $response.relations | where {$_.rel -like '*TestedBy*'} 

#Retrieve the links in a loop
$relationob = @()
foreach ($rel in $relations){

$customObject = new-object PSObject -property @{
          "Rel" = $rel.rel
          "url" = $rel.url
        } 

    $relationob += $customObject
}

$relationob | Select `
                Rel,
                url  #|export-csv -Path C:\test\Links.csv -NoTypeInformation

enter image description here