aboutsummaryrefslogtreecommitdiff
path: root/src/github-cli/install.sh
diff options
context:
space:
mode:
Diffstat (limited to 'src/github-cli/install.sh')
-rwxr-xr-xsrc/github-cli/install.sh49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/github-cli/install.sh b/src/github-cli/install.sh
index b46a6a2..65bbfc4 100755
--- a/src/github-cli/install.sh
+++ b/src/github-cli/install.sh
@@ -158,6 +158,46 @@ find_version_from_git_tags() {
echo "${variable_name}=${!variable_name}"
}
+# Use semver logic to decrement a version number then look for the closest match
+find_prev_version_from_git_tags() {
+ local variable_name=$1
+ local current_version=${!variable_name}
+ local repository=$2
+ # Normally a "v" is used before the version number, but support alternate cases
+ local prefix=${3:-"tags/v"}
+ # Some repositories use "_" instead of "." for version number part separation, support that
+ local separator=${4:-"."}
+ # Some tools release versions that omit the last digit (e.g. go)
+ local last_part_optional=${5:-"false"}
+ # Some repositories may have tags that include a suffix (e.g. actions/node-versions)
+ local version_suffix_regex=$6
+ # Try one break fix version number less if we get a failure. Use "set +e" since "set -e" can cause failures in valid scenarios.
+ set +e
+ major="$(echo "${current_version}" | grep -oE '^[0-9]+' || echo '')"
+ minor="$(echo "${current_version}" | grep -oP '^[0-9]+\.\K[0-9]+' || echo '')"
+ breakfix="$(echo "${current_version}" | grep -oP '^[0-9]+\.[0-9]+\.\K[0-9]+' 2>/dev/null || echo '')"
+
+ if [ "${minor}" = "0" ] && [ "${breakfix}" = "0" ]; then
+ ((major=major-1))
+ declare -g ${variable_name}="${major}"
+ # Look for latest version from previous major release
+ find_version_from_git_tags "${variable_name}" "${repository}" "${prefix}" "${separator}" "${last_part_optional}"
+ # Handle situations like Go's odd version pattern where "0" releases omit the last part
+ elif [ "${breakfix}" = "" ] || [ "${breakfix}" = "0" ]; then
+ ((minor=minor-1))
+ declare -g ${variable_name}="${major}.${minor}"
+ # Look for latest version from previous minor release
+ find_version_from_git_tags "${variable_name}" "${repository}" "${prefix}" "${separator}" "${last_part_optional}"
+ else
+ ((breakfix=breakfix-1))
+ if [ "${breakfix}" = "0" ] && [ "${last_part_optional}" = "true" ]; then
+ declare -g ${variable_name}="${major}.${minor}"
+ else
+ declare -g ${variable_name}="${major}.${minor}.${breakfix}"
+ fi
+ fi
+ set -e
+}
# Fall back on direct download if no apt package exists
# Fetches .deb file to be installed with dpkg
@@ -171,6 +211,15 @@ install_deb_using_github() {
mkdir -p /tmp/ghcli
pushd /tmp/ghcli
wget https://github.com/cli/cli/releases/download/v${CLI_VERSION}/${cli_filename}
+ exit_code=$?
+ set -e
+ if [ "$exit_code" != "0" ]; then
+ # Handle situation where git tags are ahead of what was is available to actually download
+ echo "(!) github-cli version ${CLI_VERSION} failed to download. Attempting to fall back one version to retry..."
+ find_prev_version_from_git_tags CLI_VERSION https://github.com/cli/cli
+ wget https://github.com/cli/cli/releases/download/v${CLI_VERSION}/${cli_filename}
+ fi
+
dpkg -i /tmp/ghcli/${cli_filename}
popd
rm -rf /tmp/ghcli