How to validate issuekey provided in linked issue field is bug type while duplicate transition

Dayanand
Contributor
November 19, 2023

Hi all,

We have transition called duplicate when use clicks it window opens with field Linked Issue then  user choose as duplicate value then we will have another field call Issue (issue searchable field ) for this field how we write validator or scriptrunner script to validate that enter key is bug type then only transition otherwise through error massage (refer attached screenshot for more)

image.png

2 answers

2 accepted

3 votes
Answer accepted
Ram Kumar Aravindakshan _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.
November 21, 2023

Hi @Dayanand

Your requirement can be achieved very easily using the ScriptRunner Server-Side Behaviour.

There is no need to use the Workflow Validator for this.

Below is a sample working code for your reference:-

import com.adaptavist.hapi.jira.issues.Issues
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours
def linkedIssues = getFieldById('issuelinks-issues')
def linkedIssuesValue = linkedIssues.value as List<String>
linkedIssues.clearError()

linkedIssuesValue.each {
def issue = Issues.getByKey(it)
if (issue.issueType.name != 'Story') {
linkedIssues.error = 'Only Story Types Are Allowed'
}
}

Please note that the sample working code above is not 100% exact to your environment. Hence, you will need to make the required modifications. In the sample code above, I have provided a scenario where only the Story issue type should be permitted in the Linked Issue field. However in your case, since you want to check this against the Bug issue type, you will need to make the required modification in the if condition.

Below is a screenshot of the Behaviour configuration:-

behaviour_config.png

I have added a Server-Side Behaviour for the Linked Issue field in the configuration above.

In the code, I have added a very basic validation on the Linked Issue field that will only allow Story-type issues. If any other issue type is selected, it will return the error message, and the issue will fail to be created.

Below are a couple of test screenshots for your reference:-

1. For the test case below, I am going to select 2 issues: one is of the Story type, and the second is of the Bug-type, as shown in the screenshot below:-

test1.png

 

2. When the Story type issue is added, as expected, no error message is returned, as shown in the screenshot below:-test2.png

 

3. However, if the Bug issue type is added, as expected, the error message is displayed as shown in the screenshot below:-

test3.png

I hope this helps to solve your question. :-)

Thank you and Kind regards,

Ram

Dayanand
Contributor
November 21, 2023

@Ram Kumar Aravindakshan _Adaptavist_ ,

Thank you for the complete solution however facing below package error

image.png 

any help appreciated 

Dayanand
Contributor
November 21, 2023

script runner plugin Installed version: 7.8.0 and code supported versions looks fine and using JIRA server 8.20.12 let me know do i need to update plugin or any way i can create Issue object to solve this error?

Ram Kumar Aravindakshan _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.
November 22, 2023

Hi @Dayanand

Right, your getting the error message from the import statement because the current ScriptRunner version you are using doesn't support the HAPI feature. Hence, it's not able to invoke the classes from HAPI's API.

Please upgrade your ScriptRunner plugin to the latest release. It will resolve the problem.

Thank you and Kind regards,

Ram

Dayanand
Contributor
November 22, 2023

@Ram Kumar Aravindakshan _Adaptavist_ ,

#We have many existing behaviours, listeners, jobs, scripted fields based on scriptrunner version 7.8.0 Is upgrading plugin will break existing scripts? if yes what will be best to practice or easy steps to upgrade scriptrunner plugin?

Second# If possible can i get the above script that works with scriptrunner version 7.8.0 someting like defining Issue object by importing this package -> import com.atlassian.jira.issue.Issue 

 

Thank you in advance, 

Daya

Ram Kumar Aravindakshan _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.
November 22, 2023

Hi @Dayanand

Upgrading your ScriptRunner plugin to the latest release will not impact your existing code in any way. You can still reuse it.

The main benefit of upgrading your ScriptRunner plugin is that you can use the HAPI feature, which helps simplify your code.

If you still intend to use the old approach, you must add additional code, i.e. invoking the ComponentAccessor as shown below.

import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.jira.groovy.user.FieldBehaviours
import groovy.transform.BaseScript

@BaseScript FieldBehaviours behaviours
def linkedIssues = getFieldById('issuelinks-issues')
def linkedIssuesValue = linkedIssues.value as List<String>
linkedIssues.clearError()

def issueManager = ComponentAccessor.issueManager

linkedIssuesValue.each {
def issue = issueManager.getIssueByCurrentKey(it)
if (issue.issueType.name != 'Story') {
linkedIssues.error = 'Only Story Types Are Allowed'
}
}

Thank you and Kind regards,

Ram 

Like Vikrant Yadav likes this
Dayanand
Contributor
November 22, 2023

@Ram Kumar Aravindakshan _Adaptavist_ ,

Thank you so much for valuable , quick responses and its working as expected.

1 vote
Answer accepted
Adrien Low _ServiceRocket_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 20, 2023

Hello @Dayanand ,

Perhaps you can refer to the documentation below for more information:-

With some tweaking, you should be able to configure a Simple scripted validator [ScriptRunner] in your workflow with the below script:-

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueFieldConstants
import com.atlassian.jira.issue.fields.IssueLinksSystemField
import com.opensymphony.workflow.InvalidInputException
import webwork.action.ActionContext

def fieldManager = ComponentAccessor.getFieldManager()
def issueManager = ComponentAccessor.getIssueManager()
def linksSystemField = fieldManager.getField("issuelinks") as IssueLinksSystemField

def
request = ActionContext.getRequest()

if (request) {
    def params = request.getParameterMap()
    def issueLinkingValue = linksSystemField.getRelevantParams(params) as IssueLinksSystemField.IssueLinkingValue

    for(def issueKey: issueLinkingValue.linkedIssues) {
        def currentIssue = issueManager.getIssueByKeyIgnoreCase(issueKey)

        if(!currentIssue.getIssueType().getName().equals("Bug")) {
            return false
        }
    }
    return true
}

You might need to tweak the script further for your requirements.

Hope this helps.

Regards.

Dayanand
Contributor
November 20, 2023

@Adrien Low _ServiceRocket_ ,

Thank you for the such valuable support. kindly help me to sort error after pasting above code into Simple scripted validator as below 

on line 

def issueLinkingValue = linksSystemField.getRelevantParams(params) as IssueLinksSystemField.IssueLinkingValue
 
getting below error: [Static type checking] - Cannot find matching method com.atlassian.jira.issue.fields.IssueLinksSystemField#getRelevantParams(java.util.Map <java.lang.String, [Ljava.lang.String;>). Please check if the declared type is correct and if the method exists.

Also, refer attached screen shot for complete error 

Dayanand
Contributor
November 20, 2023

@Adrien Low _ServiceRocket_ ,

Thank you for the such valuable support. kindly help me to sort error after pasting above code into Simple scripted validator as below 

on line 

def issueLinkingValue = linksSystemField.getRelevantParams(params) as IssueLinksSystemField.IssueLinkingValue
 
getting below error: [Static type checking] - Cannot find matching method com.atlassian.jira.issue.fields.IssueLinksSystemField#getRelevantParams(java.util.Map <java.lang.String, [Ljava.lang.String;>). Please check if the declared type is correct and if the method exists.

Also, refer attached screen shot for complete error 

 

image.png

Dayanand
Contributor
November 20, 2023

@Adrien Low _ServiceRocket_ 

script runner plugin Installed version: 7.8.0 and code supported versions looks fine and usig JIRA server 8.20.12

Adrien Low _ServiceRocket_
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 20, 2023

Hello @Dayanand ,

You can probably ignore that error message. I have the same behavior on my end. Try saving the configuration and give it a go.

Regards.

Dayanand
Contributor
November 22, 2023

@Adrien Low _ServiceRocket_ ,

yes with error also its working as expected takes only bug issues in linkedissue field but user looking for below usecases as well

Consider we have 3 issue parent, child1, child2 where parent is first issue created 

#when user clicks on duplicate transition for child1 then user choose parent issue in linkedissue thats fine

#but when user click on duplicate transition for child1 user should not to allowed to use child1 to be entered into linkedissue field as its already duplicate of parent issue this is one more scenario we are looking for

Second# is allow only single bug should be provided into linkedissue field (as of now this field takes multiple bugs issues) 

Tried this code not working if((!currentIssue.getIssueType().getName().equals("Bug")) && (issueLinkingValue.linkedIssues.size() <= 1)) { return false } still it is allowing user can provide 2 bugs and make transition not getting where i am wrong where log.warn(ssueLinkingValue.linkedIssues.size()) shows oputput 2 but condition not working

 

Third# Say user already provided linkedissue then another same issue-key should not be allowed until user removed previous link manually then allow user to add same existing issue as duplicate 

Thank you in advance for your valuable responses and answers

Suggest an answer

Log in or Sign up to answer