Forked from sguillope/bamboo_delete_disabled_branches.sh
Last active
July 12, 2016 20:52
-
-
Save jkstrauss/3169853c435efa85924f536ac58cd7d5 to your computer and use it in GitHub Desktop.
Script to delete all disabled branches of a Bamboo build plan.
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 -u | |
# Deletes all disabled branches of a Bamboo build plan | |
# ----------------------------------------------------------------------------- | |
# Syntax: | |
# $0 {planKey} | |
# ----------------------------------------------------------------------------- | |
# Purpose: Bamboo does not automatically delete plan branches when the | |
# corresponding branch in the repository gets deleted. Because Bamboo fails | |
# to pull from it, it disables the branch but keep it around forever. | |
# This script goes through all branches of a build plan and delete the ones | |
# that are disabled. | |
# | |
# Notes: | |
# - Script depends on jq library: https://github.com/stedolan/jq | |
# - The script will prompt for Bamboo credentials. The corresponding | |
# account must have admin access to the given plan. | |
# - Before running the script, change the value of `BAMBOO_BASE_URL` to | |
# the correct url of the bamboo instance. | |
# ----------------------------------------------------------------------------- | |
# Copyright 2014 Lixar I.T. Inc. | |
# Author: Sylvain Guillopé <[email protected]> | |
# ----------------------------------------------------------------------------- | |
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) {planKey}" | |
readonly PLAN_KEY="$1" | |
readonly MAX_BRANCH_RESULT="500" | |
readonly BAMBOO_BASE_URL="https://mybamboo.net" | |
readonly BAMBOO_GET_PLAN_URL="$BAMBOO_BASE_URL/rest/api/latest/plan/$PLAN_KEY?os_authType=basic&expand=branches&max-results=$MAX_BRANCH_RESULT" | |
readonly BAMBOO_DELETE_PLAN_URL="$BAMBOO_BASE_URL/chain/admin/deleteChain!doDelete.action?os_authType=basic" | |
# Ask for bamboo credentials | |
username= | |
password= | |
while [ -z "$username" ]; do | |
read -p "Bamboo username: " -s username | |
done | |
while [ -z "$password" ]; do | |
read -p "Bamboo password: " -s password | |
done | |
function fetch_plan { | |
echo $(curl --silent --user "$username:$password" --header "Accept: application/json" --header "X-Atlassian-Token: no-check" "$BAMBOO_GET_PLAN_URL") | |
} | |
function get_branch_lines { | |
echo "$1" | jq --compact-output '.branches.branch[] | select(.enabled | not)' | |
} | |
function delete_disabled_branches { | |
while read -r branch_line; do | |
local branch_key=$(get_branch_key "$branch_line") | |
delete_branch "$branch_key" | |
done <<< "$branch_lines" | |
} | |
function delete_branch { | |
local branch_key=$1 | |
echo $branch_key | |
curl -qs --user "$username:$password" --header "X-Atlassian-Token: no-check" --data "buildKey=$branch_key" "$BAMBOO_DELETE_PLAN_URL" | |
} | |
function get_branch_key { | |
echo $1 | jq --raw-output '.key' | |
} | |
function main { | |
local plan_details=$(fetch_plan "$1") | |
local branch_lines=$(get_branch_lines "$plan_details") | |
delete_disabled_branches "$branch_lines" | |
} | |
main "$PLAN_KEY" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment