fix(版本計算): 改用分頁取得所有 release 並以 semver 取最新穩定版
This commit is contained in:
+75
-20
@@ -4,6 +4,7 @@ set -euo pipefail
|
||||
|
||||
readonly LINE="=================================================="
|
||||
readonly SUBLINE="--------------------------------------------------"
|
||||
readonly RELEASES_PER_PAGE=10
|
||||
|
||||
section() {
|
||||
printf '\n%s\n%s\n%s\n' "$LINE" "$1" "$SUBLINE"
|
||||
@@ -53,13 +54,17 @@ latest_stable_version() {
|
||||
fi
|
||||
|
||||
printf '%s' "$release_json" | jq -r '
|
||||
if type == "array" then
|
||||
[ .[] | select(.tag_name? and (.tag_name | test("-beta\\.") | not)) | .tag_name ][0]
|
||||
// "v0.0.0"
|
||||
else
|
||||
"v0.0.0"
|
||||
end
|
||||
| sub("^v"; "")
|
||||
( if type == "array" then . else [] end )
|
||||
| [ .[]
|
||||
| select(.tag_name? and (.tag_name | test("-beta\\.") | not))
|
||||
| .tag_name
|
||||
| sub("^v"; "")
|
||||
| select(test("^[0-9]+(\\.[0-9]+)*$"))
|
||||
| split(".")
|
||||
| map(tonumber)
|
||||
]
|
||||
| if length == 0 then [0, 0, 0] else (sort | last) end
|
||||
| "\(.[0] // 0).\(.[1] // 0).\(.[2] // 0)"
|
||||
'
|
||||
}
|
||||
|
||||
@@ -124,13 +129,17 @@ calculate_version() {
|
||||
|
||||
printf '%s' "$release_json" | jq -r --arg is_beta "$is_beta" '
|
||||
def stable_version:
|
||||
if type == "array" then
|
||||
[ .[] | select(.tag_name? and (.tag_name | test("-beta\\.") | not)) | .tag_name ][0]
|
||||
// "v0.0.0"
|
||||
else
|
||||
"v0.0.0"
|
||||
end
|
||||
| sub("^v"; "");
|
||||
( if type == "array" then . else [] end )
|
||||
| [ .[]
|
||||
| select(.tag_name? and (.tag_name | test("-beta\\.") | not))
|
||||
| .tag_name
|
||||
| sub("^v"; "")
|
||||
| select(test("^[0-9]+(\\.[0-9]+)*$"))
|
||||
| split(".")
|
||||
| map(tonumber)
|
||||
]
|
||||
| if length == 0 then [0, 0, 0] else (sort | last) end
|
||||
| "\(.[0] // 0).\(.[1] // 0).\(.[2] // 0)";
|
||||
|
||||
def next_release($latest):
|
||||
($latest | split(".") | map(tonumber? // 0)) as $parts
|
||||
@@ -166,6 +175,56 @@ calculate_version() {
|
||||
'
|
||||
}
|
||||
|
||||
fetch_releases() {
|
||||
local base_url="$1"
|
||||
local -a auth_args=()
|
||||
|
||||
if [ -n "${RUNNER_TOKEN:-}" ] && [ "${RUNNER_TOKEN:-}" != "null" ]; then
|
||||
info "使用授權 token 取得 release" >&2
|
||||
auth_args=(-H "Authorization: token $RUNNER_TOKEN")
|
||||
else
|
||||
info "使用匿名請求取得 release" >&2
|
||||
fi
|
||||
|
||||
local page=1
|
||||
local combined="[]"
|
||||
local page_json count
|
||||
|
||||
while :; do
|
||||
if ! page_json="$(curl -fsS ${auth_args[@]+"${auth_args[@]}"} "${base_url}?limit=${RELEASES_PER_PAGE}&page=${page}")"; then
|
||||
fail "release API 請求失敗 (page=${page})"
|
||||
fi
|
||||
|
||||
# 空字串或 null 代表已無更多資料
|
||||
if [ -z "$page_json" ] || [ "$page_json" = "null" ]; then
|
||||
break
|
||||
fi
|
||||
|
||||
if ! count="$(printf '%s' "$page_json" | jq 'if type == "array" then length else -1 end')"; then
|
||||
fail "release API 回傳資料無法解析 (page=${page})"
|
||||
fi
|
||||
|
||||
if [ "$count" = "-1" ]; then
|
||||
fail "release API 回傳非陣列資料 (page=${page})"
|
||||
fi
|
||||
|
||||
info "第 ${page} 頁取得 ${count} 筆 release" >&2
|
||||
|
||||
if ! combined="$(jq -n --argjson acc "$combined" --argjson page "$page_json" '$acc + $page')"; then
|
||||
fail "合併 release 資料失敗 (page=${page})"
|
||||
fi
|
||||
|
||||
# 不足一頁代表已取完
|
||||
if [ "$count" -lt "$RELEASES_PER_PAGE" ]; then
|
||||
break
|
||||
fi
|
||||
|
||||
page=$((page + 1))
|
||||
done
|
||||
|
||||
printf '%s' "$combined"
|
||||
}
|
||||
|
||||
main() {
|
||||
section "參數檢查"
|
||||
|
||||
@@ -186,12 +245,8 @@ main() {
|
||||
RELEASE_URL="$GITEA_SERVER_URL/api/v1/repos/$GITEA_REPOSITORY/releases"
|
||||
info "RELEASE_URL=$RELEASE_URL"
|
||||
|
||||
if [ -n "${RUNNER_TOKEN:-}" ] && [ "${RUNNER_TOKEN:-}" != "null" ]; then
|
||||
info "使用授權 token 取得 release"
|
||||
RELEASE_JSON="$(curl -fsS -H "Authorization: token $RUNNER_TOKEN" "$RELEASE_URL")"
|
||||
else
|
||||
info "使用匿名請求取得 release"
|
||||
RELEASE_JSON="$(curl -fsS "$RELEASE_URL")"
|
||||
if ! RELEASE_JSON="$(fetch_releases "$RELEASE_URL")"; then
|
||||
fail "取得 release 資料失敗"
|
||||
fi
|
||||
|
||||
VERSION_INFO="$(calculate_version "$RELEASE_JSON" "$IS_BETA")"
|
||||
|
||||
Reference in New Issue
Block a user