Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to validate issue type on linked issue field on create?

Paz Grimberg September 12, 2016

Hi,

Im using script runner and i want to create a scripted validator to limit the "linked issues" field on create by link type ("causes") and issue type(Story, id - 10001).

 

Here is my 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
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.link.IssueLink

MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("ZD-9");

def fieldManager = ComponentAccessor.getFieldManager()
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


    if (! (issueLinkingValue.linkDescription == "causes" && issueLinkingValue.linkedIssues.size() > 0 )) {
        for ( link in issueLinkingValue.linkedIssues){ 
        def object = ComponentAccessor.getIssueManager().getIssueObject(link)
        if(object.getIssueType().toString() != '10001'){
            throw new InvalidInputException(IssueFieldConstants.ISSUE_LINKS,
            "Note: You must link this Sub Bug to a Story.")}
        }
        throw new InvalidInputException(IssueFieldConstants.ISSUE_LINKS,
            "Note: You must use link type 'causes'.")
    }
}

This validator does not work for the issuetype of the chosen keys but it works for the linktype.

What am i doing wrong?

 

Thanks for your help,

Paz

3 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
JamieA
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 12, 2016

Why is the issue ID hard-coded? Can you add some logging for the "issueLinkingValue" and others.

object.getIssueType().toString() != '10001' looks very fishy - the toString implementation does not return the ID AFAICS.

Use 

object.issueType.id != '10001'

Paz Grimberg September 13, 2016

Hi Jamie,

i used object.issueType.id != '10001' and it is still doesnt work..

What do you mean by adding logs for  the "issueLinkingValue" ?

Thanks

Jonny Carter
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 14, 2016

I believe Jamie means something like log.warn(issueLinkingValue) so that you can see the value used by that variable.

That said, if your link type check is working, I'd second Jamie's suggestion and look into whether this condition is right:

object.getIssueType().toString() != '10001'

That's where the script is failing. The question is why? Is the getIssueType() even returning a value?

Jonny Carter
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 14, 2016

Here's a quick version of the script that might help you debug why it's failing. Change the log statements as necessary to get what you're after:

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
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.link.IssueLink

MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("ZD-9");

def fieldManager = ComponentAccessor.getFieldManager()
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

    log.debug(issueLinkingValue.linkDescription)
    log.debug(issueLinkingValue.linkedIssues)
    def linkTypeIsCauses = issueLinkingValue.linkDescription == "causes"
    log.debug("Is the linked value 'causes'? $linkTypeIsCauses")
    def moreThanOne = issueLinkingValue.linkedIssues.size() > 0
    log.debug("Is there more than one link? $moreThanOne")
    if (! (linkTypeIsCauses && moreThanOne) ) {
        log.debug("This rule will fail")
        for ( link in issueLinkingValue.linkedIssues){
            def object = ComponentAccessor.getIssueManager().getIssueObject(link)
            log.debug("The issue type is: ${object.getIssueType()}")
            if(object.getIssueType().toString() != '10001'){
                log.debug("And it's gonna be because the linked issue type wasn't the right type")
                throw new InvalidInputException(IssueFieldConstants.ISSUE_LINKS,
                    "Note: You must link this Sub Bug to a Story.")}
        }
        log.debug("And it's gonna be over the lack of a causes link")
        throw new InvalidInputException(IssueFieldConstants.ISSUE_LINKS,
            "Note: You must use link type 'causes'.")
    }
}
}
Paz Grimberg September 15, 2016

Hi Jonny,

Thanks much!

Before dealing with your debug version i checked out the logs again and found that my problem is that "object" is null,

it means that i cannot get the chosen key's chosen by the user.

What do you think? does the for ( link in issueLinkingValue.linkedIssues){} is even right to use?

Paz 

Jonny Carter
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 15, 2016
Paz Grimberg September 17, 2016

Hi Jonny,

I solved it by changing the "if" statement from if(link in issueLinkingValue.linkedIssues) to 

if(link in issueLinkingValue.getLinkedIssues() ) and it works smile

Thanks for helping!

Here is the full 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
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.link.IssueLink
 
MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("ZD-9");
 
def fieldManager = ComponentAccessor.getFieldManager()
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
 
 
if (! (issueLinkingValue.linkDescription == "causes" && 		issueLinkingValue.linkedIssues.size() > 0 )) {
  throw new InvalidInputException(IssueFieldConstants.ISSUE_LINKS,
            "Note: You must use link type 'causes'.")
    }
       for ( link in issueLinkingValue.getLinkedIssues() ){
       	 def object=ComponentAccessor.getIssueManager().getIssueObject(link)
        if(object.getIssueType().toString() != '10001'){
            throw new InvalidInputException(IssueFieldConstants.ISSUE_LINKS,
            "Note: You must link this Sub Bug to a Story.")
		}
        } 
      
}
Suresh December 5, 2017

Hi @Paz Grimberg @JamieA

@Jonny Carter 

I am trying to add validate on create action.

The validation should be, when user select (from field:Linked Issues ) linktype equal to "Roadmap Item " and we need to select only epic issuetype tickets from issues dropdown, other than epic type we need throw validation error.

the script is not working, did I am missing any thing here.

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 linksSystemField = fieldManager.getField("issuelinks") as IssueLinksSystemField

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

if (issueLinkingValue.linkDescription == "Roadmap Item") {
for ( link in issueLinkingValue.getLinkedIssues() ){
def object=ComponentAccessor.getIssueManager().getIssueObject(link)
if(object.getIssueType().toString() != '11000'){
throw new InvalidInputException(IssueFieldConstants.ISSUE_LINKS,
"Note: You must link this to Epic Issuetype.")
}
}
}
}

 

0 votes
Richard Duffy February 26, 2019

Hi @JamieA @Paz Grimberg 

I am trying to validate the issue type (linked issues) on the create transition so users must link the issue to our issue type called "Initiative" (id = 10008). 

The script does stop give the error message when you try create the ticket and link to another issue type, however it does not allow creation when the user does choose an "Initiative" as linked issue. 

Here is what I currently have:

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
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.link.IssueLink


MutableIssue issue = ComponentAccessor.getIssueManager().getIssueObject("")

def fieldManager = ComponentAccessor.getFieldManager()
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 ( link in issueLinkingValue.getLinkedIssues() ){
def object=ComponentAccessor.getIssueManager().getIssueObject(link)
if(object.getIssueType().toString() != 'Initiative'){
throw new InvalidInputException(IssueFieldConstants.ISSUE_LINKS,
"Note: You must link this Deployment Request to a Initiative.")
}
}
}

 

Thanks for any help 

Richard

Richard Duffy February 26, 2019

I have also tried this:

for ( link in issueLinkingValue.getLinkedIssues() ){
def object=ComponentAccessor.getIssueManager().getIssueObject(link)
if(object.getIssueType() != "10008"){
throw new InvalidInputException(IssueFieldConstants.ISSUE_LINKS,
"Note: You must link this Deployment Request to a Initiative.")
0 votes
Vasiliy Zverev
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 12, 2016

It is better to use IssueLinkiManager to have deal with links. Here is my code for this validotor:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.link.IssueLink

Issue issue = ComponentAccessor.getIssueManager().getIssueObject("")

for(IssueLink link: ComponentAccessor.getIssueLinkManager().getInwardLinks(issue.getId())){
    if(! link.getIssueLinkType().getName().equals("causes"))
        return false;
    if(!link.getDestinationObject().getIssueTypeId().equals("10001"))
        return false

    return true;
}

Note, that there are inward and outward links, and source and destination objects for links. Make it clear and you will never have troubles with linked issues.

JamieA
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 12, 2016

The problem is you cannot use this code on a validator (where the issue links is a form field), because, unhelpfully, the links are not available through IssueLInkManager until after the entire transition is complete. So you have to use the form params.

Like Azfar Masut likes this
Vasiliy Zverev
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 13, 2016

I see. Thank for comment, I have never faced with this case.

David Lynott August 11, 2018

Jamie,

This seems to work for creating issues via the UI. How does one adapt this code to handle creating the issue via REST?

Thanks

TAGS
AUG Leaders

Atlassian Community Events