首页 > 解决方案 > 如何使用rest API从多个变更集中获取文件列表

问题描述

我尝试使用 TFS REST API 获取文件列表以形成多个变更集。我能够使用以下 URL 成功获取单个变更集的文件列表:

https://company.visualstudio.com/DefaultCollection/_apis/tfvc/changesets/"{ChangesetsetID}/changes?api-version=4.1"

如何获取文件列表以形成多个变更集?我是用 Javascript Ajax 来获取和显示的。

标签: javascriptjqueryvisual-studiotfvc

解决方案


通常对于源代码管理中添加的文件,您可以根据您的需求/调试等更改它们,然后使用变更集签入。这意味着,几乎所有文件都属于多个变更集......

所以,在我看来,获取属于多个变更集的文件列表是没有意义的。相反,获取属于单个变更集的文件更有意义......

无论如何,您可以使用以下 REST API 获取包含对指定项目/文件的更改的变更集:

GET https://SERVER:8080/TFS/{CollectionName}/_apis/tfvc/changesets?searchCriteria.itemPath=$/Fabrikam-Fiber-TFVC/AuthSample/AuthSample/Program.cs&api-version=3.2

您可以获取文件的名称并循环调用 REST API 以检查每个文件,如果响应中的值的计数大于 1,则该文件应该是您要检索的文件...

PowerShell 示例供您参考:(更改path正文中的,例如在下面的示例中,我检查了路径下的文件$/ScrumProject/ConsoleApplication1/ConsoleApplication1:)

Param(
   [string]$collectionurl = "http://server:8080/tfs/DefaultCollection",
   [string]$user = "domain\name",
   [string]$token = "password",
   [string]$exportpath = "D:\temp"

)
$filename = (Get-Date).ToString("yyyyMMdd-HHmmss") + "-" + "FileList.csv"

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

#Get path of the files from a sepcific folder 
$fileurl = "$collectionurl/_apis/tfvc/itemBatch?api-version=3.2"

$body = '{"includeContentMetadata":true,"includeLinks":null,"itemDescriptors":[{"path":"$/ScrumProject/ConsoleApplication1/ConsoleApplication1","versionType":5,"recursionLevel":4}]}'

$pathresponse = Invoke-RestMethod -Uri $fileurl -Method POST -Body $body -ContentType "application/json"-Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
$paths = $pathresponse.value.path

Clear-Host
#Get changesets that contain changes to the specified item/file
foreach ($path in $paths )
{
$baseUrl = "$collectionurl/_apis/tfvc/changesets?searchCriteria.itemPath=$path&api-version=3.2" 
$changesetResponse = Invoke-RestMethod -Uri $baseUrl -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
$count = $changesetResponse.count

#Displsy and export the matching files to a *.csv file

 if ($count -gt 1)
 {
  Write-host $path
  $path | Add-Content $exportpath\$filename
 }
}

在此处输入图像描述


推荐阅读