Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I grab the latest "stable" repo version from the Github API?

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"
    }
}]

Questions

  1. This list appears to have the latest tag at the top, followed by a history of previous tags listed in reverse chronological order. Why? That seems odd that the first result is ordered differently then the rest, maybe I'm reading this wrong?
  2. Is there any way to programmatically retrieve the latest version applied to the master branch only? I'd like to programmatically retrieve the latest stable version of a repo.

Any help / insight would be appreciated.

like image 673
potench Avatar asked Jan 29 '26 01:01

potench


2 Answers

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

like image 91
Mahmoud Elshahat Avatar answered Feb 01 '26 07:02

Mahmoud Elshahat


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>

Getting the latest "stable" release

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>
like image 22
Dan Dascalescu Avatar answered Feb 01 '26 08:02

Dan Dascalescu



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!