Retrieve latest tag from GitHub with Go
To monitor the latest activity of Git repositories, I was using the go-github golang package to interface with GitHub’s API. While the package API is very rich and powerful, I was still missing one feature: just getting the latest tag of a specific repository. It does have an option to get the latest release, which is GitHub’s way of promoting a tag, however these are not native to Git itself.
However, the API provides a function to get all tags (Github, Godoc):
Let’s get started with our imports:
and initialize a new GitHub client:
Since we do not use any features requiring authentication to the GitHub APIs, we can simply pass
nil as an
http.Client during initialization (https://godoc.org/github.com/google/go-github/github#hdr-Authentication).
Next, we fetch all tags available:
ListTags gets a context to run in, the name of the repository owner (either a username or an organization), the name of the repository (only the name itself, not the full URL) and some pagination options, which we don’t need here and therefore we pass
ListTags returns the tags as an array of
*RepositoryTags, GitHub’s raw response and an error, if any. We are not interested in the response here, therefore discard it with
_, however we do absolutely need to handle the error.
GitHub sorts the tags automagically for us, therefore the latest tag is stored in the first element of the array. However, do not make the same mistake I initially made: forgetting to check if there are even any elements in the array!
If you are looking for a function to split a full Git URI into only the “owner” and “repository” part (to pass those along to
ListTags for instance), here is one I wrote:
Feel free to use wherever you like, suggestions for improvement are very welcome!