From 848ddad7d4b8ffd10c81b3a0c7e210a96aabf104 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tobias=20Kn=C3=B6ppler?= <6317548+theCalcaholic@users.noreply.github.com> Date: Sun, 8 Sep 2024 23:15:20 +0200 Subject: [PATCH] library.sh: Fix resolution of target nextcloud version if requesting older version than current MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Tobias Knöppler <6317548+theCalcaholic@users.noreply.github.com> --- bin/ncp-update-nc.d/update-nc.sh | 7 +-- etc/library.sh | 13 ++++-- etc/library.test | 76 ++++++++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 etc/library.test diff --git a/bin/ncp-update-nc.d/update-nc.sh b/bin/ncp-update-nc.d/update-nc.sh index 1055971bf..b8bdd18c3 100755 --- a/bin/ncp-update-nc.d/update-nc.sh +++ b/bin/ncp-update-nc.d/update-nc.sh @@ -24,7 +24,7 @@ ncc status &>/dev/null || { [[ "$DBG" == x ]] && ncc status; echo "Next CURRENT="$(nc_version)" if [[ "$VER" == "0" ]] || [[ "$VER" == "" ]] then - REQUESTED_VERSION="${NCLATESTVER?}" + REQUESTED_VERSION="latest" else REQUESTED_VERSION="$VER" fi @@ -57,10 +57,11 @@ grep -qP "\d+\.\d+\.\d+" <<<"$CURRENT" || { echo "Malformed version $CURRENT"; e grep -qP "\d+\.\d+\.\d+" <<<"$TARGET_VERSION" || { echo "Malformed version $TARGET_VERSION" ; exit 1; } echo "Current Nextcloud version $CURRENT" -echo "Available Nextcloud version $TARGET_VERSION" +echo "Requested Nextcloud version $REQUESTED_VERSION" +echo "Selected Nextcloud version $TARGET_VERSION" if [[ "$TARGET_VERSION" != "$REQUESTED_VERSION" ]] then - echo "INFO: You have requested an update to '${REQUESTED_VERSION}', but a direct update to '${REQUESTED_VERSION}' cannot be performed, so the latest available version that can be updated to has been selected automatically." + echo "INFO: You have requested an update to '${REQUESTED_VERSION}', but a direct update to '${REQUESTED_VERSION}' cannot be performed, so the latest available version that can be updated to (${TARGET_VERSION}) has been selected automatically." fi # make sure that cron.php is not running and there are no pending jobs diff --git a/etc/library.sh b/etc/library.sh index 74eafcb14..1f031ca36 100644 --- a/etc/library.sh +++ b/etc/library.sh @@ -531,10 +531,17 @@ function determine_nc_update_version() { supported_maj="${supported%%.*}" # If valid version is requested -> direct update, don't consider anything else - if [[ "$requested" =~ ^[0-9.]*$ ]] && [[ "$requested_maj" -le "$((current_maj + 1))" ]] + if [[ "$requested" =~ ^[0-9]*.[0-9]*.[0-9]*$ ]] then - echo "$requested" - return 0 + if ! is_more_recent_than "${requested}" "${current}" + then + echo "$current" + return 0 + elif [[ "$requested_maj" -le "$((current_maj + 1))" ]] + then + echo "$requested" + return 0 + fi fi versions="$(curl -q -L -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" https://api.github.com/repos/nextcloud/server/releases?per_page=100 | jq -r '.[].tag_name' | grep -v -e 'rc.$' -e 'beta.$' | sort -V)" diff --git a/etc/library.test b/etc/library.test new file mode 100644 index 000000000..fb0da935f --- /dev/null +++ b/etc/library.test @@ -0,0 +1,76 @@ +#!/bin/bash +set -ex + +fail() { + echo "$@" + exit 1 +} + +#shellcheck source=./library.sh +. "$(dirname "$0")/library.sh" ||: + + +case=1 + +current=27.0.1 +supported=27.0.8 +requested="" + +expected=27.1.11 + +actual="$(determine_nc_update_version "$current" "$supported" "$requested")" + +[[ "$expected" == "$actual" ]] || fail "C$case: unexpected nc target version: $actual (expected: $expected)" + + +case=2 + +# TODO: Set to 26.0.1 +current=26.0.13 +supported=27.0.8 +requested="" + +expected=27.1.11 + +actual="$(determine_nc_update_version "$current" "$supported" "$requested")" + +[[ "$expected" == "$actual" ]] || fail "C$case: unexpected nc target version: $actual (expected: $expected)" + + +case=3 + +current=26.0.1 +supported=27.0.8 +requested="27.1.3" + +expected=27.1.3 + +actual="$(determine_nc_update_version "$current" "$supported" "$requested")" + +[[ "$expected" == "$actual" ]] || fail "C$case: unexpected nc target version: $actual (expected: $expected)" + + +case=4 + +current=29.0.5 +supported=27.0.8 +requested="" + +expected=29.0.6 + +actual="$(determine_nc_update_version "$current" "$supported" "$requested")" + +[[ "$expected" == "$actual" ]] || fail "C$case: unexpected nc target version: $actual (expected: $expected)" + + +case=5 + +current=29.0.5 +supported=27.0.8 +requested="27.0.8" + +expected="29.0.5" + +actual="$(determine_nc_update_version "$current" "$supported" "$requested")" + +[[ "$expected" == "$actual" ]] || fail "C$case: unexpected nc target version: $actual (expected: $expected)"