Created
February 13, 2022 14:02
-
-
Save Krisztiaan/f50f8d8f1507e01566b95e750e631a37 to your computer and use it in GitHub Desktop.
Delete all workflow runs from disabled actions, so GitHub will not show them in actions anymore.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
if ! command -v gh &>/dev/null; then | |
echo "The GitHub CLI <gh> could not be found." | |
read -p "Do you want to install it? " -n 1 -r | |
echo | |
if [[ $REPLY =~ ^[Yy]$ ]]; then | |
echo "Installing GitHub CLI..." | |
if ! command -v brew &>/dev/null; then | |
echo "The <brew> command could not be found." | |
echo "Please install it first." | |
exit 1 | |
fi | |
brew install gh | |
else | |
echo "Please install the GitHub CLI <gh> manually." | |
exit 1 | |
fi | |
fi | |
if ! command -v jq &>/dev/null; then | |
echo "The JSON parser <jq> could not be found." | |
read -p "Do you want to install it? " -n 1 -r | |
echo | |
if [[ $REPLY =~ ^[Yy]$ ]]; then | |
echo "Installing JSON parser..." | |
if ! command -v brew &>/dev/null; then | |
echo "The <brew> command could not be found." | |
echo "Please install it first." | |
exit 1 | |
fi | |
brew install jq | |
else | |
echo "Please install the JSON parser <jq> manually." | |
exit 1 | |
fi | |
fi | |
while [[ "$#" -gt 0 ]]; do | |
case $1 in | |
-o | --org) | |
org="$2" | |
shift | |
;; | |
-r | --repo) | |
repo="$2" | |
shift | |
;; | |
*) | |
echo "Unknown parameter passed: $1" | |
exit 1 | |
;; | |
esac | |
shift | |
done | |
# Check if org is set | |
if [[ -z "$org" ]]; then | |
echo "Please specify an organization with --org/-o <organization>." | |
exit 1 | |
fi | |
# Check if repo is set | |
if [[ -z "$repo" ]]; then | |
echo "Please specify a repository with --repo/-r <repository>." | |
exit 1 | |
fi | |
echo "Org : $org" | |
echo "Repo: $org/$repo" | |
# Get workflow IDs with status "disabled_manually" | |
workflow_ids=($(gh api repos/$org/$repo/actions/workflows | jq '.workflows[] | select(.["state"] | contains("disabled_manually")) | .id')) | |
if [[ ${#workflow_ids[@]} -gt 0 ]]; then | |
echo "The following workflow(s) will be deleted:" | |
for id in "${workflow_ids[@]}"; do | |
gh api repos/$org/$repo/actions/workflows/$id | jq -r '.name' | |
echo "^ workflow ID: $id" | |
done | |
read -p "Do you want to proceed? " -n 1 -r | |
echo | |
if [[ $REPLY =~ ^[Nn]$ ]]; then | |
echo "Aborting..." | |
exit 1 | |
fi | |
else | |
echo "No workflows found with status \"disabled_manually\"." | |
exit 0 | |
fi | |
for workflow_id in "${workflow_ids[@]}"; do | |
echo "Listing runs for the workflow ID $workflow_id" | |
run_ids=($(gh api repos/$org/$repo/actions/workflows/$workflow_id/runs --paginate | jq '.workflow_runs[].id')) | |
# Echo count of runs | |
echo "Found ${#run_ids[@]} runs." | |
# Delete runs and show progress as percentage | |
for i in "${!run_ids[@]}"; do | |
# calculate percentage | |
percent=$((100 * $i / ${#run_ids[@]})) | |
printf "Deleting run ${run_ids[$i]} ($percent%%)\r" | |
gh api repos/$org/$repo/actions/runs/${run_ids[$i]} -X DELETE >/dev/null | |
done | |
done | |
echo "Done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment