How to add/remove Task variables from User Input Tasks using Jython API

Follow

Shashank Srivastava -

Scenario
You have a few Release variables & you want to add &/or remove to/from User Input Tasks using Jython API from within a Jython Script task.
Environment

XL Release

Steps to Perform

1. Create your Release Variables. This can be done manually or using Jython API/ From within Jython script task.

2. Create 2 user Input tasks A & B. This can, again, be done manually or using Jython API. My script does everything automatically.

3. Below is the script that you can use to add/remove Task variables. I have put relevant comments to make the script more descriptive.

#Creating 3 Release variables first.
print "Creating a Release variable userName... \n"
myvar = Variable('userName', 'shashank')
myvar.setLabel("userName")
releaseApi.createVariable(release.id, myvar)
var_userName = releaseApi.getVariables(release.id)
print "Below is newly created variable. \n"
print var_userName

print "\n Creating a Release variable passWord... \n"
myvar2 = Variable('passWord', 'password')
myvar2.setLabel("passWord")
releaseApi.createVariable(release.id, myvar2)
varList = releaseApi.getVariables(release.id)
var_passWord = varList[1]
print "Below is newly created variable. \n"
print var_passWord

print "\n Creating another Release variable hostName... \n"
myvar3 = Variable('hostName', 'mysql-server')
myvar3.setLabel("hostName")
releaseApi.createVariable(release.id, myvar3)
varList = releaseApi.getVariables(release.id)
print "Printing Variables list... \n"
print varList
print "\n"
var_hostName = varList[2]
print "Below is newly created variable. \n"
print var_hostName

#Creating a User Input task & adding variables to it from Release variables.
print "\n Creating a User Input task A... \n"
phase = getCurrentPhase()
task = taskApi.newTask("xlrelease.UserInputTask")
task.title = "User Input task A"
task.setVariables(varList)
taskApi.addTask(phase.id, task)
print "User Input task created. \n"

#Creating a User Input task, adding variables to it from Release variables & then removing the 3rd variable var_hostName.
print "\n Creating a User Input task B... \n"
phase = getCurrentPhase()
task2 = taskApi.newTask("xlrelease.UserInputTask")
task2.title = "User Input task B"
task2.setVariables(varList)
taskApi.addTask(phase.id, task2)
print "User Input task2 created. \n"
print "Printing User Input task Variables...\n"
print task2.variables
print "\n Removing User Input task variable %s" % var_hostName.getLabel()
print "\n"
task2.variables = [x for x in task2.variables if x.id != var_hostName.id]
taskApi.updateTask(task2)
print "Variable removed from 2nd User Input task."

XL_Release_-_Release_variables.png

XL_Release_-_task_variables_added.png

XL_Release_-_first_task_variable_removed.png

Caveats (if any)

This script intentionally removes the first Task variable. You can manipulate it to suit your needs. 

Additional Information

Refer to XebiaLabs documentation for more information.

https://docs.xebialabs.com/xl-release/#

Tags
 kb, how-to, xl-release, Jython API, scripts
Have more questions? Submit a request
Powered by Zendesk