Last active
November 2, 2020 13:39
-
-
Save cmoetzing/2e7cba61a195fda35528080da90ebc10 to your computer and use it in GitHub Desktop.
Script to delete all disabled branchs of all plans of a project.
This file contains hidden or 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 | |
# Based on a script by Sylvain Guillopé <[email protected]> | |
# https://gist.github.com/sguillope/001c1f5574be3be3a595#file-bamboo_delete_disabled_branches-sh | |
LC_COLLATE=C ; export LC_COLLATE | |
LANG=C ; export LANG | |
umask 022 | |
function die { | |
[ -z "$1" ] || echo 1>&2 "[!] $1" | |
exit 1 | |
} | |
[ -z "$1" ] && die "Usage: ./$(basename $0) {projectKey}" | |
readonly PROJECT_KEY="$1" | |
readonly MAX_BRANCH_RESULT="500" | |
readonly BAMBOO_BASE_URL="https://my-bamboo.net" | |
readonly BAMBOO_DELETE_PLAN_URL="$BAMBOO_BASE_URL/ajax/deleteChain.action" | |
# Ask for bamboo credentials | |
username= | |
password= | |
while [ -z "$username" ]; do | |
read -p "Bamboo username: " -s username | |
done | |
echo "" | |
while [ -z "$password" ]; do | |
read -p "Bamboo password: " -s password | |
done | |
echo "" | |
function fetch_plans { | |
local projectKey=$1 | |
local url="$BAMBOO_BASE_URL/rest/api/latest/project/$projectKey?expand=plans&os_authType=basic" | |
echo $(curl -qs -k --user "$username:$password" -H 'X-Atlassian-Token: no-check' -H 'Accept: application/json' -H 'User-Agent: curl' "$url") | |
} | |
function get_plan_keys { | |
local projectKey=$1 | |
local json=$(fetch_plans "$projectKey") | |
echo "$json" | jq --raw-output '.plans.plan[].key' | |
} | |
function fetch_branches { | |
local planKey=$1 | |
local url="$BAMBOO_BASE_URL/rest/api/latest/plan/$planKey?os_authType=basic&expand=branches&max-results=$MAX_BRANCH_RESULT" | |
echo $(curl -qs -k --user "$username:$password" -H 'X-Atlassian-Token: no-check' -H 'Accept: application/json' -H 'User-Agent: curl' "$url") | |
} | |
function get_keys_of_disabled_branches { | |
local planKey=$1 | |
local json=$(fetch_branches "$planKey") | |
echo "$json" | jq --raw-output '.branches.branch[] | select(.enabled == false) | .key' | |
} | |
function delete_branch { | |
local branch_key=$1 | |
echo -n "delete branch ${1}: " | |
curl -qs -k --user "$username:$password" -H 'X-Atlassian-Token: no-check' -H 'Accept: application/json' -H 'User-Agent: curl' -H 'Content-Type: application/x-www-form-urlencoded' --data "buildKey=$branch_key" "$BAMBOO_DELETE_PLAN_URL" | |
echo | |
} | |
function delete_branches { | |
local keys=$1 | |
for key in $keys; do | |
delete_branch "$key" | |
done | |
} | |
function delete_disabled_branches_of_plan { | |
local planKey=$1 | |
echo "delete disabled branches of plan $planKey" | |
local branchKeys=$(get_keys_of_disabled_branches "$planKey") | |
delete_branches "$branchKeys" | |
} | |
function main { | |
local projectKey=$1 | |
echo "cleanup branches of plans of project $projectKey" | |
local keys=$(get_plan_keys "$projectKey") | |
for key in $keys; do | |
delete_disabled_branches_of_plan "$key" | |
done | |
} | |
main "$PROJECT_KEY" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment