dwkns
dwkns

Reputation: 2449

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 :

$ 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.

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.

EDIT : The latest tag created was 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?

Upvotes: 3

Views: 3438

Answers (3)

F1Linux
F1Linux

Reputation: 4373

Semantic Versioning Example

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. 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).

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.

Versioning Format Construction:

Since we're using grep to match the version number, I need to explain its' construction. 3 pairs of integers separated by 2 dots and prefaced by a "v":

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.

The Solution:

Github Username: F1Linux
Github Repo Name: pi-ap (NOTE: exclude the ".git" suffix)

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

Validate the Result Correct:

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.

Upvotes: 2

François Maturel
François Maturel

Reputation: 6162

Using jq in combination with curl you can have a pretty straightforward command:

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:

  • -r is for "raw", avoid json quotes on jq's output
  • .[0] selects the first (latest) tag object in json array we got from github
  • .name selects the name property in this lastest json object

Upvotes: 2

Zombo
Zombo

Reputation: 1

#!/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}
  }
}

Upvotes: 0

Related Questions