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