Skip to content

Commit

Permalink
Enhanced software release retrieval
Browse files Browse the repository at this point in the history
Updated:
- Web::upgradeRelease
- Web::getReleases
- Github::installRelease

Changes:
- support for gh
- allows to override version place holder using env variable VERSION_PLACEHOLDER
  • Loading branch information
fchastanet committed Jan 19, 2025
1 parent f27f9f9 commit 355197b
Show file tree
Hide file tree
Showing 11 changed files with 373 additions and 334 deletions.
45 changes: 32 additions & 13 deletions bin/doc
Original file line number Diff line number Diff line change
Expand Up @@ -1544,12 +1544,27 @@ Version::parse() {
Web::getReleases() {
local releaseListUrl="$1"
# Get latest release from GitHub api
Retry::parameterized "${RETRY_MAX_RETRY:-5}" "${RETRY_DELAY_BETWEEN_RETRIES:-15}" "Retrieving release versions list ..." curl \
-L \
--connect-timeout "${CURL_CONNECT_TIMEOUT:-5}" \
--fail \
--silent \
"${releaseListUrl}"
if command -v gh &>/dev/null && [[ -n "${GH_TOKEN}" ]]; then
Log::displayDebug "Using gh to retrieve release versions list"
Retry::parameterized "${RETRY_MAX_RETRY:-5}" \
"${RETRY_DELAY_BETWEEN_RETRIES:-15}" \
"Retrieving release versions list ..." \
gh api "${releaseListUrl#https://github.com}"
else
if command -v gh &>/dev/null && [[ "${GH_WARNING_DISPLAYED:-0}" = "0" ]]; then
Log::displayWarning "GH_TOKEN is not set, cannot use gh, using curl to retrieve release versions list"
GH_WARNING_DISPLAYED=1
fi
Retry::parameterized "${RETRY_MAX_RETRY:-5}" \
"${RETRY_DELAY_BETWEEN_RETRIES:-15}" \
"Retrieving release versions list ..." \
curl \
-L \
--connect-timeout "${CURL_CONNECT_TIMEOUT:-5}" \
--fail \
--silent \
"${releaseListUrl}"
fi
}


Expand All @@ -1567,6 +1582,7 @@ Web::getReleases() {
# @env PARSE_VERSION_CALLBACK a callback to parse the version of the existing command
# @env INSTALL_CALLBACK a callback to install the software downloaded
# @env CURL_CONNECT_TIMEOUT number of seconds before giving up host connection
# @env VERSION_PLACEHOLDER a placeholder to replace in downloadReleaseUrl (default: @latestVersion@)
Web::upgradeRelease() {
local targetFile="$1"
local releasesUrl="$2"
Expand All @@ -1577,25 +1593,28 @@ Web::upgradeRelease() {
local filterLastVersionCallback="${FILTER_LAST_VERSION_CALLBACK:-Version::parse}"
local softVersionCallback="${SOFT_VERSION_CALLBACK:-Version::getCommandVersionFromPlainText}"
local installCallback="${INSTALL_CALLBACK:-}"
local latestVersion
latestVersion="$(Web::getReleases "${releasesUrl}" | ${filterLastVersionCallback})" || {
Log::displayError "latest version not found on ${releasesUrl}"
return 1
}
Log::displayInfo "Latest version found is ${latestVersion}"

local currentVersion="not existing"
if [[ -f "${targetFile}" ]]; then
currentVersion="$(${softVersionCallback} "${targetFile}" "${softVersionArg}" 2>&1 || true)"
fi
if [[ -z "${exactVersion}" ]]; then
local latestVersion
latestVersion="$(Web::getReleases "${releasesUrl}" | ${filterLastVersionCallback})" || {
Log::displayError "latest version not found on ${releasesUrl}"
return 1
}
Log::displayInfo "Latest version found is ${latestVersion}"

exactVersion="${latestVersion}"
fi
local url="${downloadReleaseUrl//@latestVersion@/${exactVersion}}"
local url="${downloadReleaseUrl//${VERSION_PLACEHOLDER:-@latestVersion@}/${exactVersion}}"
if [[ -n "${exactVersion}" ]] && ! Github::isReleaseVersionExist "${url}"; then
Log::displayError "${targetFile} version ${exactVersion} doesn't exist on github"
return 2
fi
Log::displayDebug "currentVersion: '${currentVersion}'"
Log::displayDebug "exactVersion: '${exactVersion}'"
if [[ "${currentVersion}" = "${exactVersion}" ]]; then
Log::displayInfo "${targetFile} version ${exactVersion} already installed"
else
Expand Down
67 changes: 54 additions & 13 deletions bin/dockerLint
Original file line number Diff line number Diff line change
Expand Up @@ -612,6 +612,19 @@ Log::displaySuccess() {
}


# @description Display message using warning color (yellow)
# @arg $1 message:String the message to display
# @env DISPLAY_DURATION int (default 0) if 1 display elapsed time information between 2 info logs
# @env LOG_CONTEXT String allows to contextualize the log
Log::displayWarning() {
if ((BASH_FRAMEWORK_DISPLAY_LEVEL >= __LEVEL_WARNING)); then
Log::computeDuration
echo -e "${__WARNING_COLOR}WARN - ${LOG_CONTEXT:-}${LOG_LAST_DURATION_STR:-}${1}${__RESET_COLOR}" >&2
fi
Log::logWarning "$1"
}


# @description Display message using error color (red) and exit immediately with error status 1
# @arg $1 message:String the message to display
# @env DISPLAY_DURATION int (default 0) if 1 display elapsed time information between 2 info logs
Expand Down Expand Up @@ -704,6 +717,15 @@ Log::logSuccess() {
}


# @description log message to file
# @arg $1 message:String the message to display
Log::logWarning() {
if ((BASH_FRAMEWORK_LOG_LEVEL >= __LEVEL_WARNING)); then
Log::logMessage "${2:-WARNING}" "$1"
fi
}


# @description activate or not Log::display* and Log::log* functions
# based on BASH_FRAMEWORK_DISPLAY_LEVEL and BASH_FRAMEWORK_LOG_LEVEL
# environment variables loaded by Env::requireLoad
Expand Down Expand Up @@ -1074,12 +1096,27 @@ Version::parse() {
Web::getReleases() {
local releaseListUrl="$1"
# Get latest release from GitHub api
Retry::parameterized "${RETRY_MAX_RETRY:-5}" "${RETRY_DELAY_BETWEEN_RETRIES:-15}" "Retrieving release versions list ..." curl \
-L \
--connect-timeout "${CURL_CONNECT_TIMEOUT:-5}" \
--fail \
--silent \
"${releaseListUrl}"
if command -v gh &>/dev/null && [[ -n "${GH_TOKEN}" ]]; then
Log::displayDebug "Using gh to retrieve release versions list"
Retry::parameterized "${RETRY_MAX_RETRY:-5}" \
"${RETRY_DELAY_BETWEEN_RETRIES:-15}" \
"Retrieving release versions list ..." \
gh api "${releaseListUrl#https://github.com}"
else
if command -v gh &>/dev/null && [[ "${GH_WARNING_DISPLAYED:-0}" = "0" ]]; then
Log::displayWarning "GH_TOKEN is not set, cannot use gh, using curl to retrieve release versions list"
GH_WARNING_DISPLAYED=1
fi
Retry::parameterized "${RETRY_MAX_RETRY:-5}" \
"${RETRY_DELAY_BETWEEN_RETRIES:-15}" \
"Retrieving release versions list ..." \
curl \
-L \
--connect-timeout "${CURL_CONNECT_TIMEOUT:-5}" \
--fail \
--silent \
"${releaseListUrl}"
fi
}


Expand All @@ -1097,6 +1134,7 @@ Web::getReleases() {
# @env PARSE_VERSION_CALLBACK a callback to parse the version of the existing command
# @env INSTALL_CALLBACK a callback to install the software downloaded
# @env CURL_CONNECT_TIMEOUT number of seconds before giving up host connection
# @env VERSION_PLACEHOLDER a placeholder to replace in downloadReleaseUrl (default: @latestVersion@)
Web::upgradeRelease() {
local targetFile="$1"
local releasesUrl="$2"
Expand All @@ -1107,25 +1145,28 @@ Web::upgradeRelease() {
local filterLastVersionCallback="${FILTER_LAST_VERSION_CALLBACK:-Version::parse}"
local softVersionCallback="${SOFT_VERSION_CALLBACK:-Version::getCommandVersionFromPlainText}"
local installCallback="${INSTALL_CALLBACK:-}"
local latestVersion
latestVersion="$(Web::getReleases "${releasesUrl}" | ${filterLastVersionCallback})" || {
Log::displayError "latest version not found on ${releasesUrl}"
return 1
}
Log::displayInfo "Latest version found is ${latestVersion}"

local currentVersion="not existing"
if [[ -f "${targetFile}" ]]; then
currentVersion="$(${softVersionCallback} "${targetFile}" "${softVersionArg}" 2>&1 || true)"
fi
if [[ -z "${exactVersion}" ]]; then
local latestVersion
latestVersion="$(Web::getReleases "${releasesUrl}" | ${filterLastVersionCallback})" || {
Log::displayError "latest version not found on ${releasesUrl}"
return 1
}
Log::displayInfo "Latest version found is ${latestVersion}"

exactVersion="${latestVersion}"
fi
local url="${downloadReleaseUrl//@latestVersion@/${exactVersion}}"
local url="${downloadReleaseUrl//${VERSION_PLACEHOLDER:-@latestVersion@}/${exactVersion}}"
if [[ -n "${exactVersion}" ]] && ! Github::isReleaseVersionExist "${url}"; then
Log::displayError "${targetFile} version ${exactVersion} doesn't exist on github"
return 2
fi
Log::displayDebug "currentVersion: '${currentVersion}'"
Log::displayDebug "exactVersion: '${exactVersion}'"
if [[ "${currentVersion}" = "${exactVersion}" ]]; then
Log::displayInfo "${targetFile} version ${exactVersion} already installed"
else
Expand Down
66 changes: 33 additions & 33 deletions bin/installFacadeExample
Original file line number Diff line number Diff line change
Expand Up @@ -516,39 +516,39 @@ Log::logFatal() {
# FUNCTIONS

facade_main_installFacadeExamplesh() {
FRAMEWORK_ROOT_DIR="$(cd "${CURRENT_DIR}/.." && pwd -P)"
FRAMEWORK_SRC_DIR="${FRAMEWORK_ROOT_DIR}/src"
FRAMEWORK_BIN_DIR="${FRAMEWORK_ROOT_DIR}/bin"
FRAMEWORK_VENDOR_DIR="${FRAMEWORK_ROOT_DIR}/vendor"
FRAMEWORK_VENDOR_BIN_DIR="${FRAMEWORK_ROOT_DIR}/vendor/bin"
# REQUIRES
Env::requireLoad
Log::requireLoad
UI::requireTheme
Compiler::Facade::requireCommandBinDir

# @require Compiler::Facade::requireCommandBinDir

install() {
echo "installation in progress"
}

local action=$1
shift || true
case ${action} in
install)
install "$@"
;;
*)
if Assert::functionExists defaultFacadeAction; then
defaultFacadeAction "$1" "$@"
else
Log::displayError "invalid action requested: ${action}"
exit 1
fi
;;
esac
exit 0
FRAMEWORK_ROOT_DIR="$(cd "${CURRENT_DIR}/.." && pwd -P)"
FRAMEWORK_SRC_DIR="${FRAMEWORK_ROOT_DIR}/src"
FRAMEWORK_BIN_DIR="${FRAMEWORK_ROOT_DIR}/bin"
FRAMEWORK_VENDOR_DIR="${FRAMEWORK_ROOT_DIR}/vendor"
FRAMEWORK_VENDOR_BIN_DIR="${FRAMEWORK_ROOT_DIR}/vendor/bin"
# REQUIRES
Env::requireLoad
Log::requireLoad
UI::requireTheme
Compiler::Facade::requireCommandBinDir

# @require Compiler::Facade::requireCommandBinDir

install() {
echo "installation in progress"
}

local action=$1
shift || true
case ${action} in
install)
install "$@"
;;
*)
if Assert::functionExists defaultFacadeAction; then
defaultFacadeAction "$1" "$@"
else
Log::displayError "invalid action requested: ${action}"
exit 1
fi
;;
esac
exit 0
}

# if file is sourced avoid calling main function
Expand Down
45 changes: 32 additions & 13 deletions bin/installRequirements
Original file line number Diff line number Diff line change
Expand Up @@ -1172,12 +1172,27 @@ Version::parse() {
Web::getReleases() {
local releaseListUrl="$1"
# Get latest release from GitHub api
Retry::parameterized "${RETRY_MAX_RETRY:-5}" "${RETRY_DELAY_BETWEEN_RETRIES:-15}" "Retrieving release versions list ..." curl \
-L \
--connect-timeout "${CURL_CONNECT_TIMEOUT:-5}" \
--fail \
--silent \
"${releaseListUrl}"
if command -v gh &>/dev/null && [[ -n "${GH_TOKEN}" ]]; then
Log::displayDebug "Using gh to retrieve release versions list"
Retry::parameterized "${RETRY_MAX_RETRY:-5}" \
"${RETRY_DELAY_BETWEEN_RETRIES:-15}" \
"Retrieving release versions list ..." \
gh api "${releaseListUrl#https://github.com}"
else
if command -v gh &>/dev/null && [[ "${GH_WARNING_DISPLAYED:-0}" = "0" ]]; then
Log::displayWarning "GH_TOKEN is not set, cannot use gh, using curl to retrieve release versions list"
GH_WARNING_DISPLAYED=1
fi
Retry::parameterized "${RETRY_MAX_RETRY:-5}" \
"${RETRY_DELAY_BETWEEN_RETRIES:-15}" \
"Retrieving release versions list ..." \
curl \
-L \
--connect-timeout "${CURL_CONNECT_TIMEOUT:-5}" \
--fail \
--silent \
"${releaseListUrl}"
fi
}


Expand All @@ -1195,6 +1210,7 @@ Web::getReleases() {
# @env PARSE_VERSION_CALLBACK a callback to parse the version of the existing command
# @env INSTALL_CALLBACK a callback to install the software downloaded
# @env CURL_CONNECT_TIMEOUT number of seconds before giving up host connection
# @env VERSION_PLACEHOLDER a placeholder to replace in downloadReleaseUrl (default: @latestVersion@)
Web::upgradeRelease() {
local targetFile="$1"
local releasesUrl="$2"
Expand All @@ -1205,25 +1221,28 @@ Web::upgradeRelease() {
local filterLastVersionCallback="${FILTER_LAST_VERSION_CALLBACK:-Version::parse}"
local softVersionCallback="${SOFT_VERSION_CALLBACK:-Version::getCommandVersionFromPlainText}"
local installCallback="${INSTALL_CALLBACK:-}"
local latestVersion
latestVersion="$(Web::getReleases "${releasesUrl}" | ${filterLastVersionCallback})" || {
Log::displayError "latest version not found on ${releasesUrl}"
return 1
}
Log::displayInfo "Latest version found is ${latestVersion}"

local currentVersion="not existing"
if [[ -f "${targetFile}" ]]; then
currentVersion="$(${softVersionCallback} "${targetFile}" "${softVersionArg}" 2>&1 || true)"
fi
if [[ -z "${exactVersion}" ]]; then
local latestVersion
latestVersion="$(Web::getReleases "${releasesUrl}" | ${filterLastVersionCallback})" || {
Log::displayError "latest version not found on ${releasesUrl}"
return 1
}
Log::displayInfo "Latest version found is ${latestVersion}"

exactVersion="${latestVersion}"
fi
local url="${downloadReleaseUrl//@latestVersion@/${exactVersion}}"
local url="${downloadReleaseUrl//${VERSION_PLACEHOLDER:-@latestVersion@}/${exactVersion}}"
if [[ -n "${exactVersion}" ]] && ! Github::isReleaseVersionExist "${url}"; then
Log::displayError "${targetFile} version ${exactVersion} doesn't exist on github"
return 2
fi
Log::displayDebug "currentVersion: '${currentVersion}'"
Log::displayDebug "exactVersion: '${exactVersion}'"
if [[ "${currentVersion}" = "${exactVersion}" ]]; then
Log::displayInfo "${targetFile} version ${exactVersion} already installed"
else
Expand Down
36 changes: 25 additions & 11 deletions bin/megalinter
Original file line number Diff line number Diff line change
Expand Up @@ -382,17 +382,31 @@ Github::getLatestRelease() {
resultRef=""
local resultFile
resultFile="$(mktemp -p "${TMPDIR:-/tmp}" -t githubLatestRelease.XXXX)"
# Get latest release from GitHub api
if Retry::default curl \
-L \
--connect-timeout "${CURL_CONNECT_TIMEOUT:-5}" \
-o "${resultFile}" \
--fail \
--silent \
"https://api.github.com/repos/${repo}/releases/latest"; then
# shellcheck disable=SC2034
resultRef="$(Version::githubApiExtractVersion <"${resultFile}")"
return 0
local query="repos/${repo}/releases/latest"

if command -v gh &>/dev/null && [[ -n "${GH_TOKEN}" ]]; then
Log::displayDebug "Using gh to retrieve release versions list"
Retry::parameterized "${RETRY_MAX_RETRY:-5}" \
"${RETRY_DELAY_BETWEEN_RETRIES:-15}" \
"Retrieving release versions list ..." \
gh api "${query}" >"${resultFile}"
else
if command -v gh &>/dev/null && [[ "${GH_WARNING_DISPLAYED:-0}" = "0" ]]; then
Log::displayWarning "GH_TOKEN is not set, cannot use gh, using curl to retrieve release versions list"
GH_WARNING_DISPLAYED=1
fi
# Get latest release from GitHub api
if Retry::default curl \
-L \
--connect-timeout "${CURL_CONNECT_TIMEOUT:-5}" \
-o "${resultFile}" \
--fail \
--silent \
"https://api.github.com/${query}"; then
# shellcheck disable=SC2034
resultRef="$(Version::githubApiExtractVersion <"${resultFile}")"
return 0
fi
fi
# display curl result in case of failure
cat >&2 "${resultFile}"
Expand Down
2 changes: 1 addition & 1 deletion src/Filters/firstField.bats
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function Filters::firstField::bash { #@test
}

function Filters::firstField::bigFile { #@test
tail -1209 "${BATS_TEST_DIRNAME}/testsData/binary" | {
tail -1170 "${BATS_TEST_DIRNAME}/testsData/binary" | {
run Filters::firstField
assert_success
assert_output 'Github::upgradeRelease'
Expand Down
Loading

0 comments on commit 355197b

Please sign in to comment.