JIRA Script Runner Append version to Fix Version/s

Roy Chapman June 2, 2017

My requirement is to update an issue on transition (post function) to append the next unreleased version to Fix Version/s.  I have managed to do the first part, i.e. find the next unreleased fix version.

import com.atlassian.jira.component.ComponentAccessor
import org.apache.log4j.Logger
import org.apache.log4j.Level
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.project.version.Version
import com.atlassian.jira.project.version.VersionManager
import com.atlassian.jira.event.type.EventDispatchOption

def versionManager = ComponentAccessor.getVersionManager()
def projectManager = ComponentAccessor.getProjectManager()
def project = projectManager.getProjectObjByKey(issue.projectObject.key)

def versions = versionManager.getVersions(project)

def newversions = versions.collect()
  
def log = Logger.getLogger("com.acme.CreateSubtask")
log.setLevel(Level.DEBUG)
  
newversions = newversions.sort({version1, version2 -> version1.releaseDate<=>version2.releaseDate}).findAll{version -> ! version.released }

def versionToUse = newversions.first();
log.debug("First element: " + versionToUse)

issue.setFixVersions([versionToUse])

And I can also update the Fix Version/s field.

The problem is that this code will overwrite the Fix Version/s, I want to append.  And I guess that when I append I want to check that the version I am appending doesn't already exist.

Thoughts

1 answer

1 accepted

1 vote
Answer accepted
Daniel Yelamos [Adaptavist]
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 5, 2017

Hello Roy:

You can use something like this:

/*You fetch the fixVersions from the object, 
the function returns Collection<Version>*/

def versions = issue.getFixVersions()
/*Then you check that the version that you found is not contained
in that collection*/

if(!versions.contains(versionToUse)){

/*Append your version to the collection in whichever way
you find appropiate*/. versions.add(versionToUse)

/*And then set the new array that containes the old value
and your*/ issue.setFixVersions(versions)
}

/*If it is contained, then you don't have to do anything
Because it is already there*/

 This should work correctly.

The function I use is documented here

If I misunderstood anything that you wanted to do, or if it doesn't work correcly, let me know and I'll be glad to have another look at this.

Good luck!

DYelamos

Roy Chapman June 13, 2017

Perfect, exactly what I was looking for.

Roy

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events