Anthony Hunter
Anthony Hunter

Reputation: 21

Using the TFS REST API to get changeset history

Is there a way with the TFS rest api's to get the history of a changeset? I have the item path and its current changeset id, this is actually a merge id, and I want to see the merge details so that I can get the id of the changeset that it came from. From the web I can see this easily, but I need to be able to code this as I need to produce a report for internal audit purposes. Visual history of changeset

Thanks, Anthony

Upvotes: 0

Views: 2804

Answers (3)

Anthony Hunter
Anthony Hunter

Reputation: 21

Thanks for the great feedback. I continued investigating myself and found a similar way to finding the information.

$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
}

Upvotes: 0

Andy Li-MSFT
Andy Li-MSFT

Reputation: 30372

So, just use the get changes REST API to retrieve the merge details of the specific changeset:

GET http://SERVER:8080/tfs/DefaultCollection/_apis/tfvc/changesets/{changesetId}/changes

You can simply use this PS sample to get the merge details of the specific merge 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

You can also get each merge changeset details in a loop, you can also export the result to a .csv file: (Note : The running may very slow if you have too many changesets, you can cut off as needed with the conodition restricted.)

#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

enter image description here

Upvotes: 1

ChamindaC
ChamindaC

Reputation: 1492

You can call REST API in following format to get the changes including the IDs of the merged changes and changed file paths. Say your changeset is 736 then call REST API with

http://yourtfs:8080/tfs/collectionname/_apis/tfvc/changesets/736/changes

For example in VSTS below works

https://myacc.visualstudio.com/defaultcollection/_apis/tfvc/changesets/736/changes

How did I find it?

Tested the scenario with VSTS and it should work fine with TFS 2017 as it is using REST api version 1.0

My changeset 736 here is a merge happened to the branch and it has two changes done in other other branch.

enter image description here

When I execute get with changset id 736 i receive the changeset details from REST api.

https://myacc.visualstudio.com/DefaultCollection/_apis/tfvc/changesets/736?api-version=1.0

enter image description here

Then I can call changes api url found in the returned result highlighted above which will return the other changeset ids including changed file paths

https://myacc.visualstudio.com/_apis/tfvc/changesets/736/changes

enter image description here

Upvotes: 0

Related Questions