Skip to content

Instantly share code, notes, and snippets.

@jonz94
Last active October 29, 2021 10:07
Show Gist options
  • Save jonz94/6163cb33f0e0062902128d1d6fc969a0 to your computer and use it in GitHub Desktop.
Save jonz94/6163cb33f0e0062902128d1d6fc969a0 to your computer and use it in GitHub Desktop.
Get latest git tag of a github repository

Problem

GitHub REST API does have an api to get all the tags of a repo, and does have an api to get the latest release of a repo, but does not have one to get the latest tag 😔

Solution

😎 Use https://shields.io as the API!

Options

  1. Get svg and then parse it
USER="qmk"
REPO="qmk_firmware"

curl -fsSL "https://shields.io/github/v/tag/${USER}/${REPO}.svg?sort=semver" | \
  awk -F 'title>' '{print $2}' | \
  cut -d 'v' -f 2 | \
  cut -d '<' -f 1
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="86" height="20" role="img" aria-label="tag: v0.14.29">
  <title>tag: v0.14.29</title>
  <linearGradient id="s" x2="0" y2="100%">
    <stop offset="0" stop-color="#bbb" stop-opacity=".1"/>
    <stop offset="1" stop-opacity=".1"/>
  </linearGradient>
  <clipPath id="r">
    <rect width="86" height="20" rx="3" fill="#fff"/>
  </clipPath>
  <g clip-path="url(#r)">
    <rect width="27" height="20" fill="#555"/>
    <rect x="27" width="59" height="20" fill="#fe7d37"/>
    <rect width="86" height="20" fill="url(#s)"/>
  </g>
  <g fill="#fff" text-anchor="middle" font-family="Verdana,Geneva,DejaVu Sans,sans-serif" text-rendering="geometricPrecision" font-size="110">
    <text aria-hidden="true" x="145" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="170">tag</text>
    <text x="145" y="140" transform="scale(.1)" fill="#fff" textLength="170">tag</text>
    <text aria-hidden="true" x="555" y="150" fill="#010101" fill-opacity=".3" transform="scale(.1)" textLength="490">v0.14.29</text>
    <text x="555" y="140" transform="scale(.1)" fill="#fff" textLength="490">v0.14.29</text>
  </g>
</svg>
  1. Get json and then parse it
USER="qmk"
REPO="qmk_firmware"

curl -fsSL 'https://shields.io/github/v/tag/${USER}/${REPO}.json?sort=semver' | \
  awk -F 'value' '{print $2}' | \
  cut -d '"' -f 3 | \
  cut -d 'v' -f 2
{
  "label": "tag",
  "message": "v0.14.29",
  "color": "orange",
  "link": [],
  "name": "tag",
  "value": "v0.14.29"
}
  1. Obviously, there are so many tools and ways to parse svg and json 🤣, so the options is unlimited!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment