Home > database >  How to get latest github tag names in Golang?
How to get latest github tag names in Golang?

Time:03-24

I need to check the latest version in GitHub tags. So far, I found one way that uses go-git library. I found this one https://github.com/src-d/go-git/issues/1030#. Is there any other method to get the latest GitHub tags name? I tried to find a good way for a few hours... I really appreciate your comments or help!

CodePudding user response:

Just make GET request to github REST API.

# https://api.github.com/repos/<user>/<repo>/tags
curl https://api.github.com/repos/src-d/go-git/tags

Output:

[
  {
    "name": "v4.13.1",
    "zipball_url": "https://api.github.com/repos/src-d/go-git/zipball/refs/tags/v4.13.1",
    "tarball_url": "https://api.github.com/repos/src-d/go-git/tarball/refs/tags/v4.13.1",
    "commit": {
      "sha": "0d1a009cbb604db18be960db5f1525b99a55d727",
      "url": "https://api.github.com/repos/src-d/go-git/commits/0d1a009cbb604db18be960db5f1525b99a55d727"
    },
    "node_id": "MDM6UmVmNDQ3MzkwNDQ6cmVmcy90YWdzL3Y0LjEzLjE="
  },
  {
    "name": "v4.13.0",
    "zipball_url": "https://api.github.com/repos/src-d/go-git/zipball/refs/tags/v4.13.0",
    "tarball_url": "https://api.github.com/repos/src-d/go-git/tarball/refs/tags/v4.13.0",
    "commit": {
      "sha": "6241d0e70427cb0db4ca00182717af88f638268c",
      "url": "https://api.github.com/repos/src-d/go-git/commits/6241d0e70427cb0db4ca00182717af88f638268c"
    },
    "node_id": "MDM6UmVmNDQ3MzkwNDQ6cmVmcy90YWdzL3Y0LjEzLjA="
  },
  ...
]

CodePudding user response:

As @medasx indicated, you can get the tags of any GitHub project using the GitHub REST API.

It will return the tags in JSON format, which you can then analyze somehow.

For instance, if the tags are Semantic Versions, you should be able to sort the tag names and get the most recent version.

For instance, to get an array of just the tags, using JQ for the json parsing:

curl https://api.github.com/repos/src-d/go-git/tags | jq '[.[]|.name]'

To sort them and get the last one:

curl https://api.github.com/repos/src-d/go-git/tags | jq '[.[]|.name|sort|.[-1]'
  • Related