ScriptRunner to transition linked issue

Andrew Culver
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.
November 15, 2016

I'm trying to write a scriptrunner post function script to transition a linked issue in another project. I've used pieces from the Auto Close Subtasks and All Depending Issues Resolved examples to come up with my code.

When the post function runs, the user sees an error "Property 'destIssue' not found" in the transition screen and can't proceed. Had a look at the API docs and I seem to be doing everything correctly.

This is my first groovy script, so I'm sure I've done something wrong, but don't know where.

I was also wondering about the ScriptRunner's example code use of directly accessing values such as "issue.id" and "it.statusObject.name" instead of using accessor methods to get these values. Is this proper or just old styling?

Running JIRA 6.3.13.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink
def issueService = ComponentAccessor.getIssueService()
def linkType = ["escalated from"]
def user = ComponentAccessor.getJiraAuthenticationContext().getUser()
def linkMgr = ComponentAccessor.getIssueLinkManager()
for (IssueLink link in linkMgr.getOutwardLinks(issue.id)) {
    if (linkType.contains(link.issueLinkType.name)) {
        def destIssue = link.getDestinationObject()
        def destStatusObject = destIssue.getStatusObject()
        if (destStatusObject.name == "Installing") {
            def issueInputParameters = issueService.newIssueInputParameters()
            issueInputParameters.with {
                setComment("Camera has been installed. Signs are needed before camera can be activated.")
                setSkipScreenCheck(true)
            }
            def validationResult = issueService.validateTransition(user, destIssue.id, 41, issueInputParameters)
            if (validationResult.isValid()) {
                def issueResult = issueService.transition(user, validationResult)
                if (! issueResult.isValid()) {
                    log.warn("Failed to transition task ${destIssue.key}, errors: ${issueResult.errorCollection}")
                }
            } else {
                log.warn("Could not transition task ${destIssue.key}, errors: ${validationResult.errorCollection}")
            }
        }
    }
}

4 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.
November 15, 2016

> ScriptRunner's example code use of directly accessing values such as "issue.id" and "it.statusObject.name" instead of using accessor methods to get these values

It's proper, it's the groovy way... I did a quick google and found this which sums it up quite nicely:

http://www.javaworld.com/article/2073053/groovy-hip-tip--to-use-or-not-to-use-setters-and-getters.html

The problem you are having is an old-ish issue with inline scripts, and the fact that the JIRA tries to expand strings like $destIssue before it passes the parameters to the workflow functions.

You could upgrade and fix it, except we don't support 6.3 anymore. So the easiest option is to move the inline script to a file, and that should fix it. Other than that your script looks ok...

Andrew Culver
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.
November 15, 2016

Thanks Jamie. This took care of the error. We will be upgrading JIRA sometime in December/January.

For anyone else looking for how to transition a linked issue, here's my current script (JIRA 6.3.13):

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink
def issueService = ComponentAccessor.getIssueService()
def linkType = ["Escalated"]
// 6.x validateTransition wants a User, so we have to use getDirectoryUser()
// 7.x validateTransition wants an ApplicationUser, so remove the .getDirectoryUser() after we upgrade
def user = ComponentAccessor.getJiraAuthenticationContext().getUser().getDirectoryUser()
def linkMgr = ComponentAccessor.getIssueLinkManager()
// Look through the outward links
for (IssueLink link in linkMgr.getOutwardLinks(issue.id)) {
    def destIssue = link.getDestinationObject()
    // Does the name of the link match "Escalated" ?
    if (linkType.contains(link.issueLinkType.name)) {
        def destStatusObject = destIssue.getStatusObject()
	// Is the status of the linked issue "Installing" ?
        if (destStatusObject.name == "Installing") {
            // Prepare our input for the transition
            def issueInputParameters = issueService.newIssueInputParameters()
            issueInputParameters.with {
                setComment("Camera has been installed. Signs are needed before camera can be activated.")
                setSkipScreenCheck(true)
            }
            // Validate transitioning the linked issue to "Signs Needed"
            def validationResult = issueService.validateTransition(user, destIssue.id, 41, issueInputParameters)
            if (validationResult.isValid()) {
                // Perform the transition
                def issueResult = issueService.transition(user, validationResult)
                if (! issueResult.isValid()) {
                    log.warn("Failed to transition task ${destIssue.key}, errors: ${issueResult.errorCollection}")
                }
            } else {
                log.warn("Could not transition task ${destIssue.key}, errors: ${validationResult.errorCollection}")
            }
        } else {
            log.warn("Skipping link: ${link.issueLinkType.name} ${destIssue.key} ${destStatusObject.name} (wrong status)")
        }
    } else {
        log.warn("Skipping link: ${link.issueLinkType.name} ${destIssue.key} (wrong type)")
    }
}
Like Ann Kristin likes this
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.
November 17, 2016

cool, looks proper...

Andrew Culver
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.
November 18, 2016

One more question about this script. Is it possible for the transition on the linked issue to be performed by a different user. The logged in user may not have permission to transition the linked issue, but I have a user "jirabot" which has permission to run any transition in any project. Could I specify a different user in the issueService.validateTransition call? How would I do this?

Andrew Culver
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.
November 18, 2016

Found my own answer after some trial and error. Changed this line:

//def user = ComponentAccessor.getJiraAuthenticationContext().getUser().getDirectoryUser()
def user = ComponentAccessor.getUserManager().getUserByName("jirabot").getDirectoryUser()
Andrew Culver
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.
November 18, 2016

I found that I also needed to remove:

setSkipScreenCheck(true)

Otherwise the linked issue would get the comment twice (once from the logged in user and once from the jirabot user).

JörgS February 14, 2017

Is it possible to transition the linked issue also in a remote JIRA instance (Applink is configured)?

0 votes
Karim Belhadj October 27, 2018

Hello i did the same code in a post function , an error always appear , please can somone help me !!!

My code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.link.IssueLink
def issueService = ComponentAccessor.getIssueService()
def linkType = ["Cloners"]
// 6.x validateTransition wants a User, so we have to use getDirectoryUser()
// 7.x validateTransition wants an ApplicationUser, so remove the .getDirectoryUser() after we upgrade
def user = ComponentAccessor.getJiraAuthenticationContext().getUser().getDirectoryUser()
def linkMgr = ComponentAccessor.getIssueLinkManager()
// Look through the outward links

for (IssueLink link in linkMgr.getOutwardLinks(issue.id)) {
def destIssue = link.getDestinationObject()
// Does the name of the link match "Cloner" ?

if (linkType.contains(link.issueLinkType.name)) {

def destStatusObject = destIssue.getStatusObject()
// Is the status of the linked issue "En attente niveau 2" ?
if (destStatusObject.name == "En attente niveau 2") {
// Prepare our input for the transition

def issueInputParameters = issueService.newIssueInputParameters()
issueInputParameters.with {
setComment("Camera has been installed. Signs are needed before camera can be activated.")
setSkipScreenCheck(true)
}
// return destStatusObject.name
//return destIssue.id
// return user
// Validate transitioning the linked issue to "Signs Needed"
def validationResult = issueService.validateTransition(user, destIssue.id, 31, issueInputParameters)
return validationResult
if (validationResult.isValid()) {
// Perform the transition

def issueResult = issueService.transition(user, validationResult)
if (! issueResult.isValid()) {

log.warn("Failed to transition task ${destIssue.key}, errors: ${issueResult.errorCollection}")
}
} else {
log.warn("Could not transition task ${destIssue.key}, errors: ${validationResult.errorCollection}")
}
} else {
log.warn("Skipping link: ${link.issueLinkType.name} ${destIssue.key} ${destStatusObject.name} (wrong status)")
}
} else {
log.warn("Skipping link: ${link.issueLinkType.name} ${destIssue.key} (wrong type)")
}
}

 

 

 

 

 

 

 

error:

V2018-10-27 20:13:12,919 ERROR [runner.ScriptFieldPreviewRunner]: ************************************************************************************* 2018-10-27 20:13:12,919 ERROR [runner.ScriptFieldPreviewRunner]: Script field preview failed for field that has not yet been created groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.bc.issue.DefaultIssueService.validateTransition() is applicable for argument types: (com.atlassian.jira.user.BridgedDirectoryUser, java.lang.Long, java.lang.Integer, com.atlassian.jira.issue.IssueInputParametersImpl) values: [kimo:1, 10459, 31, com.atlassian.jira.issue.IssueInputParametersImpl@1eed919] Possible solutions: validateTransition(com.atlassian.jira.user.ApplicationUser, java.lang.Long, int, com.atlassian.jira.issue.IssueInputParameters), validateTransition(com.atlassian.jira.user.ApplicationUser, java.lang.Long, int, com.atlassian.jira.issue.IssueInputParameters, com.atlassian.jira.workflow.TransitionOptions) at Script1273.run(Script1273.groovy:31)

0 votes
Karim Belhadj October 27, 2018

can some one help me in this topic please!!

0 votes
Sergey Karaev _Aplana_ June 27, 2017

Hi,

I'm also wondering if this script will work for remote issue ( remote JIRA instance)

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

TAGS
AUG Leaders

Atlassian Community Events