Debian Codegraph @

util/dwr.sh

raw
1 #!/usr/bin/env bash
2
3 # `dwr` - delete workflow runs (by DJ Adams)
4 # ref: <https://github.com/qmacro/dotfiles/blob/230c6df494f239e9d1762794943847816e1b7c32/scripts/dwr>
5 # ref: [Mass deletion of GitHub Actions workflow runs](https://qmacro.org/autodidactics/2021/03/26/mass-deletion-of-github-actions-workflow-runs) @@ <https://archive.is/rxdCY>
6
7 # LICENSE: "Feel free to steal, modify, or make fun of" (from <https://github.com/qmacro/dotfiles/blob/230c6df494f239e9d1762794943847816e1b7c32/README.md>)
8
9 # spell-checker:ignore (options) multi ; (people) DJ Adams * qmacro ; (words) gsub
10
11 # Given an "owner/repo" name, such as "qmacro/thinking-aloud",
12 # retrieve the workflow runs for that repo and present them in a
13 # list. Selected runs will be deleted. Uses the GitHub API.
14
15 # Requires gh (GitHub CLI) and jq (JSON processor)
16
17 # First version
18
19 set -o errexit
20 set -o pipefail
21
22 declare repo=${1:?No owner/repo specified}
23
24 jq_script() {
25
26 cat <<EOF
27 def symbol:
28 sub("skipped"; "SKIP") |
29 sub("success"; "GOOD") |
30 sub("failure"; "FAIL");
31
32 def tz:
33 gsub("[TZ]"; " ");
34
35
36 .workflow_runs[]
37 | [
38 (.conclusion | symbol),
39 (.created_at | tz),
40 .id,
41 .event,
42 .name
43 ]
44 | @tsv
45 EOF
46
47 }
48
49 select_runs() {
50
51 gh api --paginate "/repos/$repo/actions/runs" |
52 jq -r -f <(jq_script) |
53 fzf --multi
54
55 }
56
57 delete_run() {
58
59 local run id result
60 run=$1
61 id="$(cut -f 3 <<<"$run")"
62 gh api -X DELETE "/repos/$repo/actions/runs/$id"
63 # shellcheck disable=SC2181
64 [[ $? = 0 ]] && result="OK!" || result="BAD"
65 printf "%s\t%s\n" "$result" "$run"
66
67 }
68
69 delete_runs() {
70
71 local id
72 while read -r run; do
73 delete_run "$run"
74 sleep 0.25
75 done
76
77 }
78
79 main() {
80
81 select_runs | delete_runs
82
83 }
84
85 main