set Component in groovy script

Kate October 3, 2012

Hi!

I have a some problem with my groovy script. I try to set component in post-function, but it doesn't work:

import org.apache.log4j.Category

log = Category.getInstance("com.onresolve.jira.groovy.PostFunction") 


ComponentManager componentManager = ComponentManager.getInstance()
MutableIssue issue = issue
ProjectComponent component = componentManager.getProjectComponentManager().findByComponentNameCaseInSensitive("MyComponent")
issue.setComponentObjects([component.getName()])

6 answers

1 accepted

2 votes
Answer accepted
dborsboom March 26, 2013

Hey Kate, I figured out the answer about two weeks ago. Sorry I didn't reply any faster!

Here is the script:

import com.atlassian.jira.ComponentManager
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 

MutableIssue issue = issue

Project project = issue.getProjectObject()

def component = componentManager.getProjectComponentManager().findByComponentName(project.getId(),'ComponentName')

issue.setComponentObjects([component])

Kate March 27, 2013

I guess it's alright :)

Thank you!

0 votes
Chiara Squilloni December 19, 2016

Hi all,

hope anyone can help me.

I made a post funcion when a issue is created to assign a component if the issue reporter is someone specific.

It seems that everything work well but, when I open the issue, the component is unset.

Does anybody can help me?

Here is my post function:

import com.atlassian.jira.component.ComponentAccessor

import com.atlassian.jira.user.ApplicationUser

import com.atlassian.jira.ComponentManager

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

 

 

MutableIssue issue = issue

def projectComponentManager = ComponentAccessor.getProjectComponentManager()

def project = issue.getProjectObject()

 

def componentSicurezza = projectComponentManager.findByComponentName(project.getId(), "CSR Sicurezza");

def componentSistemi = projectComponentManager.findByComponentName(project.getId(), "CSR Sistemi");

def componentPuc = projectComponentManager.findByComponentName(project.getId(), "CSR PUC");

 

def reporter = issue.reporter?.name

 

if (reporter == "emailhandlerSicurezza") {

    issue.setComponentObjects([componentSicurezza])

}

if (reporter == "EmailSenderSistemi") {

    issue.setComponentObjects([componentSistemi])

}

if (reporter == "emailhandler") {

    issue.setComponentObjects([componentPuc])

}

 

issue.store()

Matthew Gaffney August 7, 2019

Did you end up solving your issue where you were trying to set different component values based on the person submitting the issue? 

 

Thank you

Like Chiara Squilloni likes this
Chiara Squilloni August 8, 2019

Hi @Matthew Gaffney 

I'm sorry, I resolved it using a post funcion based on issue type. The post funcion is set as first post funcion, befor the "Creates the issue originally" postfuncion. 

In this way the component is set before the current.issue is created. 

Here is the post funcion:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.ComponentManager
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


MutableIssue issue = issue
def projectComponentManager = ComponentAccessor.getProjectComponentManager()
def project = issue.getProjectObject()

def componentSicurezza = projectComponentManager.findByComponentName(project.getId(), "CSR Sicurezza");
def componentSistemi = projectComponentManager.findByComponentName(project.getId(), "CSR Sistemi");
def componentPuc = projectComponentManager.findByComponentName(project.getId(), "CSR Servizio Tecnico");

def issueType = issue.issueTypeObject.name

if (issueType == 'Task CSR Servizio Tecnico') {
issue.setComponentObjects([componentPuc])
}
if (issueType == 'Task CSR Sicurezza') {
issue.setComponentObjects([componentSicurezza])
}
if (issueType == 'Task CSR Sistemi') {
issue.setComponentObjects([componentSistemi])
}

You can may try set the component based on the reporter setting the post funcion after the "create issue originally".

Like Matthew Gaffney likes this
Matthew Gaffney August 8, 2019

Thanks! I was hoping to find a way to do this without having to define each component we want to set first and then make a statement saying change to this defined component, but it doesn't look like there is a way to do that. At least not in my searching. 

We'll give this a shot and see if it will work for us. The biggest issue is we have 10 different components we are setting with the core part of the script. 

 

Thanks Again!

Matthew Gaffney August 8, 2019

I'm on Jira Service Desk Server and the editor doesn't like 

issue.setComponentObjects

If I use issue.setComponent the editor doesn't get mad, but it also doesn't do anything with it.  

 

I tried putting the post-funtion (inline script) both before and after "Creates the issue originally." The script just fails when it's before "Creates the issue originally." It will at least add the placeholder comments I have if I put the inline script as the second option in the order that the post-functions occur.

 

Here is our post-function. I had tested this with comments at first since those are more accessible.:

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.jira.project.Project
import com.atlassian.jira.component.ComponentAccessor
import com.onresolve.scriptrunner.runner.util.UserMessageUtil
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.user.ApplicationUser


def commentManager = ComponentAccessor.getCommentManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
//Declaring Description field
def description = issue.description
//Declaring MutableIssue
MutableIssue issue = issue
def projectComponentManager = ComponentAccessor.getProjectComponentManager()
def project = issue.getProjectObject()
//Declaring the Department field and the value of the department field
def Department = ComponentAccessor.getCustomFieldManager().getCustomFieldObject("customfield_10501")
def DepartmentValue = (String)issue.getCustomFieldValue(Department);

//Check to see if component is already set
if (issue.getComponents().size() != 0) {
commentManager.create(
issue,
user,
"The component is already set", true)
}

//Set components for issues and emails that have component= in the body
if (description.contains("Component=")) {
commentManager.create(
issue,
user,
"The component is listed in the description", true)
}

//Sets component CHAS Tech
if (DepartmentValue == "Art") {
def CHASTech = projectComponentManager.findByComponentName(project.getId(),'CHAS Tech');
issue.setComponent ([CHASTech])
}

//Comment to help with troubleshooting how broke the previous script is/n't working
commentManager.create(
issue,
user,
"Comment that appears when the script runs.", true)

Nima Unnikrishnan November 28, 2019

Hi

Do we have any news regarding setComponentObject?

I am using jira server. Same issue here, setComponentObject is not considered as an existing method.

 

Regards,

Nima

Matthew Gaffney December 2, 2019

This worked for what we are doing:

def Tech = projectComponentManager.findByComponentName(project.getId(),"Tech");
issue.setComponent ([Tech])
    ComponentAccessor.getIssueManager().updateIssue(user,issue,EventDispatchOption.ISSUE_UPDATED,true)  
Like cgadade likes this
cgadade February 6, 2020

Hi @Matthew Gaffney 

Thanks,this works for me.

Like Matthew Gaffney likes this
0 votes
Kate March 26, 2013

Unfortunately, no..

0 votes
dborsboom February 28, 2013

Did you ever solve this? If you did I would like to know, as I'm facing the same problem at the moment.

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.
October 3, 2012

If you have that as the first post-function, I think what you have is sufficient. But as Harry says... check the logs, add some debug.

0 votes
Harry Chan
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.
October 3, 2012

What happens? Do you get any errors in the logs? Does it save? I had to call issue.store() after all the set functions to get it to save.

Also check the order of your post function. It's important.

Suggest an answer

Log in or Sign up to answer