git 有没有办法使用 github API v3 获取给定 repo 的最新标签
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29109673/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to get the latest tag of a given repo using github API v3
提问by Varun Kumar Rao Ponugoti
I am relatively new to the github api and I am struggling to get the latest tag of a given repo.
我对 github api 比较陌生,我正在努力获取给定 repo 的最新标签。
Q:Why I need that ?
问:为什么我需要那个?
A:As a QA I am responsible for testing and releasing to LIVE and our team owns around 40 artefacts(repos in github). I want to build a tool which lists the projects which have commits after that latest tag. So that I can manage releases more efficiently.
A:作为 QA,我负责测试并发布到 LIVE,我们的团队拥有大约 40 个人工制品(在 github 中的存储库)。我想构建一个工具,列出在最新标签之后提交的项目。这样我就可以更有效地管理发布。
Coming to the point.
进入正题。
According to Github api to get all tags of a give repo is
根据 Github api 获取给定 repo 的所有标签是
GET /repos/:owner/:repo/tags
But this gives the full list of tags the repo has.
但这给出了 repo 的完整标签列表。
Is there an easy way to find the latest tag without iterating through the all available tags from the above api call?
有没有一种简单的方法可以在不遍历上述 api 调用中的所有可用标签的情况下找到最新的标签?
If I have iterate through each tag in order to find the latest tag (based on timestamp of each tag)thats clearly going to be not the efficient way of doing this as the time goes the number of tags will increase and since I want to repeat the same process for at least more than 10 repos.
如果我遍历每个标签以找到最新的标签(基于每个标签的时间戳),随着时间的推移,这显然不是这样做的有效方法,标签的数量会增加,因为我想重复至少 10 个以上的回购采用相同的流程。
Any help will be highly appreciated.
任何帮助将不胜感激。
Many thanks in advance
提前谢谢了
回答by Dan Dascalescu
GitHub doesn't have an API to retrieve the latest tag, as it has for retrieving the latest release. That might be because tags could be arbitrary strings, no necessarily semvers, but it's not really an excuse, since tags have timestamps, and GitHub does sort tags lexicographically when returning them via its Tags API.
GitHub 没有用于检索最新标签的 API,因为它具有检索最新版本的 API 。那可能是因为标签可以是任意字符串,不一定是 semvers,但这并不是真正的借口,因为标签有时间戳,而且 GitHub 在通过其Tags API返回标签时会按字典顺序对标签进行排序。
Anyway, to get the latest tag, you need to call that API, then sort the tags according to semverrules. Since these rules are non-trivial (see point 11 at that link), it's better to use the semver library(ported for the browser).
无论如何,要获取最新的标签,您需要调用该 API,然后根据semver规则对标签进行排序。由于这些规则非常重要(请参阅该链接上的第 11 点),因此最好使用semver 库(为浏览器移植)。
var gitHubPath = 'iDoRecall/selection-menu'; // 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>
回答by VonC
You could consider, as an alternative to the GitHub API, a simple script mentioned in "Is there a simple way to “git describe
” a remote repository?" (source, by svnpenn
):
作为 GitHub API 的替代方案,您可以考虑使用“是否有一种简单的方法来“ git describe
”远程存储库?”(来源,作者svnpenn
)中提到的简单脚本:
#!/usr/bin/awk -f
BEGIN {
if (ARGC != 2) {
print "git-describe-remote.awk https://github.com/stedolan/jq"
exit
}
FS = "[ /^]+"
while ("git ls-remote " ARGV[1] "| sort -Vk2" | getline) {
if (!sha)
sha = substr(curl https://api.github.com/repos/$org/$repo/releases/latest -s | jq .name -r
, 1, 7)
tag =
}
while ("curl -s " ARGV[1] "/releases/tag/" tag | getline)
if ( ~ "commits")
com =
printf com ? "%s-%s-g%s\n" : "%s\n", tag, com, sha
}
That does extract the tag (and more), without having to clone the repo.
这确实提取了标签(以及更多),而无需克隆 repo。
Note: as commented belowby Joels Elf, make sure /usr/bin/awk
refers to gawk
, not mawk
.
回答by Mathieu
You can get the latest release at https://api.github.com/repos/$org/$repo/releases/latest
您可以在以下位置获得最新版本 https://api.github.com/repos/$org/$repo/releases/latest
If you want just the tag name, you can try something like this:
如果你只想要标签名称,你可以尝试这样的事情:
{
repository(owner: "bertrandmartel", name: "caipy-dashboard") {
refs(refPrefix: "refs/tags/", first: 1, orderBy: {field: TAG_COMMIT_DATE, direction: DESC}) {
edges {
node {
name
target {
oid
... on Tag {
message
commitUrl
tagger {
name
email
date
}
}
}
}
}
}
}
}
回答by Bertrand Martel
Using GraphQL API v4, you can get the last tag by alphabetical or commit date. For instance by commit date (eg the tag that point to the most recent commit) :
使用GraphQL API v4,您可以按字母顺序或提交日期获取最后一个标签。例如按提交日期(例如,指向最近提交的标签):
{
repository(owner: "google", name: "gson") {
refs(refPrefix: "refs/tags/", first: 100, orderBy: {field: TAG_COMMIT_DATE, direction: DESC}) {
edges {
node {
name
target {
oid
... on Tag {
commitUrl
tagger {
date
}
}
}
}
}
}
}
}
Note that if the tag is a lightweight tag, there will be no tagger
or message
field. You can also use field: ALPHABETICAL
for the last tag in alphabetical order.
请注意,如果标签是轻量级标签,则不会有tagger
或message
字段。您还可以field: ALPHABETICAL
按字母顺序用于最后一个标签。
If you want to get the last tag that was created(which could be different from the tag that point to the most recent commit, or the last tag in alphabetical order), it's only possible for annotated tag since their creation date is stored and will be available in the tagger
field.
如果您想获取创建的最后一个标签(它可能不同于指向最近提交的标签,或按字母顺序排列的最后一个标签),则只能使用带注释的标签,因为它们的创建日期已存储并且将可在tagger
现场使用。
To get the last created tag, you would get all the tags and filter the most recent date in data.repository.refs.edges.node.target.tagger.date
field in the response of the following request :
要获取最后创建的标签,您将获取所有标签并data.repository.refs.edges.node.target.tagger.date
在以下请求的响应中过滤字段中的最新日期: