Hello guys,
I try to clone an issue and remove a specific label from it (named CCB). Following code is working fine (this is just a snippet as in the original code I will really create the cloned issue)
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.label.LabelManager
import com.atlassian.jira.issue.label.Label
// get Issue
def issueManager = ComponentAccessor.getIssueManager()
def issue = issueManager.getIssueObject("SWCM-46")
// Clone Issue
def issueFactory = ComponentAccessor.getIssueFactory()
def newIssue = issueFactory.cloneIssue(issue)
// get Labels from Issue
LabelManager labelManager = ComponentAccessor.getComponent(LabelManager)
List <String> labels = labelManager.getLabels(issue.id).collect{ it.getLabel() }
labels -= 'CCB'
newIssue.setLabels(labels.toSet())
return null
Now my problem concerns static type checking:
As I can see labels is a collection and is converted to a Set by calling toSet() - so everything should fine as the argument of method setLabels() must be a Set according to documentation: http://docs.groovy-lang.org/2.4.15/html/groovy-jdk/java/util/Collection.html#toSet()
So why is type checking failing here?
Kind Regards
Frank
I believe this is a quirk of the scriptrunner linter. It treats any object declared as a def as a standard object (of class java.lang.object). When you declare newIssue on this line
def newIssue = issueFactory.cloneIssue(issue)
the linter always treats newIssue as a generic object, regardless of the actual type (which is why the script works despite this- from a programming perspective your code is correct, its just a problem with the console). If you want the warning to go away, you should just be able to declare it as an IssueImpl (since issueImpl implements the MutableIssue interface), you just need to add this import:
import com.atlassian.jira.issue.IssueImpl
Personally, this bug (amongst other problems with the console) is why I don't write my scripts in jira itself- I use a 3rd party text editor (you can use any you want, but my preference is Sublime Text) to actually write the scripts, and then just copy and paste them into jira when I'm ready to test them.
Hi Ben
Thanks for your answer I definitely will try out Sublime Text :-)
I tried using IssueImpl:
import com.atlassian.jira.issue.IssueImpl
IssueImpl newIssue = issueFactory.cloneIssue(issue)
This leads to warning:
Meanwhile I got rid of the warning by using as operator for argument of setLabels(...):
newIssue.setLabels(labels.toSet() as Set)
Kind Regards
Frank
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Not sure if you still have the first, warning, but if you do, you should be able to fix it by casting the cloned issue to issueimpl
IssueImpl newIssue = (IssueImpl)issueFactory.cloneIssue(issue)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
That helps for getting rid of warning about mismatch between MutableIssue and IssueImp but the first warning still appears...
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.