chore: refine logging output for clarity and conciseness in entrypoint script and README

This commit is contained in:
2026-05-15 07:32:39 +00:00
parent 8641fd32d6
commit 1114b7859f
3 changed files with 5 additions and 52 deletions
+4 -9
View File
@@ -8,7 +8,7 @@
- 預設每個套件保留最新 `2` 個版本。 - 預設每個套件保留最新 `2` 個版本。
- 直接刪除超出保留數量的舊版本。 - 直接刪除超出保留數量的舊版本。
- 只處理你指定的 NuGet 套件名稱,可一次指定多個。 - 只處理你指定的 NuGet 套件名稱,可一次指定多個。
- 輸出可搜尋的 log包含 API status、request id 與 summary。 - 輸出精簡 log只保留必要的錯誤與 summary。
- 每頁預設抓取 100 筆版本,可用 `PAGE_LIMIT` 調整。 - 每頁預設抓取 100 筆版本,可用 `PAGE_LIMIT` 調整。
## Token 來源順序 ## Token 來源順序
@@ -31,16 +31,11 @@ Action 會依序嘗試以下來源:
## Log 行為 ## Log 行為
執行時會輸出這類資訊: 執行時會輸出必要資訊:
- `Trying token from ...` - `ERROR: ...`
- `Using token from ...`
- `keep_count=...`
- `package_names=...`
- `GET /api/v1/... -> 200 OK`
- `Candidate to delete: ...`
- `Deleted package ... -> 204 No Content`
- `Summary: packages=... versions=... kept=... candidates=... deleted=... errors=...` - `Summary: packages=... versions=... kept=... candidates=... deleted=... errors=...`
- `No matching packages found for requested package_names` 之類的少量狀態訊息
## Workflow 範例 ## Workflow 範例
+1 -35
View File
@@ -33,10 +33,7 @@ url_encode() {
resolve_token() { resolve_token() {
# Resolve the already-merged token input passed in RUNNER_TOKEN. # Resolve the already-merged token input passed in RUNNER_TOKEN.
log "Trying token from RUNNER_TOKEN"
if [[ -n "${RUNNER_TOKEN:-}" ]]; then if [[ -n "${RUNNER_TOKEN:-}" ]]; then
log "Using token from RUNNER_TOKEN"
printf '%s' "${RUNNER_TOKEN}" printf '%s' "${RUNNER_TOKEN}"
return 0 return 0
fi fi
@@ -142,12 +139,6 @@ api_request() {
awk -F': *' 'tolower($1)=="x-gitea-request-id" || tolower($1)=="x-request-id" {value=$2} END {print value}' "${headers_file}" | tr -d '\r' awk -F': *' 'tolower($1)=="x-gitea-request-id" || tolower($1)=="x-request-id" {value=$2} END {print value}' "${headers_file}" | tr -d '\r'
)" )"
if [[ -n "${request_id}" ]]; then
log "${method} ${path} -> ${status_text} request_id=${request_id}"
else
log "${method} ${path} -> ${status_text}"
fi
printf '%s\t%s\t%s\n' "${http_code}" "${status_text}" "${request_id}" printf '%s\t%s\t%s\n' "${http_code}" "${status_text}" "${request_id}"
} }
@@ -242,7 +233,6 @@ collect_package_candidates() {
versions_json="$(fetch_package_versions "${owner}" "${package_name}" "${token}")" versions_json="$(fetch_package_versions "${owner}" "${package_name}" "${token}")"
if [[ "$(jq 'length' <<<"${versions_json}")" -eq 0 ]]; then if [[ "$(jq 'length' <<<"${versions_json}")" -eq 0 ]]; then
log "No versions found for package ${package_name}"
continue continue
fi fi
@@ -250,15 +240,7 @@ collect_package_candidates() {
total_versions="$(jq 'length' <<<"${versions_json}")" total_versions="$(jq 'length' <<<"${versions_json}")"
total_version_count=$((total_version_count + total_versions)) total_version_count=$((total_version_count + total_versions))
log "Package ${package_name}: total_versions=${total_versions} keep_count=${keep_count}"
log "Package ${package_name} versions (oldest -> newest):"
while IFS=$'\t' read -r version created_at; do
[[ -z "${version}" ]] && continue
log " - ${version} (${created_at})"
done < <(jq -r 'sort_by(.created_at, .version)[] | [.version, .created_at] | @tsv' <<<"${versions_json}")
if (( total_versions <= keep_count )); then if (( total_versions <= keep_count )); then
log " keep all ${total_versions} versions"
kept_count=$((kept_count + total_versions)) kept_count=$((kept_count + total_versions))
continue continue
fi fi
@@ -271,7 +253,6 @@ collect_package_candidates() {
while IFS=$'\t' read -r package version created_at; do while IFS=$'\t' read -r package version created_at; do
[[ -z "${package}" ]] && continue [[ -z "${package}" ]] && continue
log "Candidate to delete: package ${package} version ${version} (created: ${created_at})"
printf '%s\t%s\t%s\n' "${package}" "${version}" "${created_at}" >> "${candidate_file}" printf '%s\t%s\t%s\n' "${package}" "${version}" "${created_at}" >> "${candidate_file}"
candidate_count=$((candidate_count + 1)) candidate_count=$((candidate_count + 1))
done < <(jq -r '.[] | [.name, .version, .created_at] | @tsv' <<<"${candidates_json}") done < <(jq -r '.[] | [.name, .version, .created_at] | @tsv' <<<"${candidates_json}")
@@ -304,7 +285,6 @@ process_candidates() {
local body_file headers_file meta http_code status_text request_id local body_file headers_file meta http_code status_text request_id
if [[ ! -s "${candidate_file}" ]]; then if [[ ! -s "${candidate_file}" ]]; then
log "No delete candidates found"
log "Summary: packages=${package_count} versions=${total_version_count} kept=${kept_count} candidates=0 deleted=0 errors=0" log "Summary: packages=${package_count} versions=${total_version_count} kept=${kept_count} candidates=0 deleted=0 errors=0"
return 0 return 0
fi fi
@@ -323,7 +303,6 @@ process_candidates() {
IFS=$'\t' read -r http_code status_text request_id <<< "${meta}" IFS=$'\t' read -r http_code status_text request_id <<< "${meta}"
if [[ "${http_code}" =~ ^2 ]]; then if [[ "${http_code}" =~ ^2 ]]; then
log "Deleted package ${package_name} version ${version} -> ${status_text}"
deleted_count=$((deleted_count + 1)) deleted_count=$((deleted_count + 1))
else else
if [[ -n "${request_id}" ]]; then if [[ -n "${request_id}" ]]; then
@@ -342,13 +321,10 @@ process_candidates() {
main() { main() {
# Entry point for the Docker container. Resolves inputs, builds candidates, # Entry point for the Docker container. Resolves inputs, builds candidates,
# and applies deletes for the selected NuGet packages. # and applies deletes for the selected NuGet packages.
local token keep_count repository owner _repo package_names_csv local token keep_count repository owner _repo
local -a package_names local -a package_names
local candidate_file summary package_count total_version_count kept_count candidate_count local candidate_file summary package_count total_version_count kept_count candidate_count
log "Gitea Server Url: ${GITEA_SERVER_URL:-}"
log "Gitea Repository: ${GITEA_REPOSITORY:-}"
if ! token="$(resolve_token)"; then if ! token="$(resolve_token)"; then
fail "No Gitea token available, exiting" fail "No Gitea token available, exiting"
fi fi
@@ -358,11 +334,6 @@ main() {
keep_count="$(resolve_keep_count)" keep_count="$(resolve_keep_count)"
mapfile -t package_names < <(resolve_package_names) mapfile -t package_names < <(resolve_package_names)
package_names_csv="$(IFS=,; echo "${package_names[*]}")"
log "keep_count=${keep_count}"
log "package_names=${package_names_csv}"
log "Token source resolved successfully"
candidate_file="$(mktemp)" candidate_file="$(mktemp)"
cleanup_candidate_file="${candidate_file}" cleanup_candidate_file="${candidate_file}"
@@ -371,12 +342,7 @@ main() {
summary="$(collect_package_candidates "${owner}" "${keep_count}" "${candidate_file}" "${token}" "${package_names[@]}")" summary="$(collect_package_candidates "${owner}" "${keep_count}" "${candidate_file}" "${token}" "${package_names[@]}")"
IFS=$'\t' read -r package_count total_version_count kept_count candidate_count <<< "${summary}" IFS=$'\t' read -r package_count total_version_count kept_count candidate_count <<< "${summary}"
if (( package_count == 0 )); then
log "No matching packages found for requested package_names"
fi
process_candidates "${owner}" "${candidate_file}" "${package_count}" "${total_version_count}" "${kept_count}" "${candidate_count}" "${token}" process_candidates "${owner}" "${candidate_file}" "${package_count}" "${total_version_count}" "${kept_count}" "${candidate_count}" "${token}"
log "Stage 4 complete"
} }
if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then if [[ "${BASH_SOURCE[0]}" == "$0" ]]; then
-8
View File
@@ -345,7 +345,6 @@ test_api_request() {
capture_call api_request token GET "/api/v1/test" "${body_file}" "${headers_file}" capture_call api_request token GET "/api/v1/test" "${body_file}" "${headers_file}"
assert_eq $'200\t200 OK\treq-123' "${CAPTURE_STDOUT}" "api_request metadata" assert_eq $'200\t200 OK\treq-123' "${CAPTURE_STDOUT}" "api_request metadata"
assert_contains "${CAPTURE_STDERR}" "GET /api/v1/test -> 200 OK request_id=req-123" "api_request log line"
assert_eq '{"ok":true}' "$(cat "${body_file}")" "api_request writes body" assert_eq '{"ok":true}' "$(cat "${body_file}")" "api_request writes body"
assert_contains "$(cat "${headers_file}")" "x-gitea-request-id: req-123" "api_request writes headers" assert_contains "$(cat "${headers_file}")" "x-gitea-request-id: req-123" "api_request writes headers"
@@ -389,7 +388,6 @@ test_collect_package_candidates() {
capture_call collect_package_candidates acme 2 "${candidate_file}" token pkg-a pkg-b pkg-missing capture_call collect_package_candidates acme 2 "${candidate_file}" token pkg-a pkg-b pkg-missing
assert_eq $'2\t4\t3\t1' "${CAPTURE_STDOUT}" "collect_package_candidates summary" assert_eq $'2\t4\t3\t1' "${CAPTURE_STDOUT}" "collect_package_candidates summary"
assert_eq $'pkg-a\t1.0.0\t2024-01-01T00:00:00Z' "$(cat "${candidate_file}")" "collect_package_candidates chooses oldest version" assert_eq $'pkg-a\t1.0.0\t2024-01-01T00:00:00Z' "$(cat "${candidate_file}")" "collect_package_candidates chooses oldest version"
assert_contains "${CAPTURE_STDERR}" "No versions found for package pkg-missing" "collect_package_candidates logs missing packages"
} }
test_process_candidates_empty() { test_process_candidates_empty() {
@@ -398,7 +396,6 @@ test_process_candidates_empty() {
candidate_file="$(mktemp)" candidate_file="$(mktemp)"
capture_call process_candidates acme "${candidate_file}" 0 0 0 0 token capture_call process_candidates acme "${candidate_file}" 0 0 0 0 token
assert_contains "${CAPTURE_STDERR}" "No delete candidates found" "process_candidates empty file"
assert_contains "${CAPTURE_STDERR}" "Summary: packages=0 versions=0 kept=0 candidates=0 deleted=0 errors=0" "process_candidates empty summary" assert_contains "${CAPTURE_STDERR}" "Summary: packages=0 versions=0 kept=0 candidates=0 deleted=0 errors=0" "process_candidates empty summary"
} }
@@ -413,7 +410,6 @@ test_process_candidates() {
add_route DELETE "https://gitea.example/api/v1/packages/acme/nuget/pkg-b/2.0.0" 500 "Internal Server Error" del-2 '{"error":"boom"}' add_route DELETE "https://gitea.example/api/v1/packages/acme/nuget/pkg-b/2.0.0" 500 "Internal Server Error" del-2 '{"error":"boom"}'
capture_call process_candidates acme "${candidate_file}" 2 4 3 2 token capture_call process_candidates acme "${candidate_file}" 2 4 3 2 token
assert_contains "${CAPTURE_STDERR}" "Deleted package pkg-a version 1.0.0 -> 204 No Content" "process_candidates success path"
assert_contains "${CAPTURE_STDERR}" "ERROR: DELETE package pkg-b version 2.0.0 -> 500 Internal Server Error request_id=del-2" "process_candidates failure path" assert_contains "${CAPTURE_STDERR}" "ERROR: DELETE package pkg-b version 2.0.0 -> 500 Internal Server Error request_id=del-2" "process_candidates failure path"
assert_contains "${CAPTURE_STDERR}" "Summary: packages=2 versions=4 kept=3 candidates=2 deleted=1 errors=1" "process_candidates summary" assert_contains "${CAPTURE_STDERR}" "Summary: packages=2 versions=4 kept=3 candidates=2 deleted=1 errors=1" "process_candidates summary"
} }
@@ -431,11 +427,7 @@ test_main_integration() {
add_route DELETE "https://gitea.example/api/v1/packages/acme/nuget/pkg-a/1.0.0" 204 "No Content" del-a '' add_route DELETE "https://gitea.example/api/v1/packages/acme/nuget/pkg-a/1.0.0" 204 "No Content" del-a ''
capture_call main capture_call main
assert_contains "${CAPTURE_STDERR}" "keep_count=2" "main logs keep count"
assert_contains "${CAPTURE_STDERR}" "package_names=pkg-a,pkg-b" "main logs package names"
assert_contains "${CAPTURE_STDERR}" "Deleted package pkg-a version 1.0.0 -> 204 No Content" "main deletes old version"
assert_contains "${CAPTURE_STDERR}" "Summary: packages=2 versions=4 kept=3 candidates=1 deleted=1 errors=0" "main summary" assert_contains "${CAPTURE_STDERR}" "Summary: packages=2 versions=4 kept=3 candidates=1 deleted=1 errors=0" "main summary"
assert_contains "${CAPTURE_STDERR}" "Stage 4 complete" "main final stage log"
} }
tests=( tests=(