Set Component in groovy script

Hello,

I want to set a component value in a post-function of the creation of an issue.

Therefore, I created a groovy script in the post-function of the create issue that set a specific value to the issue.

If this specific value is not exists in the project component list, the script will create it first and then will set this component to the issue.

 

Here is my script:

import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.user.util.DefaultUserManager
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.project.Project
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.util.UserMessageUtil

import org.apache.log4j.Logger
import org.apache.log4j.Category
def Category log = Category.getInstance("com.onresolve.jira.groovy.Condition")
log.setLevel(org.apache.log4j.Level.DEBUG)


MutableIssue myIssue = issue
Project project = myIssue.getProjectObject()

def theComponent = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(),'PSW')
//log.info("The Component is: " + theComponent)

if (theComponent == null) {
theComponent = ComponentAccessor.getProjectComponentManager().create("PSW","PSW component","",0,project.getId())
//log.info("The Component not exists")
//log.info(theComponent)
}


myIssue.setComponent([theComponent])

 

Something is not working here. The component is not set to the new issue, any idea why?

2 answers

1 accepted

1 vote
Alexey Matveev Community Champion Dec 14, 2017

import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.user.util.DefaultUserManager
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.project.Project
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.util.UserMessageUtil

import org.apache.log4j.Logger
import org.apache.log4j.Category
def Category log = Category.getInstance("com.onresolve.jira.groovy.Condition")
log.setLevel(org.apache.log4j.Level.DEBUG)


MutableIssue myIssue = issue
Project project = myIssue.getProjectObject()

def theComponent = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(),'PSW')
//log.info("The Component is: " + theComponent)

if (theComponent == null) {
theComponent = ComponentAccessor.getProjectComponentManager().create("PSW","PSW component","",0,project.getId())
//log.info("The Component not exists")
//log.info(theComponent)
}


myIssue.setComponent([theComponent])

myIssue.store()

It's still not working...

 

Any other idea?

Alexey Matveev Community Champion Dec 14, 2017

Try this script. It works for me. Do not forget that only Project Administrator can create a component if the component is absent

import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.util.ImportUtils
import com.atlassian.jira.user.util.DefaultUserManager
import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.project.Project
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
import com.atlassian.jira.event.type.EventDispatchOption

import org.apache.log4j.Logger
import org.apache.log4j.Category
def Category log = Category.getInstance("com.onresolve.jira.groovy.Condition")
log.setLevel(org.apache.log4j.Level.DEBUG)

 

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser();
MutableIssue myIssue = issue
Project project = myIssue.getProjectObject()

def theComponent = ComponentAccessor.getProjectComponentManager().findByComponentName(project.getId(),'PSW')
//log.info("The Component is: " + theComponent)

if (theComponent == null) {
theComponent = ComponentAccessor.getProjectComponentManager().create("PSW","PSW component","",0,project.getId())
//log.info("The Component not exists")
//log.info(theComponent)
}


myIssue.setComponent([theComponent])
ComponentAccessor.getIssueManager().updateIssue(user,myIssue,EventDispatchOption.ISSUE_UPDATED,true)

Thank you so much!

Now it's working perfect!

0 vote

Is the post functin on creation? if so add issue.store()

And your post function must be before reindexing post function

Thank you.

Where should I add it? (the issue.store())

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Thursday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

78 views 0 5
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