How to make script append value to components instead of replacing it?

arama mihai
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 28, 2018

Hello,

We have the following script listener set up for one of our projects:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption

// a map with select list option to Component
def componentsMap = [
"DOORS": ["DOORS"],
"Rhapsody / Design Manager": ["Rhapsody"],
"CS - Rational Change": ["Change (CS)"],
"CM - Rational Synergy": ["Synergy (CM)"],
"JIRA": ["JIRA"],
"Git/Gerrit": ["Gerrit", "Git"],
"Collaborator": ["Collaborator"],
"Klocwork": ["Klocwork"],
"Bullseye / RTRT": ["Bullseye"],
"VectorCAST": ["VectorCAST"],
"Temppo": ["Temppo"],
"Valgrind": ["Valgrind"],
"Ubuntu": ["LX Client"],
"Confluence": ["Confluence"],
"Environment": ["Tools Front Desk"],
"Other Tools": ["Tools Front Desk"],
"Continuous Integration / Delivery": ["Continuous Integration"],
"Data External Exchanges": ["CS External Exchange", "JIRA Data Exchange"],
"Reporting Tools": ["PR Ramp Down"],
"Other Developed Tools": ["_DevOps"]

]

def issue = issue as MutableIssue

def selectList = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Tool/Area")
def selectListValue = issue.getCustomFieldValue(selectList) as LazyLoadedOption

// there is no value for the select list - therefore do nothing
if (!selectListValue) {
return
}

def componentManager = ComponentAccessor.projectComponentManager

def components = componentsMap.get(selectListValue.value)?.collect {
componentManager.findByComponentName(issue.projectObject.id, it)
}

if (components) {
componentManager.updateIssueProjectComponents(issue, components)
}

 

To briefly explain what it does: We have a custom field, select list type, called 'Tool/Area'. This is basically a way to group several components sometimes. People should select Tool/Area, instead of Components, and when they do that , some component gets added automatically to the issue.

Unfortunately, if you also choose another component, it gets replaced.

How could we change this to append the value to existing components, instead of replacing it?

Thank you! 

1 answer

1 accepted

2 votes
Answer accepted
Alex Christensen
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
February 28, 2018

I think I've done something like this before, but I don't have the code handy. I can at least try to provide you with the logic I think I used!

  • Grab the current list of components from the current issue and store this in a new list/collection.
  • Based on the value the user input into "Tool/Area," add the appropriate set of components to be applied to the issue into this new list, so the current components and new components are added to the list.
    • You might need some logic to make sure you don't try to add the same component to the issue twice (i.e. if you already added a component, you shouldn't try to add it again). I don't know if this would cause any errors, but the thought comes to mind at least.
  • Update the components field using the new list.
Gezim Shehu [Communardo]
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
March 5, 2018

This is something similar.

Change the custom field to component

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.fields.config.FieldConfig
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.customfields.manager.OptionsManager
import com.atlassian.jira.issue.customfields.option.Option

CustomFieldManager customFieldManager = ComponentAccessor.getCustomFieldManager();

Issue issue = issue

def complianceField = customFieldManager.getCustomFieldObjectByName('Compliance Checks')
def complianceValue = issue.getCustomFieldValue(complianceField)


ArrayList <Option> selectedValues = (ArrayList<Option>) complianceValue
ArrayList <Option> newValues = new ArrayList<Option>();

if(complianceValue != null){
for(Option option : selectedValues){
if(option.getValue() != "Emergency Change"){
newValues.add(option)
}

}
issue.setCustomFieldValue(complianceField,newValues)
}
arama mihai
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.
March 12, 2018

Thanks for the proposed solutions.

The script that works for me now is:

def issue = issue as MutableIssue

def selectList = ComponentAccessor.customFieldManager.getCustomFieldObjectByName("Tool/Area")
def selectListValue = issue.getCustomFieldValue(selectList) as LazyLoadedOption

// there is no value for the select list - therefore do nothing
if (!selectListValue) {
return
}

def componentManager = ComponentAccessor.projectComponentManager

def components = componentsMap.get(selectListValue.value)?.collect {
componentManager.findByComponentName(issue.projectObject.id, it)
}

def existing_components = issue.getComponentObjects();

def newValue = existing_components + components

if (newValue) {
componentManager.updateIssueProjectComponents(issue, newValue)
}

Suggest an answer

Log in or Sign up to answer