How to create a new UserInput Task with Variable

Hello,

Someone know how can i create a new upserinput task with “X” new variable inside using Jython? Below you can see that I can create the task, but I dont know how add the variable:

import sys, string, time, os, re

import com.xhaus.jyson.JysonCodec as json
from com.xebialabs.xlrelease.domain import Task
from com.xebialabs.deployit.plugin.api.reflect import Type
from java.text import SimpleDateFormat

def createSimpleTask(phaseId, taskTypeValue, title, taskParent, propertyMap):
“”"
Function that creats a simple task
“”"
parentTask = taskApi.newTask(taskTypeValue)
parentTask.title = title
parentTask.description = title
sdf = SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”)

for item in propertyMap:
    if item.lower().find("date") > -1:
        if propertyMap[item] is not None and len(propertyMap[item]) != 0:
            parentTask.setProperty(item, sdf.parse(propertyMap[item]))
    else:
        parentTask.setProperty(item, propertyMap[item])

searchResult = taskApi.searchTasksByTitle(taskParent, None, release.id)
parallelGroup = searchResult[0]
taskApi.addTask(parallelGroup.id, parentTask)

Create XL Deploy “Deploy task”

i = 0

createSimpleTask(phase.id, “xlrelease.UserInputTask”, “SELECT PACKAGES”, “CREATE TASKS”,
{‘description’: ‘DEFINE VALUES’})
remoteTask = taskApi.searchTasksByTitle(“SELECT PACKAGES”, “DEFINE VALUES”, release.id)[0]

while i < releaseVariables[“QUANTITY PACKAGES”]:
taskApi.updateTask(remoteTask.id, remoteTask)
i = i + 1

Good question!

The API to do so is not quite obvious and appears to be not exposed as a public API. The recommended way to configure an InputTask would be to use the DevOps as-code YAML format.

If you are not dynamically modifying releases I would definitely recommend using the DevOps as-code feature.

What is your use case if I may ask?

Having said that, I can give a couple of hints on how it could work in Jython but to be honest I haven’t tried it out.

First, be aware that there is a distinction between the variable key (‘myvar’) and value (‘myvalue’) and the variable object itself that also contains all the metadata like Label and Description.

To configure the UserInputTask you have to link is to one or more variable objects from the release.

Here is a snippet that may work. Again, I have not tested it.

def link_variable_to_user_input_task(user_input_task, variable_key):
    variable_object = getRelease().getVariablesByKeys().get(variable_key)
    user_input_task.getVariables().add(variable_object)

Hope this helps!

Hes Siemelink

Hello Hes,

Thanks for the reply. In my case I have a task xldeploy.getallversions that will get all packages and versions and save in a Listbox, but the user should decide how many packages will deploy, so after the user put the value, this task will create another task with x ( x = value put before by user) variables to the user select the packages to be deployed. After it I have another task that will get this packages and create the task to deploy.

I will try your suggestion and let you know the end code if work.
Thanks again.

Hello Hes,

My code now is like below, but is faling due the getRelease()

import sys, string, time, os, re
import com.xhaus.jyson.JysonCodec as json
from com.xebialabs.xlrelease.domain import Task
from com.xebialabs.deployit.plugin.api.reflect import Type
from java.text import SimpleDateFormat

def createSimpleTask(phaseId, taskTypeValue, title, taskParent, propertyMap):
“”"
Function that creats a simple task
“”"
parentTask = taskApi.newTask(taskTypeValue)
parentTask.title = title
parentTask.description = title
sdf = SimpleDateFormat(“yyyy-MM-dd hh:mm:ss”)

for item in propertyMap:
    if item.lower().find("date") > -1:
        if propertyMap[item] is not None and len(propertyMap[item]) != 0:
            parentTask.setProperty(item, sdf.parse(propertyMap[item]))
    else:
        parentTask.setProperty(item, propertyMap[item])

searchResult = taskApi.searchTasksByTitle(taskParent, None, release.id)
parallelGroup = searchResult[0]
taskApi.addTask(parallelGroup.id, parentTask)

def link_variable_to_user_input_task(user_input_task, variable_key):
variable_object = getRelease().getVariablesByKeys().get(variable_key)
user_input_task.getVariables().add(variable_object)

Create XL Deploy “Deploy task”

i = 0
createSimpleTask(phase.id, “xlrelease.UserInputTask”, “SELECT PACKAGES”, “CREATE TASKS”,
{‘description’: ‘DEFINE VALUES’})
remoteTask = taskApi.searchTasksByTitle(“SELECT PACKAGES”, “DEFINE VALUES”, release.id)[0]
link_variable_to_user_input_task(“SELECT PACKAGES”, releaseVariables[“PACKAGE_LISTBOX”])

remoteTask.taskApi.updateTask(remoteTask.id, remoteTask)

Can you help me ? Thanks

Interesting use case… Is there much variance in X, the amount of deployments? Say if the maximum is ten I would just hardcode 10 variables and have deployments with preconditions.

It is a bit crude, but saves you the trouble of having a dynamic release.

Can you try if task.getRelease() works?

Kind regards,

Hes

Maybe that helps:, it creates a list variable with content 1,2,3,4

from com.xebialabs.xlrelease.api.v1.forms import Variable  
mylist = [1,2,3,4]
listvar = Variable()
listvar.value = mylist
listvar.key = "mylistvar"
listvar.type = "xlrelease.ListStringVariable"
releaseApi.createVariable(release.id,listvar )

More snippets here_
https://github.com/xebialabs-community/xlr-jython-code-snippets

Hi @Hes ,

I have tried to add variable to a userInputTask by using your suggestion -

user_input_task.getVariables().add(variable_object)

I am getting below attribute error -

AttributeError: ‘list’ object has no attribute ‘add’

I am able to create the userInputTask using Jython API and I have also dynamically created my required variables, just stuck now on how to add these variables to the userInputTask via Jython API, wondering why this is not documented in the Jython API guide.
any ideas/suggestion?

regards

There is some Java-Python conversion going on. Can’t test code right now, but it seems you are getting a Python list.

Try getting the ‘raw’ Java object by doing something like

task2 = unwrap(user_input_task)

Kind regards,

Hes Siemelink