How can we write script in script runner in jira for workflow validators

syamala yerramsetty September 23, 2019

Hi ,

I want to write script in script runner for workflow in between transition for validators to check the issue link is enter or not (tested by OR created by) any one of link has to enter in the transition.

So please let me know your inputs and help me out.

 

Thanks in advance,

Regards,

Syamala.Y

2 answers

0 votes
fran garcia gomera
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 23, 2019

Hi, @syamala yerramsetty 

It would be something like this (you should add this as condition in a simple script validator):

 

import com.atlassian.jira.component.ComponentAccessor

if (issue.issueType.name == 'Tarea IT'){   //checks the issuetype in case you don't want to apply to everyone
if (cfValues['linked issues']) return true;  //You have to check here the kind of links

return false;

}
return true

syamala yerramsetty September 24, 2019

Hi  fran garcia gomera,

Thanks for your input.

I have written below code in script runner but getting error in 4th line "Linked Issues" that field does not exists or multiple field exists for the same name.

import com.atlassian.jira.component.ComponentAccessor

if (issue.issueType.name == 'Story'){   //checks the issuetype in case you don't want to apply to everyone
if (cfValues['Linked Issues']) return true;  //You have to check here the kind of links

return false;

}

return true

So could please help me out.

Regards,

Syamala.Y

fran garcia gomera
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 24, 2019

@syamala yerramsetty 

My bad, i gave half the solution.

We had to use a workaround. I try to explain it completely.

The trouble is that you can't check as a validator the linked issues, there is no information until the issue is created.

The workaround was adding a new field, check that field in the validator with the code i added there and then copy the value of that field as a postfunction with the issueLink Manager.

Step by step

  1. create a new field (in the code is that one called 'Linked Issues') of type single issue picker.
  2. Add it to the create screen of your workflow.
  3. Add a new validator (simple script validator : in the condition add the code and in the Field select select 'Linked Issues' field)  in the create transition.
import com.atlassian.jira.component.ComponentAccessor

log.warn(issue.issueType.name)

if (issue.issueType.name == 'Story'){
if (cfValues['Linked Issues']) return true;

return false;

}
return true 

with this, that field is required for the issuetype you need in case you need to apply only to some issue types.

4. Create a postfunction with this script

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLinkManager
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.issue.IssueManager

//uncomment to check in console
//
//def issueManager = ComponentAccessor.getIssueManager()
//def issue = issueManager.getIssueObject("AE-82")

//end of check in console

def customFieldManager = ComponentAccessor.getCustomFieldManager()
def userManager = ComponentAccessor.userManager
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
def issueManager = ComponentAccessor.getIssueManager()


CustomField field = customFieldManager.getCustomFieldObjectByName('Linked Issues')
def clave = field.getValue(issue).toString()
def issueenlazada = issueManager.getIssueObject(clave)
IssueLinkManager issueLinkManager = ComponentAccessor.getIssueLinkManager();

issueLinkManager.createIssueLink(issue.getId(), issueenlazada.getId(), 10400, 1, user)

 That 10400 on the last line is the type of link you want that issuelink to be, in my case complements. This is the complete list

10000Blocksis blocked byblocks 
10001Clonersis cloned byclones 
10002Duplicateis duplicated byduplicates 
10003Relatesrelates torelates to 
10100jira_subtask_linkjira_subtask_inwardjira_subtask_outwardjira_subtask
10200Epic-Story Linkhas Epicis Epic ofjira_gh_epic_story
10300Gantt End to Starthas to be done afterhas to be done before 
10301Gantt Start to Starthas to be started together withhas to be started together with 
10302Gantt End to Endhas to be finished together withhas to be finished together with 
10303Gantt Start to Endstart is earliest end ofearliest end is start of 
10400ComplementsIs complemented byComplements 
10500Problem/Incidentis caused bycausesservicedesk_automation_default_linktype
     

And that's all.

A little bit messy but it's working for me.

Sorry i gave you false hope of this being very much easier

0 votes
Nic Brough -Adaptavist-
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
September 23, 2019

There are some useful examples of validators at https://library.adaptavist.com/search?q=validator

Suggest an answer

Log in or Sign up to answer