Is there a way to get the latest tag of a given repo using github API v3
GitHub doesn't have an API to retrieve the latest tag, as it has for retrieving the latest release. That might be because tags could be arbitrary strings, not necessarily semvers, but it's not really an excuse, since tags have timestamps, and GitHub does sort tags lexicographically when returning them via its Tags API.
Anyway, to get the latest tag, you need to call that API, then sort the tags according to semver rules. Since these rules are non-trivial (see point 11 at that link), it's better to use the semver library (ported for the browser).
const gitHubPath = 'dandv/local-iso-dt'; // example repoconst url = 'https://api.github.com/repos/' + gitHubPath + '/tags';$.get(url).done(data => { const versions = data.sort((v1, v2) => semver.compare(v2.name, v1.name)); $('#result').html(versions[0].name);});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><script src="https://rawgit.com/hippich/bower-semver/master/semver.min.js"></script><p>Latest tag: <span id="result"></span></p>
You could consider, as an alternative to the GitHub API, a simple script mentioned in "Is there a simple way to “git describe
” a remote repository?" (source, by svnpenn
):
#!/usr/bin/awk -fBEGIN { if (ARGC != 2) { print "git-describe-remote.awk https://github.com/stedolan/jq" exit } FS = "[ /^]+" while ("git ls-remote " ARGV[1] "| sort -Vk2" | getline) { if (!sha) sha = substr($0, 1, 7) tag = $3 } while ("curl -s " ARGV[1] "/releases/tag/" tag | getline) if ($3 ~ "commits") com = $2 printf com ? "%s-%s-g%s\n" : "%s\n", tag, com, sha}
That does extract the tag (and more), without having to clone the repo.
Note: as commented below by Joels Elf, make sure /usr/bin/awk
refers to gawk
, not mawk
.
You can get the latest release at https://api.github.com/repos/$org/$repo/releases/latest
If you want just the tag name, you can try something like this:
curl https://api.github.com/repos/$org/$repo/releases/latest -s | jq .name -r