diff --git a/scripts/remove-failed-jobs.sh b/scripts/remove-failed-jobs.sh index a90f390e..1a872857 100644 --- a/scripts/remove-failed-jobs.sh +++ b/scripts/remove-failed-jobs.sh @@ -1,5 +1,7 @@ +#!/bin/bash + gh_bulk_delete_workflow_runs() { - repo=$1 + local repo=$1 # Ensure the repo argument is provided if [[ -z "$repo" ]]; then @@ -7,19 +9,37 @@ gh_bulk_delete_workflow_runs() { return 1 fi + # Fetch workflow runs that are cancelled, failed, or timed out + local runs runs=$(gh api repos/$repo/actions/runs --paginate | - jq -r '.workflow_runs[] | - select(.conclusion == "cancelled" or - .conclusion == "failure" or - .conclusion == "timed_out") | - .id') + jq -r '.workflow_runs[] | + select(.conclusion == "cancelled" or + .conclusion == "failure" or + .conclusion == "timed_out") | + .id') + if [[ -z "$runs" ]]; then + echo "No workflow runs found for $repo with the specified conclusions." + return 0 + fi + + # Loop through each run and delete it while IFS= read -r run; do - echo "Deleting run https://github.com/$repo/actions/runs/$run" - gh api -X DELETE repos/$repo/actions/runs/$run --silent + echo "Attempting to delete run: https://github.com/$repo/actions/runs/$run" + + # Perform the deletion + if gh api -X DELETE repos/$repo/actions/runs/$run --silent; then + echo "Successfully deleted run: $run" + else + echo "Error deleting run: $run" >&2 + fi + + # Optional delay to avoid hitting API rate limits + sleep 1 done <<< "$runs" - echo "All workflow runs for $repo have been deleted." + echo "Completed deletion process for workflow runs in $repo." } +# Execute the function with the provided argument gh_bulk_delete_workflow_runs "$1"