It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

set Component in groovy script

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

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

I guess it's alright :)

Thank you!

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.

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.

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

Unfortunately, no..

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

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

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

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!

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)

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

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

Hi @Matthew_Gaffney 

Thanks,this works for me.

Like Matthew_Gaffney likes this

Suggest an answer

Log in or Sign up to answer
TAGS

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you