[英]How do I get the latest tag value from the github API for a given repository
I can get the latest commit from the GitHub api using :我可以使用以下命令从 GitHub api 获取最新提交:
$ curl 'https://api.github.com/repos/dwkns/test/commits?per_page=1'
However the resulting JSON doesn't contain any reference to the tag I created when I did that commit.但是,生成的 JSON 不包含对我在提交时创建的标签的任何引用。
I can get a list of tags using :我可以使用以下方法获取标签列表:
$ curl 'https://api.github.com/repos/dwkns/test/tags'
However the resulting JSON, while it contains the names of tags I want, is not in the order in which they were created - there is no way of telling which tag is the latest one.然而,生成的 JSON 虽然包含我想要的标签名称,但与它们的创建顺序不同 - 无法判断哪个标签是最新的。
EDIT : The latest tag created was LatestLatestLatest
编辑:创建的最新标签是LatestLatestLatest
My question then is what API call(s) do I need to do to get the name of the latest tag in my repository?我的问题是我需要执行哪些 API 调用才能获取存储库中最新标记的名称?
Using jq
in combination with curl
you can have a pretty straightforward command:将jq
与curl
结合使用,您可以获得一个非常简单的命令:
curl -s \
-H "Accept: application/vnd.github.v3+json" \
https://api.github.com/repos/dwkns/test/tags \
| jq -r '.[0].name'
Output (as of today):输出(截至今天):
v56
Explanation on jq
command: jq
命令说明:
-r
is for "raw", avoid json quotes on jq's output -r
用于“raw”,避免在 jq 的输出中使用 json 引号.[0]
selects the first (latest) tag object
in json array
we got from github .[0]
选择我们从 github 获得的 json array
的第一个(最新)标签object
.name
selects the name
property in this lastest json object
.name
选择这个最新的 json object
的name
属性#!/bin/sh
curl -s https://github.com/dwkns/test/tags |
awk '/tag-name/{print $3;exit}' FS='[<>]'
Or或者
#!/bin/awk -f
BEGIN {
FS = "[<>]"
while ("curl -s https://github.com/dwkns/test/tags" | getline) {
if(/tag-name/){print $3;exit}
}
}
NOTE: If you're in a hurry and don't need all the fine details explained, just jump down to " The Solution " and execute the command.注意:如果您很着急并且不需要解释所有细节,只需跳到“解决方案”并执行命令。
This solution uses curl
and grep
to match the LATEST semantically versioned release number.此解决方案使用curl
和grep
来匹配最新的语义版本号。 An example will be demonstrated using my own Github repo " pi-ap " (a pile of bash scripts which automates config of a Raspberry Pi into a wireless AP).将使用我自己的 Github 存储库“ pi-ap ”(一堆 bash 脚本,可将 Raspberry Pi 自动配置为无线 AP)来演示一个示例。
You can test the example I give you on the CLI and after you're satisfied it works as intended, you can tweak it to your own use-case.您可以在 CLI 上测试我给您的示例,在您对它按预期工作感到满意后,您可以将其调整为您自己的用例。
Since we're using grep
to match the version number, I need to explain its' construction.由于我们使用grep
来匹配版本号,因此我需要解释它的构造。 3 pairs of integers separated by 2 dots and prefaced by a "v":由 2 个点分隔并以“v”开头的 3 对整数:
vXX.XX.XX
^ ^ ^
| | |
| | Patch
| Minor
Major
NOTE: If a field only has a single digit, I'll pad it with a zero to ensure the resulting format is predictable: always 3 pairs of integers separated by 2 dots.注意:如果一个字段只有一个数字,我会用零填充它以确保结果格式是可预测的:总是 3 对整数,由 2 个点分隔。
Github Username: F1Linux Github 用户名: F1Linux
Github Repo Name: pi-ap (NOTE: exclude the " .git " suffix) Github Repo 名称: pi-ap (注意:排除“ .git ”后缀)
curl -s 'https://github.com/f1linux/pi-ap/tags/'|grep -Eo "$Version v[0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2}"|sort -r|head -n1
In your browser, go to:在浏览器中,转到:
https://github.com/f1linux/pi-ap/tags
And validate that the latest tag was returned from the command.并验证从命令返回的最新标记。
The above is fairly extensible for most use-cases.对于大多数用例,上述内容是相当可扩展的。 Just need to change the user & repo names and remove/replace the "v" if you don't use this convention in tagging your repos.如果您在标记您的存储库时不使用此约定,只需要更改用户和存储库名称并删除/替换“v”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.