Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Script to delete all disabled branches of a Bamboo build plan.
#!/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é <sguillope@lixar.com>
# -----------------------------------------------------------------------------
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[]'
}
function delete_disabled_branches {
while read -r branch_line; do
if is_branch_disabled "$branch_line"; then
local branch_key=$(get_branch_key "$branch_line")
delete_branch "$branch_key"
fi
done <<< "$branch_lines"
}
function is_branch_disabled {
local enabled=$(echo $1 | jq '.enabled')
if [ "$enabled" == "false" ]; then
return 0
else
return 1
fi
}
function delete_branch {
local branch_key=$1
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"
@tom-haines
Copy link

tom-haines commented Oct 28, 2014

Very helpful – thanks!

Recommend to add:
--header "X-Atlassian-Token: no-check"
to line 53 to avoid XSRF protection

echo $(curl --silent --user "$username:$password" --header "Accept: application/json" --header "X-Atlassian-Token: no-check" "$BAMBOO_GET_PLAN_URL")

See: https://jira.atlassian.com/browse/STASH-4355

@markmevans
Copy link

markmevans commented Nov 24, 2014

This is fantastic, I don't think Atlassian understands how painful this is. Make's me wonder if they even use it on anything with short lived branches.

FYI - OnDemand users also need --header "X-Atlassian-Token: no-check" in the delete_branch function.

@sguillope
Copy link
Author

sguillope commented Nov 25, 2014

Thanks @tom-haines and @markmevans, it's been fixed now.

@fazy
Copy link

fazy commented Nov 25, 2014

Superb! The latest revision works for me perfectly, I only needed to change this line (as expected):

readonly BAMBOO_BASE_URL="https://my-company-sub-domain.atlassian.net/builds"

Thanks very much. :)

@rcdailey
Copy link

rcdailey commented Dec 29, 2014

For me this wasn't working, because is_branch_enabled was always returning 1. I found out this is because the result of the enabled property in JSON has whitespace characters in it. If you trim it, it works great. Here is the new is_branch_enabled function:

function is_branch_disabled {
    local enabled=$(echo "$1" | jq '.enabled' | tr -d [[:space:]])
    if [ "$enabled" == "false" ]; then
        return 0
    else
        return 1
    fi
}

I've simply added tr -d [[:space:]] at the end of the command to get the value for enabled.

@rcdailey
Copy link

rcdailey commented Dec 29, 2014

Same issue with delete_branch... for some reason I'm getting spaces in the branch key. This trims it and fixes the issue:

function delete_branch {
    local branch_key=$(echo $1 | tr -d [[:space:]])
    curl -qs -X POST --user "$username:$password" --header "X-Atlassian-Token: no-check" --data "buildKey=$branch_key" "${BAMBOO_DELETE_PLAN_URL}"
}

@amaltson
Copy link

amaltson commented Jun 22, 2015

Thanks for sharing, this was super useful!

@rcdailey
Copy link

rcdailey commented Oct 26, 2015

I have written a python script to perform batch deletion of branches with many configurable options (disabled or enabled, regex matching on name, etc). See the script here:
https://gist.github.com/rcdailey/9eff84365ccd7e7c9108

@jkstrauss
Copy link

jkstrauss commented Jul 12, 2016

Very useful.

I forked this gist to improve speed. Feel free to merge any of my branches.

@kasabov
Copy link

kasabov commented Jul 13, 2017

--header "X-Atlassian-Token: no-check": did I get this right - you can disable XSRF protection on the client-side? how is this secure and why implement XSRF at all then?

@cmoetzing
Copy link

cmoetzing commented Jan 9, 2019

@kasabov Good question, wondering too.

As of bamboo 6.6 the URL to delete changed. See here. Implemented here.

@Hoopie
Copy link

Hoopie commented Jul 5, 2019

@cmoetzing - you script worked a charm for me on 6.5

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment