[英]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. Github 的tag 方法返回推送到你的仓库的所有标签的列表,最新的标签列在顶部。 Here's an example call: https://api.github.com/repos/ff0000/rosy/tags which produces the following json object.
这是一个示例调用: 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"
}
}]
master
branch only?master
分支的最新版本? 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:例如,对于此 repo ( https://github.com/pyIDM/PyIDM ),您可以使用以下网址:
https://api.github.com/repos/pyidm/pyidm/releases/latest
you will get a json file with tag_name=latest version你会得到一个带有 tag_name=latest version 的 json 文件
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
.你不应该依赖在GitHub的API返回的标签,因为没有时间戳和回购贡献者可能使用不一致的标签名称,例如为了在
v1.9.0
和2.5.14
。 In that particular example, v1.9.0
will show up first - see this repo .在该特定示例中,
v1.9.0
将首先出现 - 请参阅此 repo 。
You should bug the maintainers to use consistent tags ( example ), and sort GitHub's output anyway according to semver rules.你应该让维护者使用一致的标签(示例),并根据semver规则对 GitHub 的输出进行排序。 Since these rules are non-trivial (see point 11 at that link), it's better to use the semver library ( ported for the browser ).
由于这些规则非常重要(请参阅该链接上的第 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 releases have a prerelease
flag, which can be true or false. GitHub 版本有一个
prerelease
标志,它可以是 true 或 false。 If you define "stable" as prerelease: false
, then you can fetch the releases, filter for prerelease: false
and sort.如果您将“稳定”定义为
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>
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? ".请参阅“是否有任何以编程方式获取私有 github 存储库的 zipball? ”中该查询的更一般形式(对于给定分支)。
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.但这假设仓库的最新稳定版本在
master
(它可能是最新的“开发”,在它编译并通过基本单元测试之后可能不是很稳定):每个项目都可以有自己的约定.
For what it's worth, https://api.github.com/repos/ff0000/rosy/branches
would list the branches for that same repo.对于它的价值,
https://api.github.com/repos/ff0000/rosy/branches
会列出同一个 repo 的分支。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.