[ubuntu] Refactor functions for GitHub assets and hashes (#9040)

This commit is contained in:
Shamil Mubarakshin
2023-12-25 19:47:58 +01:00
committed by GitHub
parent 08d10be70d
commit 694536d87f
15 changed files with 149 additions and 114 deletions

View File

@@ -61,137 +61,171 @@ get_toolset_value() {
echo "$(jq -r "$query" $toolset_path)"
}
resolve_github_release_asset_url() {
get_github_releases_by_version() {
local repo=$1
local filter=$2
local version=${3:-"*"}
local allow_pre_release=${4:-false}
local version=${2:-".+"}
local allow_pre_release=${3:-false}
local with_assets_only=${4:-false}
page_size="100"
json=$(curl -fsSL "https://api.github.com/repos/${repo}/releases?per_page=${page_size}")
if [[ $allow_pre_release == "true" ]]; then
json=$(echo $json | jq -r '.[] | select(.assets | length > 0)')
else
json=$(echo $json | jq -r '.[] | select((.prerelease==false) and (.assets | length > 0))')
fi
if [[ $version == "latest" ]]; then
tag_name=$(echo $json | jq -r '.tag_name' | sort --unique --version-sort | egrep -v ".*-[a-z]|beta" | tail -n 1)
elif [[ $version == *"*"* ]]; then
tag_name=$(echo $json | jq -r '.tag_name' | sort --unique --version-sort | egrep -v ".*-[a-z]|beta" | egrep "${version}" | tail -n 1)
else
tag_names=$(echo $json | jq -r '.tag_name' | sort --unique --version-sort | egrep -v ".*-[a-z]|beta" | egrep "${version}")
for element in $tag_names; do
semver=$(echo "$element" | awk 'match($0, /[0-9]+\.[0-9]+\.[0-9]+/) {print substr($0, RSTART, RLENGTH)}')
if [[ $semver == $version ]]; then
tag_name=$element
fi
done
fi
download_url=$(echo $json | jq -r ". | select(.tag_name==\"${tag_name}\").assets[].browser_download_url | select(${filter})" | head -n 1)
if [ -z "$download_url" ]; then
echo "Failed to parse a download url for the '${tag_name}' tag using '${filter}' filter"
if [[ -z "$json" ]]; then
echo "Failed to get releases"
exit 1
fi
echo $download_url
if [[ $with_assets_only == "true" ]]; then
json=$(echo $json | jq -r '.[] | select(.assets | length > 0)')
else
json=$(echo $json | jq -r '.[]')
fi
if [[ $allow_pre_release == "true" ]]; then
json=$(echo $json | jq -r '.')
else
json=$(echo $json | jq -r '. | select(.prerelease==false)')
fi
# Filter out rc/beta/etc releases, convert to numeric version and sort
json=$(echo $json | jq '. | select(.tag_name | test(".*-[a-z]|beta") | not)' | jq '.tag_name |= gsub("[^\\d.]"; "")' | jq -s 'sort_by(.tag_name | split(".") | map(tonumber))')
# Select releases matching version
if [[ $version == "latest" ]]; then
json_filtered=$(echo $json | jq .[-1])
elif [[ $version == *"+"* ]] || [[ $version == *"*"* ]]; then
json_filtered=$(echo $json | jq --arg version $version '.[] | select(.tag_name | test($version))')
else
json_filtered=$(echo $json | jq --arg version $version '.[] | select(.tag_name | contains($version))')
fi
if [[ -z "$json_filtered" ]]; then
echo "Failed to get releases from ${repo} matching version ${version}"
echo "Available versions: $(echo "$json" | jq -r '.tag_name')"
exit 1
fi
echo $json_filtered
}
get_github_package_hash() {
local repo_owner=$1
local repo_name=$2
local file_name=$3
local url=$4
local version=${5:-"latest"}
local prerelease=${6:-false}
local delimiter=${7:-'|'}
local word_number=${8:-2}
resolve_github_release_asset_url() {
local repo=$1
local url_filter=$2
local version=${3:-".+"}
local allow_pre_release=${4:-false}
local allow_multiple_matches=${5:-false}
matching_releases=$(get_github_releases_by_version "${repo}" "${version}" "${allow_pre_release}" "true")
matched_url=$(echo $matching_releases | jq -r ".assets[].browser_download_url | select(${url_filter})")
if [[ -z "$matched_url" ]]; then
echo "Found no download urls matching pattern: ${url_filter}"
echo "Available download urls: $(echo "$matching_releases" | jq -r '.assets[].browser_download_url')"
exit 1
fi
if [[ "$(echo "$matched_url" | wc -l)" -gt 1 ]]; then
if [[ $allow_multiple_matches == "true" ]]; then
matched_url=$(echo "$matched_url" | tail -n 1)
else
echo "Multiple matches found for ${version} version and ${url_filter} URL filter. Please make filters more specific"
exit 1
fi
fi
echo $matched_url
}
get_checksum_from_github_release() {
local repo=$1
local file_name=$2
local version=${3:-".+"}
local hash_type=$4
local allow_pre_release=${5:-false}
if [[ -z "$file_name" ]]; then
echo "File name is not specified."
exit 1
fi
if [[ -n "$url" ]]; then
release_url="$url"
if [[ "$hash_type" == "SHA256" ]]; then
hash_pattern="[A-Fa-f0-9]{64}"
elif [[ "$hash_type" == "SHA512" ]]; then
hash_pattern="[A-Fa-f0-9]{128}"
else
if [ "$version" == "latest" ]; then
release_url="https://api.github.com/repos/${repo_owner}/${repo_name}/releases/latest"
else
json=$(curl -fsSL "https://api.github.com/repos/${repo_owner}/${repo_name}/releases?per_page=100")
tags=$(echo "$json" | jq -r --arg prerelease "$prerelease" '.[] | select(.prerelease == ($prerelease | test("true"; "i"))) | .tag_name')
tag=$(echo "$tags" | grep -o "$version")
if [[ "$(echo "$tag" | wc -l)" -gt 1 ]]; then
echo "Multiple tags found matching the version $version. Please specify a more specific version."
exit 1
fi
if [[ -z "$tag" ]]; then
echo "Failed to get a tag name for version $version."
exit 1
fi
release_url="https://api.github.com/repos/${repo_owner}/${repo_name}/releases/tags/$tag"
fi
fi
body=$(curl -fsSL "$release_url" | jq -r '.body' | tr -d '`')
matching_line=$(echo "$body" | grep "$file_name")
if [[ "$(echo "$matching_line" | wc -l)" -gt 1 ]]; then
echo "Multiple lines found included the file $file_name. Please specify a more specific file name."
exit 1
fi
if [[ -z "$matching_line" ]]; then
echo "File name '$file_name' not found in release body."
echo "Unknown hash type: ${hash_type}"
exit 1
fi
result=$(echo "$matching_line" | cut -d "$delimiter" -f "$word_number" | tr -d -c '[:alnum:]')
if [[ -z "$result" ]]; then
echo "Empty result. Check parameters delimiter and/or word_number for the matching line."
matching_releases=$(get_github_releases_by_version "${repo}" "${version}" "${allow_pre_release}" "true")
matched_line=$(printf "$(echo $matching_releases | jq '.body')\n" | grep "$file_name")
if [[ -z "$matched_line" ]]; then
echo "File name ${file_name} not found in release body"
exit 1
fi
echo "$result"
if [[ "$(echo "$matched_line" | wc -l)" -gt 1 ]]; then
echo "Multiple matches found for ${file_name} in release body: ${matched_line}"
exit 1
fi
hash=$(echo $matched_line | grep -oP "$hash_pattern")
if [[ -z "$hash" ]]; then
echo "Found ${file_name} in body of release, but failed to get hash from it: ${matched_line}"
exit 1
fi
echo "$hash"
}
get_hash_from_remote_file() {
get_checksum_from_url() {
local url=$1
local keywords=("$2" "$3")
local delimiter=${4:-' '}
local word_number=${5:-1}
local file_name=$2
local hash_type=$3
local use_custom_search_pattern=${4:-false}
local delimiter=${5:-' '}
local word_number=${6:-1}
if [[ -z "${keywords[0]}" || -z "$url" ]]; then
echo "File name and/or URL is not specified."
if [[ "$hash_type" == "SHA256" ]]; then
hash_pattern="[A-Fa-f0-9]{64}"
elif [[ "$hash_type" == "SHA512" ]]; then
hash_pattern="[A-Fa-f0-9]{128}"
else
echo "Unknown hash type: ${hash_type}"
exit 1
fi
matching_line=$(curl -fsSL "$url" | sed 's/ */ /g' | tr -d '`')
for keyword in "${keywords[@]}"; do
matching_line=$(echo "$matching_line" | grep "$keyword")
done
checksums_file_path=$(download_with_retry "$url")
checksums=$(cat "$checksums_file_path")
rm "$checksums_file_path"
if [[ "$(echo "$matching_line" | wc -l)" -gt 1 ]]; then
echo "Multiple lines found including the words: ${keywords[*]}. Please use a more specific filter."
matched_line=$(printf "$checksums\n" | grep "$file_name")
if [[ "$(echo "$matched_line" | wc -l)" -gt 1 ]]; then
echo "Found multiple lines matching file name ${file_name} in checksum file."
exit 1
fi
if [[ -z "$matching_line" ]]; then
echo "Keywords (${keywords[*]}) not found in the file with hashes."
if [[ -z "$matched_line" ]]; then
echo "File name ${file_name} not found in checksum file."
exit 1
fi
result=$(echo "$matching_line" | cut -d "$delimiter" -f "$word_number" | tr -d -c '[:alnum:]')
if [[ ${#result} -ne 64 && ${#result} -ne 128 ]]; then
echo "Invalid result length. Expected 64 or 128 characters. Please check delimiter and/or word_number parameters."
echo "Result: $result"
if [[ $use_custom_search_pattern == "true" ]]; then
hash=$(echo "$matched_line" | sed 's/ */ /g' | cut -d "$delimiter" -f "$word_number" | tr -d -c '[:alnum:]')
else
hash=$(echo $matched_line | grep -oP "$hash_pattern")
fi
if [[ -z "$hash" ]]; then
echo "Found ${file_name} in checksum file, but failed to get hash from it: ${matched_line}"
exit 1
fi
echo "$result"
echo "$hash"
}
use_checksum_comparison() {