Versions in JIRA Alphabetical Sort Script Not Working

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.

2 answers

1 accepted

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)

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)

 

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.

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: []

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();

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

 

No problemo @Avinash!

I hope it works for you! :D 

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
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 Jun 14, 2018 in Jira Service Desk

How the Telegram Integration for Jira helps Sergey's team take their support efficiency to the bank

...+ reading Fantasy). The same is true for him at the bank he works for: Efficiency is key when time literally equals money. Read on to learn how Sergey makes most of the time he has by...

503 views 3 5
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