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