It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

ScriptRunner to transition linked issue

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

> 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...

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)")
    }
}

cool, looks proper...

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?

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()

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).

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

Hi,

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

can some one help me in this topic please!!

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)

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

Community showcase
Posted in Jira Core

How to manage many similar workflows?

I have multiple projects that use variations of the same base workflow. The variations depend on the requirements of the project or issue type. The variations mostly come in the form of new statuses ...

1,085 views 7 0
Join discussion

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you