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

Accepted Answer
1 vote

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
Community showcase
Posted Oct 11, 2018 in Marketplace Apps

You + one app + a desert island...

Hi all! My name is Miles and I work on the Marketplace team. We’re looking for better ways to recommend and suggest apps that are truly crowd favorites, so of course we wanted to poll the Community. ...

3,675 views 6 6
Join discussion

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