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
Community moderators have prevented the ability to post new answers.
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'
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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'.") } } }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I'd go to the IssueLinkManager myself, as in this example:
https://scriptrunner.adaptavist.com/4.3.6/jira/recipes/scriptfields/workRemainingInLinkedIssues.html
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
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.") } } }
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.")
}
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.")
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I see. Thank for comment, I have never faced with this case.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.