Scripting Extension Basic Hello World

Description
DCwom
Posts: 37
Joined: Mon Sep 29, 2014 7:00 pm

Scripting Extension Basic Hello World

Postby DCwom » Thu Oct 30, 2014 6:22 pm

I continue to be baffled with the Scripting Extension. I believe that I have it properly installed, and I see ScriptCondition & ScriptFunction are available in the workflow actions drop-down menus. What I'm missing is how do I call my script? I've tried to include .txt files with names like ScriptCondition.txt and ScriptFunction.txt in the Polarion/scripts folder but they don't seem to be recognized. My test.js has a simple comment addition but I don't think it is being reached.
This is my test.js script:

Code: Select all

 var returnvalue = "";
var comment = "HELLO WORLD";
var title =  "HELLO WORLD";
workItem.createComment(com.polarion.core.util.types.Text.plain(comment),title,null).save()
returnvalue;

This is my ScriptFunction.txt script:

Code: Select all

 <job name="Script Job" id="script.job" cronExpression="" disabled="true" scope="system">
<scriptName>test.js</scriptName>
<scriptEngine>js</scriptEngine>
<properties>
<myProperty>myPropertyValue</myProperty>
</properties>
</job>


What am I doing wrong? Does anyone have a simple hello world example that would help me?
Dennis

engeleb
Posts: 199
Joined: Wed Aug 09, 2006 10:55 am

Re: Scripting Extension Basic Hello World

Postby engeleb » Fri Oct 31, 2014 7:39 am

Hello Dennis,
add following parameters to the function in the workflow configuration:
  • script: The script file in C:\Polarion\scripts to be executed.
  • engine: Engine that executes the script, you can use js as value.

What you have put into ScriptFunction.txt could be used in the scheduler configuration of Polarion to run a script as Polarion job.

Best Regards,
Benjamin

martins
Posts: 181
Joined: Thu Nov 05, 2009 3:24 pm

Re: Scripting Extension Basic Hello World

Postby martins » Fri Oct 31, 2014 8:08 am

Hello Dennis,

you have to read the documentation more carefully.

In a workflow's ScriptCondition or ScriptFunction you have an object called "workflowContext" which is of type "com.polarion.alm.tracker.workflow.ICallContext". This object has a method "getWorkItem()" which gives you access to your current Workitem (see SDK doc for more details). Therefore your "test.js" script requires a call like

Code: Select all

var workItem = workflowContext.getWorkItem();
in the beginning.

If you intend to write a ScriptCondition your script requires a returnvalue which decides whether the transition is allowed or not. In a ScriptCondition you should not modify your Workitem!

If you intend to write a ScriptFunction you don't need a returnvalue because this is ignored. But your code rows shut always be finalized with a semicolon ;-)

Your script files should be named *.js and be located in the /scripts folder. To include a function named "ScriptFunction.js" and a condition named "ScriptCondition.js" within your workflow your *-workflow.xml must include something like

Code: Select all

...
  <conditions>
    <condition name="ScriptCondition">
      <param name="engine" value="JavaScript"/>
      <param name="script" value="ScriptFunction.js"/>
    </condition>
  </conditions>
  <functions>
    <function name="ScriptFunction">
      <param name="engine" value="JavaScript"/>
      <param name="script" value="ScriptCondition.js"/>
    </function>
  </functions>
...
within the corresponding "action" element.

You don't need to modify the xml file directly - you can set all those parameters within the workflow configuration UI - but in the end it should look like list above - otherwise it won't work :-)

As Benjamin mentioned, your ScriptFunction.txt can be inserted inside the global jobs/schedule.xml to define a scriptjob. This has nothing to do with ScriptFunction and ScriptCondition but is another possibility to extend Polarion with own scripts. Within a ScriptJob you don't have a Workitem context because jobs are either executed in System or in a Project context - never in a Workitem context. But you have a "trackerService" object which enables you to query for workitems and also do operations on them. To modify workitems you have to embed your modifications within a transaction. Therefore you need the the "transactionService" which can be included with

Code: Select all

var transactionService = com.polarion.platform.core.PlatformContext.getPlatform().lookupService(com.polarion.platform.ITransactionService);


Kind regards

Martin

DCwom
Posts: 37
Joined: Mon Sep 29, 2014 7:00 pm

Re: Scripting Extension Basic Hello World

Postby DCwom » Fri Oct 31, 2014 1:07 pm

Benjamin, Martin,
Thanks for the help I've got it working now. I used the UI to config the workflow but I could not find where the xml file that you mentioned is located.
workflow your *-workflow.xml must include something like


For posterity, here is my working Hello World script with a screen shot of how I set up the workflow to call it.

Code: Select all

var workItem = workflowContext.getWorkItem();
var comment = "Hello World";
var title =  "Hello World";
workItem.createComment(com.polarion.core.util.types.Text.plain(comment),title,null).save()

[img]
download/file.php?mode=view&id=68
[/img]
Attachments
PolarionHelloWorld.jpg
PolarionHelloWorld.jpg (192.2 KiB) Viewed 14138 times

martins
Posts: 181
Joined: Thu Nov 05, 2009 3:24 pm

Re: Scripting Extension Basic Hello World

Postby martins » Fri Oct 31, 2014 1:27 pm

Congratulations!

The first step is always the hardest to get a script running.

The "*-workflow.xml" files are at the back-end (in the SVN repository, path .polarion/tracker/workflow/...). This is where Polarion stores all the configuration you did in the UI (and some more). Sometimes the UI is not so handy and it's easier to check or change configuration directly in the back-end. But if you do so you should realy know what you are doing there!

Kind regards

Martin

micdal
Posts: 25
Joined: Thu Feb 09, 2012 6:22 am

Re: Scripting Extension Basic Hello World

Postby micdal » Thu May 07, 2015 6:38 am

Just a question relation to the ScriptCondition: how to return "condition met" or "condition not me"?

I've wirtten a test function with just a "return true;" statement and added it to a document workflow (Polarion 2015).
Everything looks fine, but when trying to change the status, the workflow action is greyed out and "Workflow condition ScriptCondition did not pass check".

Hope someone can help

King regards,
Michael

martins
Posts: 181
Joined: Thu Nov 05, 2009 3:24 pm

Re: Scripting Extension Basic Hello World

Postby martins » Thu May 07, 2015 6:59 am

Hello Michael,

You don't have to "return" the result because your script is not "called".

Just write your result (true or false) to a variable and put this variable in the last line of your script, e.g.:

Code: Select all

var returnValue=false;
// some code
if(someCondition=fulfilled) returnValue=true;
// more code
returnValue;

Kind regards

Martin

micdal
Posts: 25
Joined: Thu Feb 09, 2012 6:22 am

Re: Scripting Extension Basic Hello World

Postby micdal » Thu May 07, 2015 7:15 am

Hello Martin,

thanks a lot: omitting "return" fixes my issue.
Sometimes life is so easy ;-)

Kind regards,
Michael

micdal
Posts: 25
Joined: Thu Feb 09, 2012 6:22 am

Re: Scripting Extension Basic Hello World

Postby micdal » Mon May 11, 2015 11:16 am

Hello,

one further question regarding ScriptCondition (and WorkflowCondition):
how to output log messages (or something similar) from a condition script?

I try to write a ScriptCondition but didn't get the results expected and now I'm searching a way to look at variables etc.

Hope someone has a hint.

Kind regards,
Michael

micdal
Posts: 25
Joined: Thu Feb 09, 2012 6:22 am

Re: Scripting Extension Basic Hello World

Postby micdal » Wed May 20, 2015 11:24 am

Hello,

I got a solution from Polarion Support to write "debug" messages to Polarion log file:
by using the following instruction

Code: Select all

com.polarion.core.util.logging.Logger.getLogger("Name of Function").info("Value = " + myValue);

the following line will be put in the Polarion log file

Code: Select all

... INFO  Name of Function  - Value = 123


Kind regards,
Michael


Return to “Polarion Application Lifecycle Management (ALM)”

Who is online

Users browsing this forum: Google [Bot] and 8 guests