我该如何从Github API获取最新的“稳定”版本库?

3
Github的标签方法返回一个列表,其中包含推送到您的存储库中的所有标记,最新的标记位于顶部。以下是一个示例调用:https://api.github.com/repos/ff0000/rosy/tags,它生成以下JSON对象。
[{
    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"
    }
}]

问题

  1. 这个列表似乎在顶部有最新的标签,其后是以相反的时间顺序列出的先前标签的历史记录。为什么?第一个结果与其余结果排序不同,这似乎很奇怪,也许我理解有误?
  2. 是否有任何编程方法可以仅检索应用于master分支的最新版本?我想编写程序来检索存储库的最新稳定版本。

任何帮助/见解将不胜感激。

3个回答

2

获取最新的版本号:

https://api.github.com/repos/user/repo/releases/latest

例如,对于这个仓库(https://github.com/pyIDM/PyIDM),您可以使用下面的网址:

https://api.github.com/repos/pyidm/pyidm/releases/latest

您将获得一个带有tag_name=latest version的JSON文件。


1

这个列表似乎是按照最新标签在顶部的顺序排列的,接着是以相反的时间顺序列出先前标签的历史记录。

您不应该依赖GitHub API返回标签的顺序,因为没有时间戳,而且仓库贡献者可能使用不一致的标签名称,例如v1.9.02.5.14。在这个特定的例子中,v1.9.0将首先显示-请参见this repo

你应该催促维护者使用一致的标签(示例),并且无论如何都要按照semver规则对GitHub的输出进行排序。由于这些规则并不简单(请参见该链接的第11点),最好使用semver库为浏览器移植)。

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发布有一个prerelease标志,可以是true或false。如果您将“稳定”定义为prerelease: false,则可以获取发布版本,过滤prerelease: false并进行排序。

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>


0
你可以直接用分支名称替换这个标签:
https://api.github.com/repos/ff0000/rosy/zipball/master

在 "有没有办法以编程方式获取私有github存储库的zipball" 中,可以看到该查询的更一般形式(针对给定分支)。

但是,这假设存储库的最新稳定版本在 master 中(它可能是最新的“开发”版本,除了编译和通过基本单元测试之外可能并不非常稳定):每个项目都可以有自己的约定。

就其价值而言,https://api.github.com/repos/ff0000/rosy/branches 将列出同一存储库的分支。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接