listener to copy target version/s field to fix version/s field

Jeffrey Cocozziello July 28, 2021

Hello all,

I am trying to create a listener script that copies the target version/s field to the fix version/s field whenever the issue is updated. I would be even better if I could do this whenever the field itself is updated, but I'm not sure what event to use for this.

The script I am currently using is:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor

def issue = event.issue as Issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def tgtField = customFieldManager.getCustomFieldObjects(event.issue).find {it.name == "fixVersions"}
def calcField = customFieldManager.getCustomFieldObjectsByName("targetVersions")
def changeHolder = new DefaultIssueChangeHolder()
tgtField.updateValue(null, issue, new ModifiedValue(issue.getCustomFieldValue(tgtField), issue.getCustomFieldValue(calcField)),changeHolder)

I am using jira server version 8.13.3. I have tried using both 'Fix Version\\s' and 'fixVersions' names for both target and fix versions. I am also getting an error using the getCustomFieldValue command (but only for calcField for some reason). Any help would be appreciated!

Thanks,

Jeff

3 answers

1 accepted

0 votes
Answer accepted
Jeffrey Cocozziello July 29, 2021

Figured it out. In case anyone is curious... I was doing two things wrong.

1) I should have used setFixVersions() rather than setFixVersion().

2) I had to update the issue after the changes are made.

Here is the final script:

 

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.project.version.Version
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.event.type.EventDispatchOption

def issue = event.issue as Issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def targetVersionsField = customFieldManager.getCustomFieldObject(11206)
def changeHolder = new DefaultIssueChangeHolder()
def targetVersionsList = issue.getCustomFieldValue(targetVersionsField)
def issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

log.warn(targetVersionsField)
log.warn(targetVersionsList)
log.warn(targetVersionsList.getClass())
issue.setFixVersions(targetVersionsList)
log.warn(issue.getFixVersions())
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

0 votes
Leo
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
July 29, 2021

Hi @Jeffrey Cocozziello,

What is the type of target version/s custom field? 

 

Regards,

Leo

Jeffrey Cocozziello July 29, 2021

Hi Leo,

If you look at the accepted answer I recently added you will see that I found a solution, but to answer your question the custom field type was "Version Picker (multiple versions)".

Jeff

0 votes
Jeffrey Cocozziello July 28, 2021

Making a bit more progress. I learned that Fix Version/s is not a custom field and that the issue object has a way of configuring them. My latest iteration is:

import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.ModifiedValue
import com.atlassian.jira.issue.util.DefaultIssueChangeHolder
import com.atlassian.jira.component.ComponentAccessor

def issue = event.issue as Issue
def customFieldManager = ComponentAccessor.getCustomFieldManager()
def targetVersionsField = customFieldManager.getCustomFieldObject(11206)
def changeHolder = new DefaultIssueChangeHolder()
log.warn(targetVersionsField)
log.warn(issue.getCustomFieldValue(targetVersionsField))
def targetVersionsList = issue.getCustomFieldValue(targetVersionsField)
issue.setFixVersion(targetVersionsList)

 

Log:

2021-07-28 11:26:37,530 WARN [runner.ScriptBindingsManager]: Target Version/s 2021-07-28 11:26:37,535 WARN [runner.ScriptBindingsManager]: [XIQ Q2r2.2 (21.02.22.xx), XIQ Q2r3 (21.02.30.xx)] 2021-07-28 11:26:37,539 ERROR [runner.AbstractScriptListener]: ************************************************************************************* 2021-07-28 11:26:37,539 ERROR [runner.AbstractScriptListener]: Script function failed on event: com.atlassian.jira.event.issue.IssueEvent, file: null groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.issue.IssueImpl.setFixVersion() is applicable for argument types: (ArrayList) values: [[XIQ Q2r2.2 (21.02.22.xx), XIQ Q2r3 (21.02.30.xx)]] Possible solutions: setFixVersions(java.util.Collection), getFixVersions() at Script174.run(Script174.groovy:13)

I think I need to typecast the array returned by the custom field to get this to work...

Suggest an answer

Log in or Sign up to answer