|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +set -e |
| 4 | + |
| 5 | +org=bodsch |
| 6 | +repo=$(basename $(git rev-parse --show-toplevel)) |
| 7 | + |
| 8 | +keep_first_two() { |
| 9 | + local arr=("$@") # Das Array wird als Argument übergeben |
| 10 | + local new_arr=("${arr[@]:0:2}") # Die ersten beiden Elemente werden beibehalten |
| 11 | + echo "${new_arr[@]}" # Das neue Array wird ausgegeben |
| 12 | +} |
| 13 | + |
| 14 | +remove_elements() { |
| 15 | + local arr=("$@") |
| 16 | + local result=() |
| 17 | + local length=${#arr[@]} |
| 18 | + |
| 19 | + # Check whether the array has more than two elements |
| 20 | + if [ $length -le 2 ] |
| 21 | + then |
| 22 | + return |
| 23 | + fi |
| 24 | + |
| 25 | + # Keep the first two elements |
| 26 | + for ((i=2; i<length; i++)) |
| 27 | + do |
| 28 | + result+=("${arr[i]}") # Collect removed elements |
| 29 | + done |
| 30 | + |
| 31 | + echo "${result[@]}" |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +if [ -n "${org}" ] && [ -n "${repo}" ] |
| 36 | +then |
| 37 | + |
| 38 | + # Get workflow IDs with status "active" |
| 39 | + workflow_ids=($(gh api repos/$org/$repo/actions/workflows --paginate | jq '.workflows[] | select(.["state"] | contains("active")) | .id')) |
| 40 | + |
| 41 | + for workflow_id in "${workflow_ids[@]}" |
| 42 | + do |
| 43 | + echo "Listing runs for the workflow ID $workflow_id" |
| 44 | + run_ids=( $(gh api repos/$org/$repo/actions/workflows/$workflow_id/runs --paginate | jq '.workflow_runs[].id') ) |
| 45 | + |
| 46 | + # echo "${run_ids[@]}" |
| 47 | + |
| 48 | + # count array |
| 49 | + count_ids=${#run_ids[@]} |
| 50 | + # echo " - ${count_ids} entries" |
| 51 | + |
| 52 | + if [ ${count_ids} -gt 2 ] |
| 53 | + then |
| 54 | + # remove the first two elements of the list |
| 55 | + array=$(remove_elements "${run_ids[@]}") |
| 56 | + |
| 57 | + for run_id in ${array} |
| 58 | + do |
| 59 | + echo "Deleting Run ID $run_id" |
| 60 | + gh api repos/$org/$repo/actions/runs/$run_id -X DELETE >/dev/null |
| 61 | + done |
| 62 | + |
| 63 | + echo "-------------------------------------------------" |
| 64 | + fi |
| 65 | + done |
| 66 | +else |
| 67 | + echo "missing values:" |
| 68 | + echo " - org : '${org}'" |
| 69 | + echo " - repo: '${repo}'" |
| 70 | + |
| 71 | + exit 1 |
| 72 | +fi |
0 commit comments