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:
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