It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

JIRA Script Runner Append version to Fix Version/s

Roy Chapman Jun 02, 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] Jun 05, 2017 • edited

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 Jun 13, 2017

Perfect, exactly what I was looking for.

Roy

Suggest an answer

Log in or Sign up to answer
This widget could not be displayed.
This widget could not be displayed.
Community showcase
Published in Marketplace Apps & Integrations

Join our webinar: “Asset Management: The Role of Context in ITSM” with Atlassian & Riada

  Hi Atlassian Community! This is Teresa from the Jira Service Desk team. On June 19th, I’ll be hosting a webinar “Asset Management: The Role of Context in ITSM” with Atlassian Partne...

152 views 2 3
Read article

Community Events

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

Find an event

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

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you