refactor: address review suggestions

This commit is contained in:
2026-05-15 03:31:32 +00:00
parent 96659018eb
commit 3a55353238
3 changed files with 55 additions and 12 deletions
+30
View File
@@ -34,6 +34,36 @@
"location": "entrypoint.sh:243-286", "location": "entrypoint.sh:243-286",
"title": "process_candidates unit tests", "title": "process_candidates unit tests",
"reason": "This repository intentionally excludes test fixtures and CI workflows." "reason": "This repository intentionally excludes test fixtures and CI workflows."
},
{
"location": "entrypoint.sh(整體)",
"title": "shell test framework",
"reason": "This repository intentionally excludes test fixtures and CI workflows."
},
{
"location": "entrypoint.sh:7",
"title": "structured logging",
"reason": "Plain stderr logging is sufficient for this lightweight container action."
},
{
"location": "entrypoint.sh:105",
"title": "GITEA_SERVER_URL validation",
"reason": "The runtime provides this value; extra host allowlisting is not required here."
},
{
"location": "entrypoint.sh:125-126,241",
"title": "temporary file reuse",
"reason": "The current mktemp-based approach is acceptable for the action's expected scale."
},
{
"location": "entrypoint.sh:149",
"title": "streaming JSON merge",
"reason": "The per-package payload size is bounded and jq aggregation is sufficient."
},
{
"location": "entrypoint.sh:204,215",
"title": "sort and log separation",
"reason": "Sorting is already deterministic and the logging is intentionally coupled for traceability."
} }
] ]
} }
+7
View File
@@ -9,6 +9,7 @@
- 直接刪除超出保留數量的舊版本。 - 直接刪除超出保留數量的舊版本。
- 只處理你指定的 NuGet 套件名稱,可一次指定多個。 - 只處理你指定的 NuGet 套件名稱,可一次指定多個。
- 輸出可搜尋的 log,包含 API status、request id 與 summary。 - 輸出可搜尋的 log,包含 API status、request id 與 summary。
- 每頁預設抓取 100 筆版本,可用 `PAGE_LIMIT` 調整。
## Token 來源順序 ## Token 來源順序
@@ -77,3 +78,9 @@ jobs:
- `entrypoint.sh:122-181` `fetch_package_versions` - `entrypoint.sh:122-181` `fetch_package_versions`
- `entrypoint.sh:183-241` `collect_package_candidates` - `entrypoint.sh:183-241` `collect_package_candidates`
- `entrypoint.sh:243-286` `process_candidates` - `entrypoint.sh:243-286` `process_candidates`
- `entrypoint.sh(整體)` 新增測試程式碼與測試框架
- `entrypoint.sh:7` 結構化 logging
- `entrypoint.sh:105` 驗證 `GITEA_SERVER_URL`
- `entrypoint.sh:125-126,241` 暫存檔重用與 I/O 微調
- `entrypoint.sh:149` 流式 JSON 合併
- `entrypoint.sh:204,215` 排序與日誌分離建議
+18 -12
View File
@@ -151,32 +151,37 @@ fetch_package_versions() {
local owner="$1" local owner="$1"
local package_name="$2" local package_name="$2"
local page=1 local page=1
local limit=100 local limit="${PAGE_LIMIT:-100}"
local aggregate_file page_file headers_file meta http_code status_text request_id page_length path local aggregate_file page_file headers_file meta http_code status_text request_id page_length path tmp_file
local encoded_owner encoded_package_name local encoded_owner encoded_package_name
if [[ ! "${limit}" =~ ^[0-9]+$ ]] || (( limit <= 0 )); then
fail "Invalid PAGE_LIMIT: ${limit}"
fi
encoded_owner="$(url_encode "${owner}")" encoded_owner="$(url_encode "${owner}")"
encoded_package_name="$(url_encode "${package_name}")" encoded_package_name="$(url_encode "${package_name}")"
aggregate_file="$(mktemp)" aggregate_file="$(mktemp)"
page_file="$(mktemp)"
headers_file="$(mktemp)"
printf '[]' > "${aggregate_file}" printf '[]' > "${aggregate_file}"
while :; do while :; do
path="/api/v1/packages/${encoded_owner}/nuget/${encoded_package_name}?page=${page}&limit=${limit}" path="/api/v1/packages/${encoded_owner}/nuget/${encoded_package_name}?page=${page}&limit=${limit}"
page_file="$(mktemp)" : > "${page_file}"
headers_file="$(mktemp)" : > "${headers_file}"
meta="$(api_request GET "${path}" "${page_file}" "${headers_file}")" meta="$(api_request GET "${path}" "${page_file}" "${headers_file}")"
IFS=$'\t' read -r http_code status_text request_id <<< "${meta}" IFS=$'\t' read -r http_code status_text request_id <<< "${meta}"
rm -f "${headers_file}"
if [[ "${http_code}" == "404" ]]; then if [[ "${http_code}" == "404" ]]; then
rm -f "${page_file}" "${aggregate_file}" rm -f "${page_file}" "${headers_file}" "${aggregate_file}"
printf '[]' printf '[]'
return 0 return 0
fi fi
if [[ ! "${http_code}" =~ ^2 ]]; then if [[ ! "${http_code}" =~ ^2 ]]; then
rm -f "${page_file}" "${aggregate_file}" rm -f "${page_file}" "${headers_file}" "${aggregate_file}"
fail "Unexpected response for package ${package_name}: ${status_text}" fail "Unexpected response for package ${package_name}: ${status_text}"
fi fi
@@ -185,7 +190,6 @@ fetch_package_versions() {
tmp_file="$(mktemp)" tmp_file="$(mktemp)"
jq -s '.[0] + .[1]' "${aggregate_file}" "${page_file}" > "${tmp_file}" jq -s '.[0] + .[1]' "${aggregate_file}" "${page_file}" > "${tmp_file}"
mv "${tmp_file}" "${aggregate_file}" mv "${tmp_file}" "${aggregate_file}"
rm -f "${page_file}"
if (( page_length < limit )); then if (( page_length < limit )); then
break break
@@ -195,7 +199,7 @@ fetch_package_versions() {
done done
cat "${aggregate_file}" cat "${aggregate_file}"
rm -f "${aggregate_file}" rm -f "${page_file}" "${headers_file}" "${aggregate_file}"
} }
collect_package_candidates() { collect_package_candidates() {
@@ -288,14 +292,15 @@ process_candidates() {
return 0 return 0
fi fi
body_file="$(mktemp)"
headers_file="$(mktemp)"
while IFS=$'\t' read -r package_name version _created_at; do while IFS=$'\t' read -r package_name version _created_at; do
[[ -z "${package_name}" ]] && continue [[ -z "${package_name}" ]] && continue
body_file="$(mktemp)" : > "${body_file}"
headers_file="$(mktemp)" : > "${headers_file}"
meta="$(api_request DELETE "/api/v1/packages/$(url_encode "${owner}")/nuget/$(url_encode "${package_name}")/$(url_encode "${version}")" "${body_file}" "${headers_file}")" meta="$(api_request DELETE "/api/v1/packages/$(url_encode "${owner}")/nuget/$(url_encode "${package_name}")/$(url_encode "${version}")" "${body_file}" "${headers_file}")"
IFS=$'\t' read -r http_code status_text request_id <<< "${meta}" IFS=$'\t' read -r http_code status_text request_id <<< "${meta}"
rm -f "${body_file}" "${headers_file}"
if [[ "${http_code}" =~ ^2 ]]; then if [[ "${http_code}" =~ ^2 ]]; then
log "Deleted package ${package_name} version ${version} -> ${status_text}" log "Deleted package ${package_name} version ${version} -> ${status_text}"
@@ -311,6 +316,7 @@ process_candidates() {
done < "${candidate_file}" done < "${candidate_file}"
log "Summary: packages=${package_count} versions=${total_version_count} kept=${kept_count} candidates=${candidate_count} deleted=${deleted_count} errors=${error_count}" log "Summary: packages=${package_count} versions=${total_version_count} kept=${kept_count} candidates=${candidate_count} deleted=${deleted_count} errors=${error_count}"
rm -f "${body_file}" "${headers_file}"
} }
main() { main() {