Changing the issue type with groovy

dy raffy
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.
February 3, 2013

Hi,

I would like to change the issue type from "Bug" to "support" with a groovyscript when a user clicks on a transition "Start Progress". How can I get "this Issue" in a Postfunction? and which objects do I need?

P.S.: both issue types use the same workflow.

Tanks for your help,

Raffy

4 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

4 votes
Answer accepted
Jozef Kotlár
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.
February 4, 2013

As stated in comments - you should be sure that issue types share field configuration and at the least workflow states. For details look at implementation of com.atlassian.jira.web.action.issue.MoveIssue* actions.

If you search for scriptrunner examples, you would find out that issue is accessible as 'issue', so

import com.atlassian.jira.component.ComponentAccessor

def newIssueType = ComponentAccessor.issueTypeSchemeManager.getIssueTypesForProject(issue.projectObject).find{it.name=="support"}
if (newIssueType) issue.setIssueTypeObject(newIssueType)

 

UPDATE 2015-07-15: corrected code as suggested below by @Joe Smith

dy raffy
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.
February 4, 2013

Thanks Jozef,

I am sure that the workflow and the field configuration scheme are the same.

dy raffy
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.
February 4, 2013

Hi Jozef,

your solution is pretty much coller than my solution:

import org.apache.log4j.Category
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.issuetype.IssueType
import com.atlassian.jira.issue.IssueImpl

/* Define a Logger */
def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")
log.setLevel(org.apache.log4j.Level.DEBUG)

def constantsManager = ComponentAccessor.getConstantsManager()
Issue issue = issue

log.debug ("### Other method ###")
def newIssueType = ComponentAccessor.issueTypeSchemeManager.getIssueTypesForProject(issue.projectObject).find{it.name=="Task"}
log.debug newIssueType.name
if (newIssueType) issue.setIssueTypeObject(newIssueType)

log.debug issue.name
log.debug("#############################  IssueType changed from 'Bug' to 'Task' ########################################")

But Unfortunately it did not work and I get javax.script.ScriptException (s) in the log file:

dy raffy
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.
February 4, 2013
Here are the errors:
2013-02-05 11:21:58,859 http-bio-8080-exec-15 ERROR admin 681x857x1 1w8c413 10.255.255.6 /secure/WorkflowUIDispatcher.jspa [onresolve.jira.groovy.GroovyRunner] The script failed : javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: name for class: com.atlassian.jira.issue.IssueImpl 2013-02-05 11:21:58,859 http-bio-8080-exec-15 ERROR admin 681x857x1 1w8c413 10.255.255.6 /secure/WorkflowUIDispatcher.jspa [onresolve.jira.groovy.GroovyFunctionPlugin] Error executing post-function javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: name for class: com.atlassian.jira.issue.IssueImpl at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:138)

nevertheless thank you very much for your Help.

Jozef Kotlár
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.
February 4, 2013

your error is in line log.debug issue.name

should be log.debug issue.issueType.name

dy raffy
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.
February 4, 2013

Now it works. Tanks a lot.

Joe Smith July 14, 2015

jozef kotlár thanks for your example, but there it is actually slightly wrong... if (newIssueType) issue.setIssueObject(newIssueType) should be if (newIssueType) issue.setIssue*Type*Object(newIssueType) as in @dy raffy's example. Got me on the right track though. Thanks both

2 votes
dy raffy
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.
February 4, 2013

The solution I was locking for is:

import org.apache.log4j.Category
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.issuetype.IssueType


/* Define a Logger */
def Category log = Category.getInstance("com.onresolve.jira.groovy.PostFunction")
log.setLevel(org.apache.log4j.Level.DEBUG)

def constantsManager = ComponentAccessor.getConstantsManager()

Issue issue = issue  // This what I was looking for :-). As simple as it is, but I didn't know that.

IssueType targetIssueType = null

log.debug "IssueType old = " + issue.issueType.name

collection = constantsManager.getAllIssueTypeObjects()
iterator = collection.iterator()
while(iterator.hasNext()){
	issueType = iterator.next()
	if(issueType.name == "Support"){
		targetIssueType = issueType
	}
}

log.debug targetIssueType.name
issue.setIssueTypeObject(targetIssueType)
log.debug "IssueType new = " + issue.issueType.name

Why should this be dangerous? It's used deliberately.

Abyakta Lenka December 11, 2018

How can this be used for moving to another project ? 

0 votes
dy raffy
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.
February 4, 2013

Yes, the Field Configuration Scheme is also the same for both issue types.

0 votes
Renjith Pillai
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.
February 4, 2013

Looks a bit dangerous. Btw, it is not just workflow, it is also the Field Configuration Scheme that should be same for both issue types.

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