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

2 answers

1 accepted

0 votes

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

Suggest an answer

Log in or Join to answer
Community showcase
Teodora [Botron]
Published Thursday in Marketplace Apps

Jira Inferno: The Nine Circles of Jira Administration Hell

If you spend enough time as a Jira admin - whether you are managing a single, mid-sized instance, a large enterprise one or juggling multiple instances at once - you will eventually find yourself in ...

563 views 2 15
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you
Atlassian Team Tour

Join us on the Team Tour

We're bringing product updates and pro tips on teamwork to ten cities around the world.

Save your spot