JIRA Script Runner Append version to Fix Version/s

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

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

Perfect, exactly what I was looking for.

Roy

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published Jul 10, 2018 in Marketplace Apps

If you’re an Atlassian app developer, you’ll want to know about Atlas Camp!

This September 6-7, hundreds of Atlassian App developers will flock to Barcelona Spain to build skills, discover product roadmaps, meet face-to-face with the Atlassian team, and learn how to extend t...

176 views 0 4
Read article

Atlassian User Groups

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

Find a group

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

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you