refactor: remove dry run support

This commit is contained in:
2026-05-15 02:18:40 +00:00
parent 68e7842586
commit c923a6ce55
4 changed files with 12 additions and 64 deletions
+2 -22
View File
@@ -6,7 +6,7 @@
- 依序嘗試取得 Gitea API token。
- 預設每個套件保留最新 `2` 個版本。
- 預設為 `dry_run=true`,只列出候選,不會真的刪除
- 直接刪除超出保留數量的舊版本
- 輸出可搜尋的 log,包含 API status、request id 與 summary。
## Token 來源順序
@@ -25,7 +25,6 @@ Action 會依序嘗試以下來源:
| --- | --- | --- | --- |
| `RUNNER_TOKEN` | string | - | Gitea API token,優先順序最高 |
| `KEEP_VERSIONS` | integer string | `2` | 每個套件要保留的最新版本數,必須是整數且 `>= 0` |
| `DRY_RUN` | boolean string | `true` | `true` 只列出候選,`false` 才會呼叫 DELETE API |
## Log 行為
@@ -34,17 +33,13 @@ Action 會依序嘗試以下來源:
- `Trying token from ...`
- `Using token from ...`
- `keep_versions=...`
- `dry_run=...`
- `GET /api/v1/... -> 200 OK`
- `Candidate to delete: ...`
- `[DRY-RUN] Would delete ...`
- `Deleted package ... -> 204 No Content`
- `Summary: packages=... versions=... kept=... candidates=... deleted=... skipped=... errors=...`
- `Summary: packages=... versions=... kept=... candidates=... deleted=... errors=...`
## Workflow 範例
### Dry-run
```yaml
jobs:
cleanup:
@@ -54,21 +49,6 @@ jobs:
with:
RUNNER_TOKEN: ${{ secrets.GITEA_TOKEN }}
KEEP_VERSIONS: 2
DRY_RUN: true
```
### 實際刪除
```yaml
jobs:
cleanup:
runs-on: ubuntu
steps:
- uses: ./
with:
RUNNER_TOKEN: ${{ secrets.GITEA_TOKEN }}
KEEP_VERSIONS: 2
DRY_RUN: false
```
## 測試
+1 -5
View File
@@ -6,13 +6,9 @@ inputs:
description: 'Gitea API token, highest priority source'
required: false
KEEP_VERSIONS:
description: 'Number of recent package versions to keep'
description: '保留的版本數量'
required: false
default: '2'
DRY_RUN:
description: 'When true, only print delete candidates'
required: false
default: 'true'
runs:
using: 'docker'
image: 'Dockerfile'
+4 -30
View File
@@ -36,22 +36,6 @@ resolve_keep_versions() {
printf '%s' "${raw_value}"
}
resolve_dry_run() {
local raw_value="${INPUT_DRY_RUN:-true}"
case "${raw_value,,}" in
true|1|yes|on)
printf 'true'
;;
false|0|no|off)
printf 'false'
;;
*)
fail "Invalid dry_run: ${raw_value}"
;;
esac
}
init_repo_context() {
local repository="${GITEA_REPOSITORY:-}"
@@ -192,27 +176,19 @@ collect_package_candidates() {
}
process_candidates() {
local dry_run="$1"
local name version created_at
local deleted_count=0
local skipped_count=0
local error_count=0
if [[ ! -s "${CANDIDATES_FILE}" ]]; then
log "No delete candidates found"
log "Summary: packages=${PACKAGE_COUNT} versions=${TOTAL_VERSION_COUNT} kept=${KEPT_COUNT} candidates=0 deleted=0 skipped=0 errors=0 dry_run=${dry_run}"
log "Summary: packages=${PACKAGE_COUNT} versions=${TOTAL_VERSION_COUNT} kept=${KEPT_COUNT} candidates=0 deleted=0 errors=0"
return 0
fi
while IFS=$'\t' read -r name version created_at; do
[[ -z "${name}" ]] && continue
if [[ "${dry_run}" == "true" ]]; then
log "[DRY-RUN] Would delete package ${name} version ${version} (created: ${created_at})"
skipped_count=$((skipped_count + 1))
continue
fi
if api_request DELETE "/api/v1/packages/${REPO_OWNER}/nuget/${name}/${version}"; then
log "Deleted package ${name} version ${version} -> ${API_STATUS_TEXT}"
deleted_count=$((deleted_count + 1))
@@ -226,11 +202,11 @@ process_candidates() {
fi
done < "${CANDIDATES_FILE}"
log "Summary: packages=${PACKAGE_COUNT} versions=${TOTAL_VERSION_COUNT} kept=${KEPT_COUNT} candidates=${CANDIDATE_COUNT} deleted=${deleted_count} skipped=${skipped_count} errors=${error_count} dry_run=${dry_run}"
log "Summary: packages=${PACKAGE_COUNT} versions=${TOTAL_VERSION_COUNT} kept=${KEPT_COUNT} candidates=${CANDIDATE_COUNT} deleted=${deleted_count} errors=${error_count}"
}
main() {
local token keep_versions dry_run
local token keep_versions
log "Gitea Server Url: ${GITEA_SERVER_URL:-}"
log "Gitea Repository: ${GITEA_REPOSITORY:-}"
@@ -242,9 +218,7 @@ main() {
export RESOLVED_GITEA_TOKEN="$token"
init_repo_context
keep_versions="$(resolve_keep_versions)"
dry_run="$(resolve_dry_run)"
log "keep_versions=${keep_versions}"
log "dry_run=${dry_run}"
log "Token source resolved successfully"
CANDIDATES_FILE="$(mktemp)"
@@ -256,7 +230,7 @@ main() {
trap 'rm -f "${CANDIDATES_FILE}"' EXIT
collect_package_candidates
process_candidates "${dry_run}"
process_candidates
log "Stage 4 complete"
}
+5 -7
View File
@@ -90,14 +90,13 @@ run_entrypoint() {
write_mock_curl
output="$(run_entrypoint INPUT_RUNNER_TOKEN=abc INPUT_KEEP_VERSIONS=1 INPUT_DRY_RUN=true)"
output="$(run_entrypoint INPUT_RUNNER_TOKEN=abc INPUT_KEEP_VERSIONS=1)"
assert_contains "Using token from inputs.RUNNER_TOKEN" "$output"
assert_contains "keep_versions=1" "$output"
assert_contains "dry_run=true" "$output"
assert_contains "request_id=req-123" "$output"
assert_contains "[DRY-RUN] Would delete package pkg-a version 1.0.0" "$output"
assert_contains "Deleted package pkg-a version 1.0.0 -> 204 No Content" "$output"
output="$(run_entrypoint GITEA_TOKEN=secret INPUT_KEEP_VERSIONS=1 INPUT_DRY_RUN=true)"
output="$(run_entrypoint GITEA_TOKEN=secret INPUT_KEEP_VERSIONS=1)"
assert_contains "Using token from secrets.GITEA_TOKEN" "$output"
if run_entrypoint INPUT_KEEP_VERSIONS=-1 >/dev/null 2>&1; then
@@ -111,7 +110,6 @@ if run_entrypoint >/dev/null 2>&1; then
fi
delete_log="${TMP_DIR}/delete.log"
output="$(MOCK_DELETE_FILE="$delete_log" run_entrypoint INPUT_RUNNER_TOKEN=abc INPUT_KEEP_VERSIONS=1 INPUT_DRY_RUN=false)"
assert_contains "Deleted package pkg-a version 1.0.0 -> 204 No Content" "$output"
assert_contains "Summary: packages=2 versions=3 kept=2 candidates=1 deleted=1 skipped=0 errors=0 dry_run=false" "$output"
output="$(MOCK_DELETE_FILE="$delete_log" run_entrypoint INPUT_RUNNER_TOKEN=abc INPUT_KEEP_VERSIONS=1)"
assert_contains "Summary: packages=2 versions=3 kept=2 candidates=1 deleted=1 errors=0" "$output"
assert_contains "DELETE https://gitea.example.com/api/v1/packages/test-owner/nuget/pkg-a/1.0.0" "$(cat "$delete_log")"