Scripted Workflow Functions and Conditions

In all workflow configurations, you can use ScriptCondition and ScriptFunction for workflow conditions or functions that are defined via a custom script. The Groovy and JavaScript/ECMAScript languages are supported. This topic provides reference details for both ScriptCondition and ScriptFunction, and also describes the procedure for implementing custom scripts for workflow conditions and functions.

ScriptCondition

Applicable for all workflow configurations — Work Items, Documents, and Test Runs. Evaluates whether or not a specific workflow action is available according to the return value of a custom script, which is the result of the last line of the script. If the script returns Boolean value false, then the workflow action will not be available. If it returns some text, then the workflow action will not be available and the returned text will be shown as the reason. If the script returns Boolean value true, then the workflow action can proceed.

Parameters:

engine

The name of the scripting engine to use. Valid values: groovy for the Groovy engine, or js for JavaScript/ECMAScript engines.

script

The name of the script to run. The script file must be stored in the scripts folder of the Polarion installation's file system. See Script implementation, below, for specific path.

For example, given the existence of a Javascript script containing the line "Test".equals(workflowContext.workItem.title); The workflow condition will be satisfied if the title of the Work Item is "Test".

Administrators of Linux systems will require Owner, User and Group privileges, and be set up as a Polarion System User.

ScriptFunction

Invokes a custom script to perform some action or actions in conjunction with a workflow transition. Applicable for all workflow configurations — Work Items, Documents, and Test Runs.

Parameters:

engine

The name of the scripting engine to use. Valid values: groovy for the Groovy engine, or js for JavaScript/ECMAScript engines.

script

The name of the script to run. The script file must be stored in the scripts folder of the Polarion installation's file system. See Script implementation, below, for specific path.

reason

A message string to return if the condition is not satisfied. If not specified, a default string is returned.

Administrators of Linux systems will require Owner, User and Group privileges, and be set up as a Polarion System User.

Script implementation

  1. Copy your custom script to Polarion's scripts directory.

    Path: C:\Polarion\scripts (Windows) and /opt/polarion/scripts (Linux).

    If the directory does not yet exist, create it. Note that the above root paths are the default created by Polarion's installer programs. The installing administrator has the option to specify a different installation root directory.

  2. Enter one of the following Administration pages, depending on the type of workflow you wish to configure:

    • Work Items: AdministrationWork ItemsWorkflow

    • Documents: AdministrationDocuments & PagesDocument Workflow

    • Test Runs: AdministrationTestingTest Run Workflow

  3. Click Edit beside the artifact to which you'd like to add the script condition or function.

  4. Click Edit beside the Action you to which you want to add the script condition or function.

  5. Select ScriptCondition from the Conditions drop-down list, or ScriptFunction from the Functions drop-down list and click (green plus sign).

  6. Click Edit beside the newly created Condition or Function.

  7. Define the following required parameters:

    (You can also define additional parameters based on your script.)

    • engine: The name of the scripting engine to use. Groovy (.groovy file type) and JavaScript/ECMAScript (.js file type) are supported.

      Valid values: groovy for the Groovy engine, or js for JavaScript/ECMAScript.

    • script: The name of the script in the scripts folder to run as the Condition or Function.

  8. Click (X icon) to close the Parameters dialog box and again to close the Details for Action dialog box.

    The new Condition or Function appears for the Action you defined it for.

  9. Click Save at the top.

Inside the script you will be able to access:

  • com.polarion.alm.tracker.workflow.ICallContext

  • com.polarion.alm.tracker.workflow.IArguments

They are passed to com.polarion.alm.tracker.workflow.IFunction/com.polarion.alm.tracker.workflow.ICondition as variables with the name, workflowContext and arguments.

Tip:

You can find details on the classes mentioned above in the javadoc in the SDK document.

Default location: Polarion\polarion\SDK\doc\javadoc\index.html

A workflow condition must evaluate if a specific workflow action is available or not.

This is decided by the return value. (The last line of the script.)

For example, if the last line of a custom JavaScript was, "test".equals(workflowContext.workItem.title);, then the condition is fulfilled if the title of the Work Item is test.

The possible return values are:

  • true - The condition is fulfilled.

  • False - The condition is not fulfilled.

  • String - A non zero-length String means that the Condition is not fulfilled and the String content is displayed by Polarion GUI as a tool-tip in the Work Item form.

Example JavaScript: Add comment

var workitem = workflowContext.getTarget();

//title might be used for querying for status changes
var title = "workflow action:"+ workflowContext.getActionId() + " processed on:"+new java.sql.Date(new Date().getTime()) ;

//copy following fields to comment body
var commentFields = arguments.getAsSetOptional("fields").toArray();
var comment = "";
for ( i in commentFields )
{
  if(!comment.equals("")) comment = comment + "\n";
  name = commentFields[i];
  comment = comment +  name + ":"+workitem.getValue(name); 
}


//create a comment
workitem.createComment(com.polarion.core.util.types.Text.plain(comment),title,null).save();