gitazure-devopsazure-devops-rest-api

Get the commits associated with an Azure DevOps release based on previous release


When working in Azure DevOps, specifically the release pipelines, you have this nice window on a stage showing the commits that are in this specific release.

Commit history in Azure DevOps for a specific release.

I would like to use this information within my pipeline, so I can log the commits that went into the specific release and publish them to a different location.

There is the BUILD_SOURCEVERSION variable available, but that's the hash from the latest commit.

I've tried using the Azure DevOps REST API and did a GET to https://vsrm.{{instance}}/{{teamproject}}/_apis/release/releases/{{releaseId}}?api-version=4.1-preview.6, but I don't think there's any information in there I can use.
I don't think the Pipelines- and Build endpoints provide any useful information for this either. But I could be wrong.

There's the changes endpoint, that can be invoked https://vsrm.{{instance}}/{{teamproject}}//_apis/Release/releases/{{releaseId}}/changes?baseReleaseId={{previousReleaseId}. This one works great, but I still need the previousReleaseId for this, and more specific the last release id the stage ran on.

Anyone got ideas on how to get the list of commits for a release?


Solution

  • You can use rest api Releases - List (with release defintionid) to get the previous release id in target release.

    GET https://vsrm.dev.azure.com/{organization}/{project}/_apis/release/releases?definitionId={definitionId}&api-version=7.1-preview.8
    

    PowerShell script sample below:

    Param(
       [string]$orgnurl = "https://vsrm.dev.azure.com/ORGNAME",
       [string]$projectName = "PROJECTNAME",
       [string]$DefinitionId = "1", #Release definition ID
       [string]$user = "",
       [string]$token = "PAT"
    )
    
    $base64AuthInfo = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $user,$token)))
    
    # to get the previous release id:
    
    $getreleaseuri = "$orgnurl/$projectName/_apis/release/releases?definitionId=$DefinitionId" + "&api-version=7.1-preview.8"
    
    $releases = Invoke-RestMethod -Uri $getreleaseuri -Method GET -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
    
    $currentreleaseid = $releases.value[0].id
    
    $previousreleaseid = $releases.value[1].id
    
    Write-Host $previousreleaseid $currentreleaseid # check previous release id and current release id
    
    # get changes assosiated with the current release
    
    $uri = "$orgnurl/$projectName/_apis/release/releases/$currentreleaseid/changes?baseReleaseId=$previousreleaseid"
    
    Write-Host $uri  # check uri to get the changes
    
    $commits = Invoke-RestMethod -Uri $uri -Method GET -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
    
    foreach ($commit in $commits.value.id) {
        Write-Host $commit
    }
    

    enter image description here

    In addition, if you would like to know the previous release id for specific release(not current release), you can change the code with below(define $targetrelease in script as above):

    $releases = Invoke-RestMethod -Uri $getreleaseuri -Method GET -Headers @{Authorization=("Basic {0}" -f $base64AuthInfo)}
    
    $PreviousReleaseId = ($releases.value | Where-Object { $_.id -lt $targetrelease } | Select-Object -First 1).id
    
    echo $PreviousReleaseId