From 3a553532384b6a4e3480d4e8ebd10241d93f73a5 Mon Sep 17 00:00:00 2001 From: Jeffery Date: Fri, 15 May 2026 03:31:32 +0000 Subject: [PATCH] refactor: address review suggestions --- .gitea/ai-review/exclusions.json | 30 ++++++++++++++++++++++++++++++ README.md | 7 +++++++ entrypoint.sh | 30 ++++++++++++++++++------------ 3 files changed, 55 insertions(+), 12 deletions(-) diff --git a/.gitea/ai-review/exclusions.json b/.gitea/ai-review/exclusions.json index e8744f3..95b573e 100644 --- a/.gitea/ai-review/exclusions.json +++ b/.gitea/ai-review/exclusions.json @@ -34,6 +34,36 @@ "location": "entrypoint.sh:243-286", "title": "process_candidates unit tests", "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." } ] } diff --git a/README.md b/README.md index 52f2891..f8f59c0 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,7 @@ - 直接刪除超出保留數量的舊版本。 - 只處理你指定的 NuGet 套件名稱,可一次指定多個。 - 輸出可搜尋的 log,包含 API status、request id 與 summary。 +- 每頁預設抓取 100 筆版本,可用 `PAGE_LIMIT` 調整。 ## Token 來源順序 @@ -77,3 +78,9 @@ jobs: - `entrypoint.sh:122-181` `fetch_package_versions` - `entrypoint.sh:183-241` `collect_package_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` 排序與日誌分離建議 diff --git a/entrypoint.sh b/entrypoint.sh index b0cb78b..c60f49b 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -151,32 +151,37 @@ fetch_package_versions() { local owner="$1" local package_name="$2" local page=1 - local limit=100 - local aggregate_file page_file headers_file meta http_code status_text request_id page_length path + local limit="${PAGE_LIMIT:-100}" + 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 + if [[ ! "${limit}" =~ ^[0-9]+$ ]] || (( limit <= 0 )); then + fail "Invalid PAGE_LIMIT: ${limit}" + fi + encoded_owner="$(url_encode "${owner}")" encoded_package_name="$(url_encode "${package_name}")" aggregate_file="$(mktemp)" + page_file="$(mktemp)" + headers_file="$(mktemp)" printf '[]' > "${aggregate_file}" while :; do path="/api/v1/packages/${encoded_owner}/nuget/${encoded_package_name}?page=${page}&limit=${limit}" - page_file="$(mktemp)" - headers_file="$(mktemp)" + : > "${page_file}" + : > "${headers_file}" meta="$(api_request GET "${path}" "${page_file}" "${headers_file}")" IFS=$'\t' read -r http_code status_text request_id <<< "${meta}" - rm -f "${headers_file}" if [[ "${http_code}" == "404" ]]; then - rm -f "${page_file}" "${aggregate_file}" + rm -f "${page_file}" "${headers_file}" "${aggregate_file}" printf '[]' return 0 fi 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}" fi @@ -185,7 +190,6 @@ fetch_package_versions() { tmp_file="$(mktemp)" jq -s '.[0] + .[1]' "${aggregate_file}" "${page_file}" > "${tmp_file}" mv "${tmp_file}" "${aggregate_file}" - rm -f "${page_file}" if (( page_length < limit )); then break @@ -195,7 +199,7 @@ fetch_package_versions() { done cat "${aggregate_file}" - rm -f "${aggregate_file}" + rm -f "${page_file}" "${headers_file}" "${aggregate_file}" } collect_package_candidates() { @@ -288,14 +292,15 @@ process_candidates() { return 0 fi + body_file="$(mktemp)" + headers_file="$(mktemp)" while IFS=$'\t' read -r package_name version _created_at; do [[ -z "${package_name}" ]] && continue - body_file="$(mktemp)" - headers_file="$(mktemp)" + : > "${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}" - rm -f "${body_file}" "${headers_file}" if [[ "${http_code}" =~ ^2 ]]; then log "Deleted package ${package_name} version ${version} -> ${status_text}" @@ -311,6 +316,7 @@ process_candidates() { done < "${candidate_file}" 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() {