如何从 Github API 获取最新版本的提交哈希
How can I get the commit hash of the latest release from Github API
我想从回购中的 Github 获取最新标记版本的 git 提交哈希。是否可以通过 GitHub API 获取它?
这将很有帮助,如果不仅对于下面的用例,我需要使用 Git 提交哈希(而不是标签)来下载 vscode 服务器代码。
这是我尝试获取 microsoft/vscode 最新提交哈希的代码:
https://github.com/b01/faith-works-t-shirts/blob/main/.docker/download-vs-code-server.sh
使用 Github Rest v3
您需要使用 get latest release API:
获取标签
GET /repos/{owner}/{repo}/releases/latest
示例:https://api.github.com/repos/mui-org/material-ui/releases/latest
然后使用get标签获取标签sha API:
GET /repos/{owner}/{repo}/git/ref/tags/{tag_name}
示例:https://api.github.com/repos/mui-org/material-ui/git/ref/tags/v4.11.3
如果type
是commit
,标签sha指向一个提交,否则你需要调用:
GET /repos/{owner}/{repo}/git/tags/{tag_sha}
并获得 object.sha
https://api.github.com/repos/$repo/git/tags/$tag_sha
使用 curl and jq 的示例:
repo=microsoft/vscode
tag=$(curl -s "https://api.github.com/repos/$repo/releases/latest" | jq -r '.tag_name')
read type tag_sha < <(echo $(curl -s "https://api.github.com/repos/$repo/git/ref/tags/$tag" |
jq -r '.object.type,.object.sha'))
if [ $type == "commit" ]; then
echo "commit sha: $tag_sha"
else
sha=$(curl -s "https://api.github.com/repos/$repo/git/tags/$tag_sha" | jq '.object.sha')
echo "commit sha: $sha"
fi
GraphQL v4
使用Graphql API v4,更直接:
{
repository(owner: "microsoft", name: "vscode") {
latestRelease{
tagCommit {
oid
}
}
}
}
repo=material-ui
owner=mui-org
token=YOUR_TOKEN
curl -s -H "Authorization: token $token" \
-H "Content-Type:application/json" \
-d '{
"query": "{repository(owner:\"'"$owner"'\", name:\"'"$repo"'\"){latestRelease{tagCommit {oid}}}}"
}' https://api.github.com/graphql | jq -r '.data.repository.latestRelease.tagCommit.oid'
我想从回购中的 Github 获取最新标记版本的 git 提交哈希。是否可以通过 GitHub API 获取它?
这将很有帮助,如果不仅对于下面的用例,我需要使用 Git 提交哈希(而不是标签)来下载 vscode 服务器代码。
这是我尝试获取 microsoft/vscode 最新提交哈希的代码: https://github.com/b01/faith-works-t-shirts/blob/main/.docker/download-vs-code-server.sh
使用 Github Rest v3
您需要使用 get latest release API:
获取标签GET /repos/{owner}/{repo}/releases/latest
示例:https://api.github.com/repos/mui-org/material-ui/releases/latest
然后使用get标签获取标签sha API:
GET /repos/{owner}/{repo}/git/ref/tags/{tag_name}
示例:https://api.github.com/repos/mui-org/material-ui/git/ref/tags/v4.11.3
如果type
是commit
,标签sha指向一个提交,否则你需要调用:
GET /repos/{owner}/{repo}/git/tags/{tag_sha}
并获得 object.sha
https://api.github.com/repos/$repo/git/tags/$tag_sha
使用 curl and jq 的示例:
repo=microsoft/vscode
tag=$(curl -s "https://api.github.com/repos/$repo/releases/latest" | jq -r '.tag_name')
read type tag_sha < <(echo $(curl -s "https://api.github.com/repos/$repo/git/ref/tags/$tag" |
jq -r '.object.type,.object.sha'))
if [ $type == "commit" ]; then
echo "commit sha: $tag_sha"
else
sha=$(curl -s "https://api.github.com/repos/$repo/git/tags/$tag_sha" | jq '.object.sha')
echo "commit sha: $sha"
fi
GraphQL v4
使用Graphql API v4,更直接:
{
repository(owner: "microsoft", name: "vscode") {
latestRelease{
tagCommit {
oid
}
}
}
}
repo=material-ui
owner=mui-org
token=YOUR_TOKEN
curl -s -H "Authorization: token $token" \
-H "Content-Type:application/json" \
-d '{
"query": "{repository(owner:\"'"$owner"'\", name:\"'"$repo"'\"){latestRelease{tagCommit {oid}}}}"
}' https://api.github.com/graphql | jq -r '.data.repository.latestRelease.tagCommit.oid'