Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in

How to set a component based on a custom select list?

Derek Sheeman June 5, 2015

I have been trying to come up with a way to set a component on an Issue based on the setting of a single select list at the time of issue creation.  I am very new to groovy code and have gotten to where my script executes without errors, but it does not actually set the component.  I am hoping that someone can give me a hand to identify what I'm doing wrong.

I am doing this to help with routing incoming issues into a support intake project to particular component leads.

 

import com.atlassian.jira.bc.project.component.ProjectComponent
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.fields.CustomField
import com.atlassian.jira.project.Project
def projectComponentManager = ComponentAccessor.getProjectComponentManager()
MutableIssue issue = issue
def cfSrcId = 12800
CustomFieldManager cManager = ComponentAccessor.getCustomFieldManager()
CustomField srcField = cManager.getCustomFieldObject(cfSrcId)
def cfrt = issue.getCustomFieldValue(srcField)?.value
Project project = issue.getProjectObject()
ProjectComponent component = null
if(cfrt == "Analytics"){
    component = projectComponentManager.findByComponentName(project.getId(), "Analytics")
}
else if(cfrt == "LMM/Volta"){
    component = projectComponentManager.findByComponentName(project.getId(),"LMM")
}
else if(cfrt == "Openstack"){
    component = projectComponentManager.findByComponentName(project.getId(),"Openstack")
}
else if(cfrt == "Account Management"){
    component = projectComponentManager.findByComponentName(project.getId(),"Account Management")
}
else if(cfrt == "App Support"){
    component = projectComponentManager.findByComponentName(project.getId(),"App Support")
}
else if(cfrt == "Capacity"){
    component = projectComponentManager.findByComponentName(project.getId(),"Capacity")
}
else if(cfrt == "OBS"){
    component = projectComponentManager.findByComponentName(project.getId(),"OBS")
}
else if(cfrt == "SDN"){
    component = projectComponentManager.findByComponentName(project.getId(),"SDN")
}
else if(cfrt == "SRE"){
    component = projectComponentManager.findByComponentName(project.getId(),"SRE")
}
else if(cfrt == "Underlay Network"){
    component = projectComponentManager.findByComponentName(project.getId(),"Underlay Network")
}
if (component) {
    issue.setComponentObjects([component])
}

2 answers

1 accepted

Comments for this post are closed

Community moderators have prevented the ability to post new answers.

Post a new question

0 votes
Answer accepted
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 8, 2015

You may as well use 

issue.setComponentObjects ([component])

rather than the outdated genericValue method. 

For a select list you want:

def cfrt = issue.getCustomFieldValue(srcField)?.value

 

Derek Sheeman June 8, 2015

Thank you for the tip Jamie, I've modified the script with the two recommendations, changing the component.getGenericValue lines and the variable definition. No errors in the logs still, but the components aren't being touched. Is there a problem because I am doing this in the Create transition? (Code above modded to match)

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 8, 2015

Which position is the function in the create transition? Definitely no errors in atlassian-jira.log?

Derek Sheeman June 8, 2015

Step 2, right after "Create issue originally" No error when I chose Analytics (first if). I see I've got a different syntax for the else if's and those do error. "" file: /data/home/projira/scripts/cloudfire_component.groovy groovy.lang.MissingPropertyException: No such property: ComponentAccessor for class: cloudfire_component at cloudfire_component.run(cloudfire_component.groovy:25) "" Side effect of my hacking code together as a newbie I expect. I'm either doing something wrong in the Analytics "if" or I've missed an import statement somewhere, possibly both.

Derek Sheeman June 8, 2015

blatant log error resolved (was missing "import com.atlassian.jira.component.ComponentAccessor") no more errors using both variations of getting the component, still not setting it though, heh.

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 8, 2015

I've modified it a bit to remove some of the redundancy: https://bitbucket.org/snippets/Adaptavist/Mbdpo I would suggest you change it so that it simply finds a known component and sets that. If that works you can move on to the select list stuff.

Derek Sheeman June 8, 2015

That certainly is more crisp than my patchwork attempt (THANK YOU). As a test, I chopped the entire if/else serice (everything below Project Component component = null) and replaced it with: component = projectComponentManager.findByComponentName(project.getId(), "LMM") issue.setComponentObjects([component]) It's still not setting a component on create, I verified no one had removed the component and that the logging is quiet. Is it possible that this isn't functioning due to being in the create transition post-functions?

0 votes
Derek Sheeman July 8, 2015

The code shown above is not functioning, both in the create post-function and in transition post functions.

I am uncertain what the problems were before, we may have had an index problem which was complicating testing.

Thank you @Jamie Echlin [Adaptavist] for all of the insights you provided.

TAGS
AUG Leaders

Atlassian Community Events