Search code examples
bitbucketbitbucket-apibitbucket-cloud

Get latest tag commit from bitbucket api 2.0


I am trying to create a utility tool for which I need to get the latest tag committed to a specific repo. I have tried so far:

curl -X GET \
  https://api.bitbucket.org/2.0/repositories/<team>/<reposlug>/refs/tags \
  -H 'Accept: */*' \
  -H 'Accept-Encoding: gzip, deflate' \
  -H 'Authorization: Basic encodedpasswd' \
  -H 'Cache-Control: no-cache' \
  -H 'Connection: keep-alive' \
  -H 'Host: api.bitbucket.org'

What I get in return is paginated response, like this:

{
    "pagelen": 10,
    "values": [
        {
            "name": "release-1.2",
            "links": {
            },
            "tagger": {
            
            },
            "date": "2019-11-15T11:53:56+00:00",
            "message": "[maven-release-plugin]copy for tag release-1.2\n",
            "type": "tag",
            "target": {
            }
        },
        {
            "name": "release-1.3",
            "links": {
            },
            "tagger": {
            },
            "date": "2019-11-20T07:53:51+00:00",
            "message": "[maven-release-plugin]copy for tag release-1.3\n",
            "type": "tag",
            "target": {
            }
        }
    ],
    "page": 1
}

Now as per the documentation I have referred through here, tags are specially ordered when returned, but I am confused as to why the value release-1.3 is not first in the response. I think I am missing something. Or if this is the expected order, how can I achieve the tags sorted on date attribute, to get the latest tag.


Solution

  • So I was able to solve this by using sort on attribute target.date.

       curl -X GET \
      https://api.bitbucket.org/2.0/repositories/<team>/<reposlug>/refs/tags?sort=target.date \
      -H 'Accept: */*' \
      -H 'Accept-Encoding: gzip, deflate' \
      -H 'Authorization: Basic encodedpasswd' \
      -H 'Cache-Control: no-cache' \
      -H 'Connection: keep-alive' \
      -H 'Host: api.bitbucket.org'