azure-devopsazure-devops-rest-apiazure-repos

Azure Devops, How to extract latest tag present in a repository using Rest APIs?


I'm trying to fetch latest tag of a Azure git repo using Rest API. So first fetched all tags of a repo using rest api. But this json returned the tags in no fixed order i.e sometimes latest tag is at top and sometimes at bottom.

Due to this I'm not able to extract.

I tried below snippet in bash, but it returned random tag, not latest.

tags_response=$(curl -s -u ":$pat" \
"https://dev.azure.com/$organization/$project/_apis/git/repositories/$repo_id/refs?filter=tags&api-version=7.1-preview.1")

# Extract the latest tag
latest_tag=$(echo $tags_response | jq -r '.value | sort_by(.name) | last(.[]).name')

Solution

  • TL;DR it's possible to get the creation date of an annotated tag, but not of a lightweight tag (see Use Git tags).

    Use the Annotated Tags - Get endpoint to get the metadata (including the creation date) of a specific tag.

    Sample response:

    {
      "name": "refs/tags/v0.1-beta2",
      "objectId": "69080710948ac8ba63e44eca2daf0b30f38c428d",
      "taggedObject": {
        "objectId": "c60be62ebf0e86b5aa01dbb98657b4b7e5905234",
        "objectType": "commit"
      },
      "taggedBy": {
        "name": "Norman Paulk",
        "email": "Fabrikamfiber16@hotmail.com",
        "date": "2017-06-22T04:28:23"
      },
      "message": "First beta release",
      "url": "https://dev.azure.com/fabrikam/c34d5807-1734-4541-ad1c-d16e9ac1faca/_apis/git/repositories/ca93c3a5-87bb-4b5b-a62f-1f971d677c79/annotatedTags/69080710948ac8ba63e44eca2daf0b30f38c428d"
    }
    

    More details

    Azure DevOps supports both annotated and lightweight tags:

    You can create annotated tags using the web portal. You can create both lightweight and annotated tags from within Visual Studio.

    Seeing the details of a tag created in the web portal:

    Tag details in Azure DevOps portal