Versions in JIRA Alphabetical Sort Script Not Working

Matthew Frassetti May 17, 2017

Hello,

Previously, I found this script to use in scriptrunner written by another user to sort versions in a project alphabetically. However, when I plug the script into my script console, it returns an error. 

The Script:

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.project.version.VersionManager;
import com.atlassian.jira.project.version.Version;
import com.atlassian.jira.project.ProjectManager;
import com.atlassian.jira.project.Project;
 
VersionManager versionManager = ComponentManager.getInstance().getVersionManager();
ProjectManager projectManager = ComponentManager.getInstance().getProjectManager();


Project project = projectManager.getProjectByCurrentKey("MYPROJECT");
List<Version> versions = versionManager.getVersions(project);


if (versions != null && versions.size() > 0) {
    Collections.sort(versions, new Comparator<Version>(){
      public int compare(Version obj1, Version obj2) {
        if (obj1 == null) {
            return -1;
        }
        if (obj2 == null) {
            return 1;
        }
        if (obj1.getName() == obj2.getName()) {
            return 0;
        }
        return obj2.getName().compareTo(obj1.getName());
      }
    });
    
    versionManager.moveToStartVersionSequence(versions[0]);
    for(int i=1;i<versions.size(); i++) {
        versionManager.moveVersionAfter(versionManager.getVersion(versions[i].getId()),versions[i-1].getId());
    }
}
return versions;

The Error:

org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 15: expecting ')', found ';' @ line 15, column 26.
   if (versions != null && versions.size() > 0) {
                            ^

1 error

	at com.adaptavist.sr.cloud.workflow.AbstractScript.parseScript(AbstractScript.groovy:40)
	at com.adaptavist.sr.cloud.workflow.AbstractScript.evaluate(AbstractScript.groovy:28)
	at com.adaptavist.sr.cloud.workflow.AbstractScript$evaluate$1.callCurrent(Unknown Source)
	at com.adaptavist.sr.cloud.events.ScriptExecution.run(ScriptExecution.groovy:26)
	at ScriptExecution1_groovyProxy.run(Unknown Source)

I've been trying to learn the scripting language so I could fix this myself, but I am in a bit of a time crunch and need assistance as soon as possible. Any and all help is appreciated. The original post can be found here.

3 answers

1 accepted

1 vote
Answer accepted
Aidan Derossett [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.
May 19, 2017

The "&" in the script is HTML for "Start of a character reference." So when you see the "&" followed by some letters and a ";" this is a reference to a character. For example, "&" is the reference for an ampersand (&). However, the compiler is not recognizing these statements as character references and is failing to compile. An easy fix for this would be to simply go through the script and replace each of the character references with the characters that they are supposed to represent.

Character conversion:

  • &lt; ---> < (less than)
  • &gt; ---> > (greater than)
  • $amp; ---> & (ampersand)
Matthew Frassetti May 19, 2017

Hey Aidan,

 

This seemed to fix the first error, but now I am getting an "unable to resolve class" error.

import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.project.version.VersionManager;
import com.atlassian.jira.project.version.Version;
import com.atlassian.jira.project.ProjectManager;
import com.atlassian.jira.project.Project;
 
VersionManager versionManager = ComponentManager.getInstance().getVersionManager();
ProjectManager projectManager = ComponentManager.getInstance().getProjectManager();


Project project = projectManager.getProjectByCurrentKey("MY PROJECT");
List<Version> versions = versionManager.getVersions(project);


if (versions != null && versions.size() > 0) {
    Collections.sort(versions, new Comparator<Version>(){
      public int compare(Version obj1, Version obj2) {
        if (obj1 == null) {
            return -1;
        }
        if (obj2 == null) {
            return 1;
        }
        if (obj1.getName() == obj2.getName()) {
            return 0;
        }
        return obj2.getName().compareTo(obj1.getName());
      }
    });
    
    versionManager.moveToStartVersionSequence(versions[0]);
    for(int i=1;i<versions.size(); i++) {
        versionManager.moveVersionAfter(versionManager.getVersion(versions[i].getId()),versions[i-1].getId());
    }
}
return versions;

Here are the errors:

 

rg.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script1.groovy: 5: unable to resolve class com.atlassian.jira.project.Project
 @ line 5, column 1.
   import com.atlassian.jira.project.Project;
   ^

Script1.groovy: 3: unable to resolve class com.atlassian.jira.project.version.Version
 @ line 3, column 1.
   import com.atlassian.jira.project.version.Version;
   ^

Script1.groovy: 1: unable to resolve class com.atlassian.jira.ComponentManager
 @ line 1, column 1.
   import com.atlassian.jira.ComponentManager;
   ^

Script1.groovy: 2: unable to resolve class com.atlassian.jira.project.version.VersionManager
 @ line 2, column 1.
   import com.atlassian.jira.project.version.VersionManager;
   ^

Script1.groovy: 4: unable to resolve class com.atlassian.jira.project.ProjectManager
 @ line 4, column 1.
   import com.atlassian.jira.project.ProjectManager;
   ^

Script1.groovy: 16: unable to resolve class com.atlassian.jira.project.version.Version 
 @ line 16, column 47.
   sort(versions, new Comparator<Version>()
                                 ^

6 errors

	at com.adaptavist.sr.cloud.workflow.AbstractScript.parseScript(AbstractScript.groovy:40)
	at com.adaptavist.sr.cloud.workflow.AbstractScript.evaluate(AbstractScript.groovy:28)
	at com.adaptavist.sr.cloud.workflow.AbstractScript$evaluate$1.callCurrent(Unknown Source)
	at com.adaptavist.sr.cloud.events.ScriptExecution.run(ScriptExecution.groovy:26)
	at ScriptExecution1_groovyProxy.run(Unknown Source)

 

Aidan Derossett [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.
May 22, 2017

Hey again,

I've put the script into my own environment and everything resolves as it should. Where are you trying to store this script? Are you using it directly in JIRA or is it in a script root or some other folder? Other than that, I noticed that the script also includes the depricated class:

import com.atlassian.jira.ComponentManager;

Which will give you some trouble down the road. You'll need to switch over to the new class, ComponentAccessor, and alter your code accordingly. 

We have some documentation that goes over class changes in newer versions of JIRA here.

Matthew Frassetti May 23, 2017

Thank you for the documentation. I will bookmark it and review today.

I'm running the script directly in the Script Console. I believe my prior error was due to me attempting this in a Cloud instance, because it is giving me a different error in our Server instance, though it could be just a version issue as well. This is what I see now.

Screen Shot 2017-05-23 at 10.43.32 AM.png

It's returning this error below: 

 

No signature of method: com.atlassian.jira.ComponentManager.getVersionManager() is applicable for argument types: () values: []
Aidan Derossett [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.
May 24, 2017

This is definentally an issue due to API changes. You need to switch the ComponentManager that you are using over to the ComponentAccessor and use the appropriate methods. You can find the documentation for ComponentAccessor here.

Try making these changes:

  • Add the import statement: 
    import com.atlassian.jira.component.ComponentAccessor
  • And change those two stataments to:
    VersionManager versionManager = ComponentAccessor.getVersionManager(); 
    ProjectManager projectManager = ComponentAccessor.getProjectManager();
Avinash December 7, 2017

Can you post the final code @Aidan Derossett [Adaptavist] ?

Aidan Derossett [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.
December 21, 2017

Hey @Avinash!

I don't actually have the full code that @Matthew Frassetti may have ended up with. But this is the code with all of the suggestions that I made to fix the issues:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.project.version.Version
import com.atlassian.jira.project.Project

def versionManager = ComponentAccessor.versionManager
def projectManager = ComponentAccessor.projectManager


Project project = projectManager.getProjectByCurrentKey("MY PROJECT")
List<Version> versions = versionManager.getVersions(project)


if (versions != null && versions.size() > 0) {
Collections.sort(versions, new Comparator<Version>(){
public int compare(Version obj1, Version obj2) {
if (obj1 == null) {
return -1
}
if (obj2 == null) {
return 1
}
if (obj1.getName() == obj2.getName()) {
return 0
}
return obj2.getName().compareTo(obj1.getName())
}
});

versionManager.moveToStartVersionSequence(versions[0])
for(int i=1;i<versions.size(); i++) {
versionManager.moveVersionAfter(versionManager.getVersion(versions[i].getId()),versions[i-1].getId())
}
}
return versions

 

Avinash January 29, 2018
Aidan Derossett [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.
January 30, 2018

No problemo @Avinash!

I hope it works for you! :D 

0 votes
Andy Ukasick April 15, 2020

The code provided by @Aidan Derossett [Adaptavist]  was 99% right.  It Just needed a slight tweak to make it work.  It was attempting to sort a "RegularImmutableList" which is something that can't be modified (i.e. sorted).  So I had to turn the list into an array that can be sorted.

Here is the working code:

 

import com.atlassian.jira.component.ComponentAccessor 
import com.atlassian.jira.project.version.Version
import com.atlassian.jira.project.Project

def versionManager = ComponentAccessor.versionManager
def projectManager = ComponentAccessor.projectManager


Project project = projectManager.getProjectByCurrentKey("MYPROJECTKEY")
List<Version> versions = versionManager.getVersions(project)
List<Version> modversions = new ArrayList<Version>(versions)

if (versions != null && modversions.size() > 0) {
Collections.sort(modversions, new Comparator<Version>(){
public int compare(Version obj1, Version obj2) {
if (obj1 == null) {
return -1
}
if (obj2 == null) {
return 1
}
if (obj1.getName() == obj2.getName()) {
return 0
}
return obj2.getName().compareTo(obj1.getName())
}
});

versionManager.moveToStartVersionSequence(modversions[0])
for(int i=1;i<modversions.size(); i++) {
versionManager.moveVersionAfter(versionManager.getVersion(modversions[i].getId()),modversions[i-1].getId())
}
}

return modversions 

 

Of course be sure to swap in the KEY of your Jira Project in which you want to sort the versions.

0 votes
Silvia Gal March 12, 2018

I'm getting this error:

groovy.lang.GroovyRuntimeException: Ambiguous method overloading for method com.atlassian.jira.project.version.DefaultVersionManager#getVersions. Cannot resolve which method to invoke for [null] due to overlapping prototypes between: [interface java.util.List] [interface com.atlassian.jira.project.Project] at Script277.run(Script277.groovy:10)

 

Could you help me on this?

Suggest an answer

Log in or Sign up to answer