简体   繁体   English

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

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

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?有没有办法以编程方式检索仅应用于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.02.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>

Getting the latest "stable" release获取最新的“稳定”版本

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.

相关问题 如何使用JitPack从GitHub获取最新版本? - how to grab the latest release from GitHub with JitPack? 如何确保我运行的是来自 GitHub 的最新版本的代码 - How can I make sure I am running the latest version of the code from GitHub Github API - 如何查看 Repo 中文件的差异? - Github API - How can I see the Diff of a File in the Repo? 如何使用 CI 的 Git API 读取 GitHub 存储库中的所有分支? - How can I read all the branches in a GitHub repo using the Git API from CI? 如何使用Composer发布稳定版本 - How can I publish stable version with composer 如何获得我的 github 存储库的最新预发布版本 - bash - How can I get the latest pre-release release for my github repo - bash 如何直接将文件从 colab 复制到 github 存储库? (可以将笔记本保存在 Github 存储库中) - How can I copy a file from colab to github repo directly? (It is possible to save the notebook in the Github repo) 如何从 GitHub 获取最新版本的代码? - How to get the latest version of code from GitHub? 如何从 API 捕获项目的最新 GitHub 版本? - How do I capture the latest GitHub release of a project from the API? 如何验证来自 github 操作的 github 令牌来自哪个 github 存储库? - How can I verify which github repo a github token from a github action is coming from?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM