@@ -24,30 +24,46 @@ check_packages() {
2424 fi
2525}
2626
27- # Figure out correct version of a three part version number is not passed
27+ # Resolve "latest" version by following the GitHub redirect (no API rate limit)
28+ resolve_latest_version () {
29+ local latest_url=" https://github.com/microsoft/sbom-tool/releases/latest"
30+ local redirect_url
31+ redirect_url=$( curl -sIL -o /dev/null -w ' %{url_effective}' " ${latest_url} " )
32+ if [ -z " ${redirect_url} " ] || [ " ${redirect_url} " = " ${latest_url} " ]; then
33+ echo " ERROR: Failed to resolve latest sbom-tool version from GitHub." >&2
34+ exit 1
35+ fi
36+ # Extract tag from redirect URL (e.g. .../releases/tag/v4.1.5 -> v4.1.5)
37+ echo " ${redirect_url##*/ } "
38+ }
39+
40+ # Validate that a given version/tag exists by checking the download URL returns 200
2841validate_version_exists () {
2942 local variable_name=$1
30- local requested_version=$2
31- if [ " ${requested_version} " = " latest" ]; then requested_version=$( curl -sL https://api.github.com/repos/microsoft/sbom-tool/releases/latest | jq -r " .tag_name" ) ; fi
32- local version_list
33- version_list=$( curl -sL https://api.github.com/repos/microsoft/sbom-tool/releases | jq -r " .[].tag_name" )
34- if [ -z " ${variable_name} " ] || ! echo " ${version_list} " | grep " ${requested_version} " > /dev/null 2>&1 ; then
35- echo -e " Invalid ${variable_name} value: ${requested_version} \nValid values:\n${version_list} " >&2
43+ local requested_version=$2
44+ local check_url=" https://github.com/microsoft/sbom-tool/releases/tag/${requested_version} "
45+ local http_code
46+ http_code=$( curl -sIL -o /dev/null -w ' %{http_code}' " ${check_url} " )
47+ if [ " ${http_code} " != " 200" ]; then
48+ echo " ERROR: ${variable_name} value '${requested_version} ' not found (HTTP ${http_code} )." >&2
49+ echo " Check available versions at: https://github.com/microsoft/sbom-tool/releases" >&2
3650 exit 1
3751 fi
3852 echo " ${variable_name} =${requested_version} "
3953}
4054
4155# make sure we have curl
42- check_packages curl jq ca-certificates libicu-dev
56+ check_packages curl ca-certificates libicu-dev
4357
4458# Normalize version: add 'v' prefix if missing
4559if [ " ${SBOM_TOOL_VERSION} " != " latest" ] && [[ " ${SBOM_TOOL_VERSION} " != v* ]]; then
4660 SBOM_TOOL_VERSION=" v${SBOM_TOOL_VERSION} "
4761fi
4862
49- # make sure version is available
50- if [ " ${SBOM_TOOL_VERSION} " = " latest" ]; then SBOM_TOOL_VERSION=$( curl -sL https://api.github.com/repos/microsoft/sbom-tool/releases/latest | jq -r " .tag_name" ) ; fi
63+ # Resolve latest or validate the requested version
64+ if [ " ${SBOM_TOOL_VERSION} " = " latest" ]; then
65+ SBOM_TOOL_VERSION=$( resolve_latest_version)
66+ fi
5167validate_version_exists SBOM_TOOL_VERSION " ${SBOM_TOOL_VERSION} "
5268
5369# download and install binary
0 commit comments