【发布时间】:2020-02-13 15:35:52
【问题描述】:
TFS rest api 有没有办法获取变更集的历史记录?我有项目路径及其当前变更集 id,这实际上是一个合并 id,我想查看合并详细信息,以便我可以获取它来自的变更集的 id。 从网上我可以很容易地看到这一点,但我需要能够对此进行编码,因为我需要为内部审计目的生成报告。 Visual history of changeset
谢谢, 安东尼
【问题讨论】:
TFS rest api 有没有办法获取变更集的历史记录?我有项目路径及其当前变更集 id,这实际上是一个合并 id,我想查看合并详细信息,以便我可以获取它来自的变更集的 id。 从网上我可以很容易地看到这一点,但我需要能够对此进行编码,因为我需要为内部审计目的生成报告。 Visual history of changeset
谢谢, 安东尼
【问题讨论】:
所以,只需使用 get changes REST API 来检索特定变更集的合并详细信息:
GET http://SERVER:8080/tfs/DefaultCollection/_apis/tfvc/changesets/{changesetId}/changes
您可以简单地使用此 PS 示例来获取特定合并 changset 的合并详细信息:
Param(
[string]$collectionUrl = "http://server:8080/tfs/DefaultCollection",
[string]$keepForever = "true",
[string]$changesetId = "376",
[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)))
$uri = "$collectionUrl/_apis/tfvc/changesets/$changesetId/changes"
$result = Invoke-RestMethod -Uri $uri -Method Get -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
$customObject = new-object PSObject -property @{
"MergeChangesetId" = $changesetId
"ServerItem" = $result.value.mergeSources.serverItem
"versionFrom" = $result.value.mergeSources.versionFrom
"versionTo" = $result.value.mergeSources.versionTo
"changeType" = $result.value.changeType
}
$customObject | Select `
MergeChangesetId,
ServerItem,
versionFrom,
versionTo,
changeType
您还可以在循环中获取每个合并变更集的详细信息,也可以将结果导出到 .csv 文件:(注意:如果您的变更集过多,运行可能会很慢,您可以根据需要在条件受限的情况下切断。)
#Get the work items associated to Release
$collectionurl = "http://server:8080/tfs/DefaultCollection"
$ErrorActionPreference = 'SilentlyContinue'
#Get changesets
$changesetsUrl = "$collectionurl/_apis/tfvc/changesets"
$changesets = Invoke-RestMethod -Uri $changesetsUrl -Method Get -UseDefaultCredential
#Get the changeset history.
$changesetResults = @()
foreach ($changeset in $changesets.value){
$changesetId = $changeset.changesetId
$baseUrl = "$collectionurl/_apis/tfvc/changesets/$changesetId/changes"
$response = Invoke-RestMethod -Uri $baseUrl -Method Get -UseDefaultCredential
$customObject = new-object PSObject -property @{
"MergeChangesetId" = $changesetId
"ServerItem" = $response.value.mergeSources.serverItem
"versionFrom" = $response.value.mergeSources.versionFrom
"versionTo" = $response.value.mergeSources.versionTo
"changeType" = $response.value.changeType
}
$changesetResults += $customObject
}
$changesetResults | Select `
MergeChangesetId,
ServerItem,
versionFrom,
versionTo,
changeType | Where-Object {$_.changeType -like '*merge*'} #|export-csv -Path C:\LC\MergeChangesetsDetails.csv -NoTypeInformation
【讨论】:
您可以通过以下格式调用 REST API 来获取更改,包括合并更改的 ID 和更改的文件路径。假设您的变更集是 736,然后使用
调用 REST APIhttp://yourtfs:8080/tfs/collectionname/_apis/tfvc/changesets/736/changes
例如在下面的 VSTS 中工作
https://myacc.visualstudio.com/defaultcollection/_apis/tfvc/changesets/736/changes
我是如何找到它的?
使用 VSTS 测试了该场景,它应该可以在 TFS 2017 上正常工作,因为它使用的是 REST api 版本 1.0
我的变更集 736 是发生在分支上的合并,它在其他分支中完成了两项更改。
当我使用 changset id 736 执行 get 时,我会收到来自 REST api 的变更集详细信息。
https://myacc.visualstudio.com/DefaultCollection/_apis/tfvc/changesets/736?api-version=1.0
然后我可以调用在上面突出显示的返回结果中找到的更改 api url,它将返回其他更改集 id,包括更改的文件路径
https://myacc.visualstudio.com/_apis/tfvc/changesets/736/changes
【讨论】:
感谢您的宝贵反馈。我继续调查自己并找到了类似的方法来查找信息。
$uri = $collection + "/_apis/tfvc/items?api-version=3.0&scopePath=" + $sourcePath + "&recursionLevel=Full"
$response = Invoke-RestMethod -Method Get -Credential $credential -ContentType application/json -Uri $uri
foreach ( $value in $response.value )
{
$uri = $collection + "/_apis/tfvc/items?api-version=3.0&scopePath=" + $value.path + "&versionType=MergeSource&version=" + $value.version
$mergeResponse = Invoke-RestMethod -Method Get -Credential $credential -ContentType application/json -Uri $uri
}
【讨论】: