Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jarek-przygodzki/1f9754ed9393bc347ec0 to your computer and use it in GitHub Desktop.
Save jarek-przygodzki/1f9754ed9393bc347ec0 to your computer and use it in GitHub Desktop.
Script to delete all disabled branches of a Bamboo build plan
#!/usr/bin/env groovy
/*
* Deletes all disabled branches of a Bamboo build plan
*
* Usage
* -----
* groovy bamboo_delete_disabled_branches.groovy \
* --bamboo-url <BAMBOO_URL, i.e. http://mycompany/bamboo> \
* --plan-key <PLAN_KEY> \
* --user <USER> --password <PASSWORD>
*
* Author: Jarek Przygódzki <jarek.przygodzki@gmail.com>
*/
def MAX_BRANCH_RESULT = 500
@Grapes(
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7.1')
)
import groovyx.net.http.*
def cli = new CliBuilder(usage:'groovy bamboo_delete_disabled_branches.groovy')
cli.with {
h longOpt: 'help', args: 0, 'Usage information', required: false
_ longOpt: 'dry-run', args: 0, "Don't delete anything"
_ longOpt: 'bamboo-url', args: 1, argName:'Bamboo URL', 'URL of the Bamboo instance, i.e. https://bamboo.mycompany.com', required:true
_ longOpt: 'plan-key', args: 1, argName:'plan key', 'Bamboo plan key', required:true
_ longOpt: 'user', args: 1, argName: 'user', "User name", required: true
_ longOpt: 'password', args: 1, argName: 'pass', "Password", required: true
}
def options = cli.parse(args)
if(!options) {
return
}
def bambooUrl = options.'bamboo-url'
def dryRun = options.'dry-run'
def user = options.'user'
def pass = options.'password'
def planKey = options.'plan-key'
def bambooGetPlanUrl = "$bambooUrl/rest/api/latest/plan/$planKey/branch"
def bambooDeletePlanUrl = "$bambooUrl/chain/admin/deleteChain!doDelete.action"
def http = new RESTClient(bambooUrl)
//http.auth.basic(user, pass) doesn't work with GET
http.headers['Authorization'] = 'Basic '+"$user:$pass".getBytes('iso-8859-1').encodeBase64()
http.headers["X-Atlassian-Token"] = "no-check"
http.contentType = ContentType.JSON
def deleteBranch
if(!dryRun)
deleteBranch = { buildKey -> http.post(path: bambooDeletePlanUrl, query: ['os_authType':'basic', 'buildKey' : buildKey]) }
else
deleteBranch = { buildKey -> }
def deleteBranches = { branches -> branches.each {
def buildKey = it.key
println "Deleting $buildKey ..."
deleteBranch(buildKey)
}
}
def getAllBranches = {
http.get(path: bambooGetPlanUrl, query: ['expand':'branches', 'max-results': MAX_BRANCH_RESULT])
}
println "Deleting disabled branches from $planKey ($bambooUrl)"
def resp = getAllBranches()
def branches = resp.data.branches.branch
def isBranchDisabled = { branch -> !branch.enabled }
def disabledBranches = branches.findAll isBranchDisabled
println "Found (${disabledBranches.size()}) disabled branch plans"
println "Deleting branch plans" + (dryRun ? " [dry run]" : "")
deleteBranches(disabledBranches)
println 'Done'
@maria-fleetwood-loopup
Copy link

Does not work on Bamboo 6.0.0, no errors messengers just do not delete any disabled branches

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