简体   繁体   中英

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.

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, eg 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>

You could simply replace the tag by the branch name:

https://api.github.com/repos/ff0000/rosy/zipball/master

See the more general form of that query (for a given branch) in " Is there anyway to programmatically fetch a zipball of private github repo? ".

But that presume that the latest stable version of a repo is in master (it could be the latest 'development', which might not be very stable past the fact that it compile and passes basic unit test): each project can have its own convention.

For what it's worth, https://api.github.com/repos/ff0000/rosy/branches would list the branches for that same repo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM