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.
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:
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
It's returning this error below:
No signature of method: com.atlassian.jira.ComponentManager.getVersionManager() is applicable for argument types: () values: []
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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:
import com.atlassian.jira.component.ComponentAccessor
VersionManager versionManager = ComponentAccessor.getVersionManager(); ProjectManager projectManager = ComponentAccessor.getProjectManager();
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
No problemo @Avinash!
I hope it works for you! :D
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Online forums and learning are now in one easy-to-use experience.
By continuing, you accept the updated Community Terms of Use and acknowledge the Privacy Policy. Your public name, photo, and achievements may be publicly visible and available in search engines.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.