xref: /aosp_15_r20/external/dagger2/util/latest-dagger-version.sh (revision f585d8a307d0621d6060bd7e80091fdcbf94fe27)
1#!/bin/bash
2
3set -eu
4
5function github-rest-api {
6  local GITHUB_REST_API=$1
7  local GITHUB_API_HEADER_ACCEPT="Accept: application/vnd.github.v3+json"
8  # Grab the GH_TOKEN or else default to an empty string.
9  local GITHUB_TOKEN="${GH_TOKEN:-}"
10
11  if [ -z "$GITHUB_TOKEN" ]; then
12    curl -s $GITHUB_REST_API -H $GITHUB_API_HEADER_ACCEPT
13  else
14    curl -s $GITHUB_REST_API \
15      -H $GITHUB_API_HEADER_ACCEPT \
16      -H "authorization: Bearer $GITHUB_TOKEN"
17  fi
18}
19
20function github-latest-release-tag {
21  local REPO_NAME=$1
22
23  # Grab the last two latest releases:
24  # (We skip the latest release if we haven't set release notes yet).
25  local RELEASE_API="https://api.github.com/repos/$REPO_NAME/releases?per_page=2"
26
27  # This gets the latest release info (as json) from github.
28  local RELEASE_JSON=$(github-rest-api $RELEASE_API)
29
30  # This pulls out the "body" from the json (i.e. the release notes)
31  local RELEASE_NOTES=$(echo $RELEASE_JSON | jq '.[0].body')
32
33  if [ "$RELEASE_NOTES" ]
34  then
35    # Return the latest release tag
36    echo $RELEASE_JSON | jq '.[0].tag_name'
37  else
38    # If there are no release notes in the latest release then we use the
39    # 2nd most latest version since we don't want to update the version until
40    # the release notes are set.
41    echo "Ignoring the latest release since the release notes have not been set."
42    echo "Using the previous release's version as latest."
43
44    # Return the 2nd most recent release tag
45    echo $RELEASE_JSON | jq '.[1].tag_name'
46  fi
47}
48
49function dagger-latest-release {
50  # Get the latest Dagger release tag, e.g. "dagger-2.31.2" or "dagger-2.32"
51  local DAGGER_RELEASE_TAG=$(github-latest-release-tag "google/dagger")
52
53  # Converts the "tag_name" to a version, e.g. "dagger-2.32" => "2.32"
54  echo $DAGGER_RELEASE_TAG | grep -oP "(?<=dagger-)\d+\.\d+(\.\d+)?"
55}
56
57type jq >/dev/null 2>&1 || {
58  echo >&2 "jq is not installed.  Try 'sudo apt-get install jq'.";
59  exit 1;
60}
61
62dagger-latest-release
63