-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patham-gitpruneruns
executable file
·133 lines (125 loc) · 4.39 KB
/
am-gitpruneruns
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
#!/bin/sh
set -eu
set -x
# Bash only - set -o pipefail
#
# Script to prune old workflow runs for given repository
#
# Note - GitHub Search API provides up to 1,000 results for each search run
#
_OWNER="forwardcomputers/"
_DAYS=30
_TOKEN="$LP_GITHUB_API_TOKEN"
#
_VERSION=0.1.1
_SCRIPT_URL="https://github.com/forwardcomputers/bin/raw/main/am-gitpruneruns"
_SCRIPT_NAME=$( basename "${0}")
_HEAD="Accept: application/vnd.github+json"
_AUTH="Authorization: token $_TOKEN"
if [ $( uname -s ) = "Darwin" ]; then
[ -n "${_DAYS+x}" ] && _SINCE=$( date -j -v -${_DAYS:-30}d +%Y-%m-%d ) || _SINCE=$( date -j -v -30d +%Y-%m-%d )
else
[ -n "${_DAYS+x}" ] && _SINCE=$( date --date="${_DAYS:-30} days ago" +%Y-%m-%d ) || _SINCE=$( date --date="30 days ago" +%Y-%m-%d )
fi
#
do_git_action() {
_ACTION="${1}"
_EXTRA="${_EXTRA-};${2}"
_PAGES=$( curl -sI -H "${_HEAD}" -H "${_AUTH}" "${_BASE_URL}/${_ACTION}?per_page=100${_EXTRA}" | awk '/^link/ { gsub(/^link:.*page=/,""); gsub(/>.*$/,""); print }' )
_PAGES=${_PAGES:-1}
_PAGE=1; while [ $_PAGE -le "${_PAGES}" ]; do
_URL="${_BASE_URL}/${_ACTION}?page=${_PAGE};per_page=100${_EXTRA}"
for _ID in $( curl -sL -H "${_HEAD}" -H "${_AUTH}" "${_URL}" | jq -r .workflow_runs[].id ); do
printf '%b\n' "${_DRY_RUN:-(Dry run) }Pruning run id: $_ID"
${_FORCE-false} && curl -sL -H "${_HEAD}" -H "${_AUTH}" -X DELETE "${_BASE_URL}/${_ACTION}/$_ID"
done
_PAGE=$(( _PAGE + 1 ))
done
}
#
usage() {
printf "GitHub prune workflow runs v%s (%s)\nReleased under CC0, with no warranty and no liability. Use on your own risk.\n\n" "${_VERSION}" "${_SCRIPT_URL}"
printf "Usage: %s [Options] Repository\n\n" "${_SCRIPT_NAME}"
printf "Script to prune old workflow runs for given repository\n\n"
printf "Options:\n\n"
printf " --cancelled Cancelled runs only\n"
printf " --completed Completed runs only\n"
printf "-d, --days <number> Days to keep (default 30 days)\n"
printf " --failure Failed runs only\n"
printf "-f, --force Force pruning (Dry run normally)\n"
printf "-h, --help Print this help and exit\n"
printf "-s, --since <string> Keep since yyyy-mm-dd\n"
printf " --skipped Skipped runs only\n"
printf " --success Successful runs only\n"
printf "-v, --verbose Make the operation more talkative\n\n"
exit
}
# usage() {
# cat <<_EOF
# GitHub prune workflow runs v${_VERSION} (${_SCRIPT_URL})
# Released under CC0, with no warranty and no liability. Use on your own risk.
# Usage: ${_SCRIPT_NAME} [Options] Repository
# Script to prune old workflow runs for given repository
# Options:
# --cancelled Cancelled runs only
# --completed Completed runs only
# -d, --days <number> Days to keep (default 30 days)
# --failure Failed runs only
# -f, --force Force pruning (Dry run normally)
# -h, --help Print this help and exit
# -s, --since <string> Keep since yyyy-mm-dd
# --skipped Skipped runs only
# --success Successful runs only
# -v, --verbose Make the operation more talkative
# _EOF
# exit
# }
#
cleanup() {
[ $? -eq 0 ] && exit
die "Missing parameter"
}
#
msg() {
printf '\n%b\n' >&2 "${1-}"
}
#
die() {
msg="${1}"
code="${2-1}" # default exit status 1
msg "$msg"
usage
exit "$code"
}
#
parse_params() {
while [ "$#" -ne 0 ]; do
case "${1-}" in
--cancelled ) _EXTRA=";status=${1#--}" ;;
--completed ) _EXTRA=";status=${1#--}" ;;
-d | --days ) _SINCE=$( date --date="${2-} days ago" +%Y-%m-%d ); shift ;;
--failure ) _EXTRA=";status=${1#--}" ;;
-f | --force ) _FORCE=true; _DRY_RUN="" ;;
-h | --help ) usage ;;
-s | --since ) _SINCE=$( date --date="${2-}" +%Y-%m-%d ); shift ;;
--skipped ) _EXTRA=";status=${1#--}" ;;
--success ) _EXTRA=";status=${1#--}" ;;
-v | --verbose ) set -x ;;
-?* ) die "Unknown option: $1" ;;
* ) _REPO=$1 ;;
esac
shift > /dev/null 2>&1
done
[ -z "${_REPO-}" ] && die "Missing Repository"
[ -z "${_SINCE-}" ] && die "Missing Date parameter"
return 0
}
#
trap cleanup EXIT
parse_params "$@"
#
echo "${_REPO}" | grep -q "/" || _REPO="${_OWNER-}${_REPO}"
echo "${_REPO}" | grep -q "/" || die "Missing Repository owner"
_BASE_URL="https://api.github.com/repos/${_REPO}"
do_git_action "actions/runs" "created=<$_SINCE"
#