How can I get the Issue in scope when using custom Post Function?

Robby Jennings May 22, 2014

I've created the script below to transition related tickets. I tested it using Script Runner and by adding an issue as a string in .getIssueObject().

def issue = constantsManager.getIssueManager().getIssueObject('AM-1234')

How can I get/pass the current issue that is transitioning into the scope of the Post Function? New to Groovy/JAVA and JIRA. :)

// Transition script to transition an inWardLinked issue status when related link is approved 
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.link.IssueLink
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.workflow.WorkflowTransitionUtil
import com.atlassian.jira.workflow.WorkflowTransitionUtilImpl
import com.atlassian.jira.util.JiraUtils

def constantsManager =  ComponentManager.getInstance()
WorkflowTransitionUtil workflowTransitionUtil = ( WorkflowTransitionUtil)JiraUtils.loadComponent( WorkflowTransitionUtilImpl.class )

def issue = constantsManager.getIssueManager().getIssueObject('AM-1234')
def issueLinkManager = constantsManager.getIssueLinkManager()
def relatedLinks = issueLinkManager.getInwardLinks(issue.id)

relatedLinks.each {issueLink ->
def linkedIssue = issueLink.getSourceObject()
// If related linktype is not in ABTEST project and it's status is "in design" transition status to approved
if (issueLink.issueLinkType.name == "Related" && linkedIssue.getProjectObject().getKey() != "ABTEST" && linkedIssue.getStatusObject().getName() == "In Design"){
        workflowTransitionUtil.setIssue(linkedIssue)
        workflowTransitionUtil.setAction(421) // design approved - see text view of workflow to get transition ids
        workflowTransitionUtil.progress()
}}

1 answer

0 votes
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.
May 22, 2014

It's in the script binding, which means you just refer to it without declaring it. So just comment out your line 15 and it should work.

Robby Jennings May 22, 2014

I removed line 15 and line 17 started to fail. So I then tested in Script Runner with line 17 set t0

def relatedLinks = issueLinkManager.getInwardLinks()

This run without error in Script Runner but did nothing when I ran the transistion Post Function. Any ideas?

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.
May 22, 2014

That doesn't make any sense... the line you mention above is not valid. Can you add some logging...

Robby Jennings May 28, 2014

Sorry for slow reply.. got torn away for a while. Error log is.

[onresolve.jira.groovy.GroovyCondition] Script condition failed on DWORK-2092: issueLinkManager.getInwardLinks(issue.getId())*.issueLinkType.name.contains('Related')
java.lang.Exception: Cannot find a script engine for extension: "contains('Related')", (make sure it's on the classpath).

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.
May 30, 2014

Sounds like you are pasted this into the text box that is for putting in a path to a file. Can you attach a screenshot.

Robby Jennings June 3, 2014

This error occurs when I transition the issue to the approved state and the post function runs. This isn't from running the Script Runner add-on. Is that what you were thinking?

Trevor Trevor June 3, 2014

This error occurs when I transition the issue to the approved state and the post function runs. This isn't from running the Script Runner add-on. Is that what you were thinking?

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.
June 3, 2014

Yes, it is from the script runner plugin... can you upload a screenshot of the config page for the workflow function.

Robby Jennings June 3, 2014

This is what I see when I run the script in the text box. http://screencast.com/t/jfx04kya2

I get the same error when running from file.

http://screencast.com/t/szBcIAjhaR2

These are different errors to what I get when I transition an issue.

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.
June 3, 2014

But it needs to be set up as a post-function. The admin script console is just a REPL for testing or running one-time scripts.

You need to edit a workflow, add a post-function, script function, then put the path to the file there, then publish the workflow, then test by putting an issue through that transition.

Robby Jennings June 3, 2014

The original error is from when I add it as a post-function and tested by transitioning an issue through the transition.

Suggest an answer

Log in or Sign up to answer