问题
I am relatively new to the github api and I am struggling to get the latest tag of a given 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.
Coming to the point.
According to Github api to get all tags of a give repo is
GET /repos/:owner/:repo/tags
But this gives the full list of tags the repo has.
Is there an easy way to find the latest tag without iterating through the all available tags from the above api call?
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.
Any help will be highly appreciated.
Many thanks in advance
回答1:
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.
Anyway, to get the latest tag, you need to call that API, then sort the tags according to semver rules. Since these rules are non-trivial (see point 11 at that link), it's better to use the semver library (ported for the browser).
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>
回答2:
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):
#!/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($0, 1, 7)
tag = $3
}
while ("curl -s " ARGV[1] "/releases/tag/" tag | getline)
if ($3 ~ "commits")
com = $2
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.
Note: as commented below by Joels Elf, make sure /usr/bin/awk
refers to gawk
, not mawk
.
回答3:
You can get the latest release at https://api.github.com/repos/$org/$repo/releases/latest
If you want just the tag name, you can try something like this:
curl https://api.github.com/repos/$org/$repo/releases/latest -s | jq .name -r
回答4:
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) :
{
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
}
}
}
}
}
}
}
}
test it in the explorer
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.
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.
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 :
{
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
}
}
}
}
}
}
}
}
test it in the explorer
来源:https://stackoverflow.com/questions/29109673/is-there-a-way-to-get-the-latest-tag-of-a-given-repo-using-github-api-v3