Github's tag method returns a list of all tags pushed to your repo with the latest tag listed at the top. Here's an example call: https://api.github.com/repos/ff0000/rosy/tags which produces the following json object.
[{
name: "rbp-folder-rename",
zipball_url: "https://api.github.com/repos/ff0000/rosy/zipball/rbp-folder-rename",
tarball_url: "https://api.github.com/repos/ff0000/rosy/tarball/rbp-folder-rename",
commit: {
sha: "09ebda2678d932a005fc86ab78f6c04eebdcd50d",
url: "https://api.github.com/repos/ff0000/rosy/commits/09ebda2678d932a005fc86ab78f6c04eebdcd50d"
}
},
{
name: "2.0.10",
zipball_url: "https://api.github.com/repos/ff0000/rosy/zipball/2.0.10",
tarball_url: "https://api.github.com/repos/ff0000/rosy/tarball/2.0.10",
commit: {
sha: "fe284c7d461107d9d08d2d4dcb676759f9485fc1",
url: "https://api.github.com/repos/ff0000/rosy/commits/fe284c7d461107d9d08d2d4dcb676759f9485fc1"
}
},
// ....
{
name: "2.1.5",
zipball_url: "https://api.github.com/repos/ff0000/rosy/zipball/2.1.5",
tarball_url: "https://api.github.com/repos/ff0000/rosy/tarball/2.1.5",
commit: {
sha: "db92baa49657b3c3d27b1475c415c19525cb2118",
url: "https://api.github.com/repos/ff0000/rosy/commits/db92baa49657b3c3d27b1475c415c19525cb2118"
}
}]
master branch only? I'd like to programmatically retrieve the latest stable version of a repo.Any help / insight would be appreciated.
To get the latest version number:
https://api.github.com/repos/user/repo/releases/latest
example, for this repo (https://github.com/pyIDM/PyIDM) you can use below url:
https://api.github.com/repos/pyidm/pyidm/releases/latest
you will get a json file with tag_name=latest version
This list appears to have the latest tag at the top, followed by a history of previous tags listed in reverse chronological order.
You shouldn't depend on the order in which the GitHub API returns tag, because there are no timestamps, and the repo contributors might have used inconsistent tag names, e.g. v1.9.0 and 2.5.14. In that particular example, v1.9.0 will show up first - see this repo.
You should bug the maintainers to use consistent tags (example), and sort GitHub's output anyway 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).
var gitHubPath = 'ff0000/rosy'; // your example repo
var url = 'https://api.github.com/repos/' + gitHubPath + '/tags';
$.get(url).done(function (data) {
var versions = data.sort(function (v1, v2) {
return 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>
GitHub releases have a prerelease flag, which can be true or false. If you define "stable" as prerelease: false, then you can fetch the releases, filter for prerelease: false and sort.
var gitHubPath = 'idorecall/selection-menu'; // your example repo doesn't have releases
var url = 'https://api.github.com/repos/' + gitHubPath + '/releases';
$.get(url).done(function (data) {
var releases = data.filter(function (release) {
return !release.prerelease;
})
releases = releases.sort(function (v1, v2) {
return Date.parse(v2.published_at) - Date.parse(v1.published_at);
});
console.log(releases[0]);
$('#result').html(releases[0].name);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<p>Latest release name: <span id="result"></span></p>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With