Reputation: 415
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!
Upvotes: 1
Views: 947
Reputation: 1381
Given an environment variable GITHUB_API_SIMONE_TOKEN
you can get it with:
latest := fetchLatestTag("emicklei/pgtalk")
and the function definition:
/*
*
curl \
-H "Accept: application/vnd.github+json" \
-H "Authorization: Bearer <TOKEN>" \
-H "X-GitHub-Api-Version: 2022-11-28" \
https://api.github.com/repos/ORG/REPO/tags
*
*/
func fetchLatestTag(orgreposlug string) string {
req, err := http.NewRequest("GET", fmt.Sprintf("https://api.github.com/repos/%s/tags", orgreposlug), nil)
if err != nil {
log.Println("versioncheck failed:", err)
return ""
}
req.Header.Set("Accept", "application/vnd.github+json")
req.Header.Set("Authorization", fmt.Sprintf("Bearer %s", os.Getenv("GITHUB_API_SIMONE_TOKEN")))
req.Header.Set("X-GitHub-Api-Version", "2022-11-28")
resp, err := http.DefaultClient.Do(req)
if err != nil {
log.Println("versioncheck failed:", err)
return ""
}
if resp.StatusCode != http.StatusOK {
log.Println("versioncheck failed:", resp.Status)
return ""
}
defer resp.Body.Close()
type tagEntry struct {
Name string `json:"name"`
}
entries := []tagEntry{}
err = json.NewDecoder(resp.Body).Decode(&entries)
if err != nil {
log.Println("versioncheck failed:", err)
return ""
}
if resp.StatusCode != http.StatusOK {
log.Println("versioncheck failed:", resp.Status)
return ""
}
if len(entries) == 0 {
log.Println("versioncheck failed: no tags")
return ""
}
return entries[0].Name
}
which today gives:
v0.29.1
Upvotes: 0
Reputation: 3874
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]'
Upvotes: 0
Reputation: 634
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="
},
...
]
Upvotes: 1