From f747f0983b8bd7d182869c830ae39ea75894453b Mon Sep 17 00:00:00 2001 From: Dibir Magomedsaygitov <61747324+dibir-magomedsaygitov@users.noreply.github.com> Date: Mon, 7 Feb 2022 17:19:09 +0300 Subject: [PATCH] [macOS] Implement helper to get GitHub package download URL (#4923) --- images/macos/provision/core/powershell.sh | 3 +-- images/macos/provision/core/stack.sh | 23 +++++++++++------------ images/macos/provision/core/swiftlint.sh | 2 +- images/macos/provision/utils/utils.sh | 23 +++++++++++++++++++++++ 4 files changed, 36 insertions(+), 15 deletions(-) diff --git a/images/macos/provision/core/powershell.sh b/images/macos/provision/core/powershell.sh index d674fccb3..1083df085 100644 --- a/images/macos/provision/core/powershell.sh +++ b/images/macos/provision/core/powershell.sh @@ -2,8 +2,7 @@ source ~/utils/utils.sh echo Installing PowerShell... -psRelease=$(curl -H "Authorization: token $API_PAT" -s "https://api.github.com/repos/PowerShell/PowerShell/releases/latest") -psDownloadUrl=$(echo $psRelease | jq -r '.assets[].browser_download_url | select(contains("osx-x64.pkg"))' | head -n 1) +psDownloadUrl=$(get_github_package_download_url "PowerShell" "PowerShell" "contains(\"osx-x64.pkg\")" "latest" "$API_PAT") download_with_retries $psDownloadUrl "/tmp" "powershell.pkg" # Work around the issue on macOS Big Sur 11.5 or higher for possible error message ("can't be opened because Apple cannot check it for malicious software") when installing the package diff --git a/images/macos/provision/core/stack.sh b/images/macos/provision/core/stack.sh index 7dfa8328d..4b4815c8b 100644 --- a/images/macos/provision/core/stack.sh +++ b/images/macos/provision/core/stack.sh @@ -2,24 +2,23 @@ source ~/utils/utils.sh echo "Get the latest Stack version..." -StackRelease=$(curl -H "Authorization: token $API_PAT" -s "https://api.github.com/repos/commercialhaskell/stack/releases/latest") -DownloadUrl=$(echo $StackRelease | jq -r '.assets[].browser_download_url | select(contains("osx-x86_64.tar.gz"))' | head -n 1) -StackVersion=$(echo $StackRelease | jq -r '.name' | cut -c2-) -StackArchive="/tmp/stack.tar.gz" +stackDownloadUrl=$(get_github_package_download_url "commercialhaskell" "stack" "contains(\"osx-x86_64.tar.gz\")" "latest" "$API_PAT") +stackVersion=$(echo $stackDownloadUrl | cut -d "/" -f8 | tr -d "v") +stackArchive="/tmp/stack.tar.gz" -echo "Download stack version $StackVersion..." -download_with_retries $DownloadUrl "/tmp" "stack.tar.gz" +echo "Download stack version $stackVersion..." +download_with_retries $stackDownloadUrl "/tmp" "stack.tar.gz" -StackToolcachePath="$AGENT_TOOLSDIRECTORY/stack/$StackVersion" -DestinationPath="$StackToolcachePath/x64" +stackToolcachePath="$AGENT_TOOLSDIRECTORY/stack/$stackVersion" +destinationPath="$stackToolcachePath/x64" -mkdir -p $DestinationPath +mkdir -p $destinationPath echo "Unzip stack archive..." -tar -xzf $StackArchive -C $DestinationPath --strip 1 +tar -xzf $stackArchive -C $destinationPath --strip 1 -touch $StackToolcachePath/x64.complete +touch $stackToolcachePath/x64.complete -echo "export PATH="\$PATH":$DestinationPath" >> "$HOME/.bashrc" +echo "export PATH="\$PATH":$destinationPath" >> "$HOME/.bashrc" invoke_tests "Common" "Stack" diff --git a/images/macos/provision/core/swiftlint.sh b/images/macos/provision/core/swiftlint.sh index 4fc0b62ed..fa3c65820 100644 --- a/images/macos/provision/core/swiftlint.sh +++ b/images/macos/provision/core/swiftlint.sh @@ -2,7 +2,7 @@ source ~/utils/utils.sh echo "Install SwiftLint" -swiftlintUrl=$(curl -H "Authorization: token $API_PAT" -s "https://api.github.com/repos/realm/SwiftLint/releases/latest" | jq -r '.assets[].browser_download_url | select(contains("portable_swiftlint.zip"))') +swiftlintUrl=$(get_github_package_download_url "realm" "SwiftLint" "contains(\"portable_swiftlint.zip\")" "latest" "$API_PAT") download_with_retries $swiftlintUrl "/tmp" "portable_swiftlint.zip" unzip -q "/tmp/portable_swiftlint.zip" -d /usr/local/bin # Remove the LICENSE file that comes along with the binary and the downloaded archive diff --git a/images/macos/provision/utils/utils.sh b/images/macos/provision/utils/utils.sh index 2e2789667..2bd88647c 100755 --- a/images/macos/provision/utils/utils.sh +++ b/images/macos/provision/utils/utils.sh @@ -171,3 +171,26 @@ configure_user_tccdb () { local sqlQuery="INSERT OR IGNORE INTO access VALUES($values);" sqlite3 "$dbPath" "$sqlQuery" } + +get_github_package_download_url() { + local REPO_OWNER=$1 + local REPO_NAME=$2 + local FILTER=$3 + local VERSION=$4 + local API_PAT=$5 + local SEARCH_IN_COUNT="100" + + [ -n "$API_PAT" ] && authString=(-H "Authorization: token ${API_PAT}") + + json=$(curl "${authString[@]}" -s "https://api.github.com/repos/${REPO_OWNER}/${REPO_NAME}/releases?per_page=${SEARCH_IN_COUNT}") + + if [[ "$VERSION" == "latest" ]]; then + tagName=$(echo $json | jq -r '.[] | select(.prerelease==false).tag_name' | sort --unique --version-sort | egrep -v ".*-[a-z]" | tail -1) + else + tagName=$(echo $json | jq -r '.[] | select(.prerelease==false).tag_name' | sort --unique --version-sort | egrep -v ".*-[a-z]" | egrep "\w*${VERSION}" | tail -1) + fi + + downloadUrl=$(echo $json | jq -r ".[] | select(.tag_name==\"${tagName}\").assets[].browser_download_url | select(${FILTER})" | head -n 1) + + echo $downloadUrl +} \ No newline at end of file