gitazuretagsdevopsrepo

How to get commit ID for a tag in Azure Devops git repository


I am trying to automate a view of all the workitems that have been closed between 2 branch tags of a git repo. One of the main issues I'm having is for some reason, when I view the tags from the tags page: https://dev.azure.com/{organization}/{project}/_git/{repositoryId}/tags, I can see the commit id of the tags:

enter image description here

However, I can't for the life of me figure out how to get that commit id from a tag via REST API call. I'm getting a list of the tags with the following:

$tagsUrl = "https://dev.azure.com/$Organization/$SystemTeamProject/_apis/git/repositories/$repositoryId/refs?filter=tags/&api-version=7.1-preview.1"
$tagsResponse = Invoke-RestMethod -Uri $tagsUrl -Method Get -Headers $headers
$allTags = $tagsResponse.value | ForEach-Object { $_.name -replace "refs/tags/", "" }

All code I've seen shows to retrieve the commit id via the following URI, but it returns the "objectId" of the tag, which does not match the actual commit id!

https://dev.azure.com/{org}/{proj}/_apis/git/repositories/{repo/refs?filter=tags/24.2.1.601-RELEASE-CANDIDATE&api-version=7.1-preview.1

enter image description here

If it helps, the initial tags are created via a PowerShell script once we create our release branch:

git tag -a $releaseInfo.ReleaseTag -m "Tagging release branch"
git push -q origin $releaseInfo.ReleaseTag

All the API calls to get a range use either a changset id or a date range. In looking at the API call from the image, there isn't even a date associated with the tag uri!

What am I missing?


Solution

  • This is the correct way to get the commit ID using the objectId of the tag name:

    function Get-CommitIdFromTagName {
        param (
            [Parameter(Mandatory = $true)]
            [string]$RepoName,
            [Parameter(Mandatory = $true)]
            [string]$TagName
        )
    
        # Retrieve all tags in the repository
        $tagsUrl = "https://dev.azure.com/$($Global:Organization)/$($Global:SystemTeamProject)/_apis/git/repositories/$RepoName/refs?filter=tags/&api-version=7.1-preview.1"
        $tagsResponse = Invoke-RestMethod -Uri $tagsUrl -Method Get -Headers $Global:headers
    
        foreach ($tag in $tagsResponse.value) {
            $currentTagName = $tag.name -replace "^refs/tags/", ""
    
            if ($currentTagName -eq $TagName) {
                $tagObjectId = $tag.objectId
    
                $annotatedTagUrl = "https://dev.azure.com/$($Global:Organization)/$($Global:SystemTeamProject)/_apis/git/repositories/$RepoName/annotatedtags/${tagObjectId}?api-version=7.1-preview.1"
                $annotatedTagResponse = Invoke-RestMethod -Uri $annotatedTagUrl -Method 
    
    Get -Headers $Global:headers
                $commitId = $annotatedTagResponse.taggedObject.objectId
    
                return $commitId
            }
        }
    
        throw "Tag '$TagName' not found in repository '$RepoName'"
    }
    

    t