Exceptions trying to delete issues via Groovy

Jens Kisters __SeibertSolutions
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.
July 3, 2019

Hello,

i had a piece of code that worked just fine when deleteing subtasks.

Now i am trying to delete standard issues and i can't get it to work.

I have tried two ways of deleting them:

 

package sandbox

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.web.bean.PagerFilter

IssueManager issueManager = ComponentAccessor.getIssueManager()
Issue invoiceIssue = issueManager.getIssueByKeyIgnoreCase("DBH-439")

ApplicationUser runAsUser = ComponentAccessor.getUserManager().getUserByKey("admin")

def getIssueListByJql = { String jql ->
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
SearchService.ParseResult parseResult = searchService.parseQuery(runAsUser, jql);
if (parseResult.isValid()) {
SearchResults searchResult = searchService.search(runAsUser, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
return searchResult.getIssues();
} else {
throw new Exception("invalid jql:" + jql)
}
}

Iterator iterator = getIssueListByJql("summary ~ \"|" + invoiceIssue.getKey() + "|\" AND project = PAGE AND issueType = Seite").iterator()

Boolean workaround = true
while (iterator.hasNext()) {
Issue issue = (Issue) iterator.next()
log.error "To DEL " +issue.getKey() + "," + runAsUser + "," + issue + "," + EventDispatchOption.ISSUE_DELETED + "," + issueManager
Issue issueToDelete = issue
if (workaround) {
issueToDelete = issueManager.getIssueByKeyIgnoreCase(issue.getKey())
}
issueManager.deleteIssue(runAsUser, issueToDelete, EventDispatchOption.DO_NOT_DISPATCH, false)
log.error "DELETED " +issue.getKey()
}

When i run this one i get

groovy.lang.GroovyRuntimeException: Ambiguous method overloading for method com.atlassian.jira.issue.managers.RequestCachingIssueManager#deleteIssue. Cannot resolve which method to invoke for [class com.atlassian.jira.user.DelegatingApplicationUser, null, class com.atlassian.jira.event.type.EventDispatchOption$EventDispatchOptionImpl, class java.lang.Boolean] due to overlapping prototypes between: [interface com.atlassian.jira.user.ApplicationUser, interface com.atlassian.jira.issue.Issue, interface com.atlassian.jira.event.type.EventDispatchOption, boolean] [interface com.atlassian.jira.user.ApplicationUser, interface com.atlassian.jira.issue.MutableIssue, interface com.atlassian.jira.event.type.EventDispatchOption, boolean] at sandbox.Script157.run(Script157.groovy:38)

 

When i set workaound to false i get 

 java.lang.UnsupportedOperationException: Cannot remove an ImmutableGenericValue at com.atlassian.jira.util.ofbiz.ImmutableGenericValue.remove(ImmutableGenericValue.java:36) at com.atlassian.jira.issue.managers.DefaultIssueDeleteHelper.deleteIssue(DefaultIssueDeleteHelper.java:143) at com.atlassian.jira.issue.managers.DefaultIssueManager.deleteIssue(DefaultIssueManager.java:717) at com.atlassian.jira.issue.managers.RequestCachingIssueManager.deleteIssue(RequestCachingIssueManager.java:225) at com.atlassian.jira.issue.IssueManager$deleteIssue$4.call(Unknown Source) at sandbox.Script159.run(Script159.groovy:38)

 

Any ideas?

 

Please help me Atlassian Community, you are my only hope

3 answers

1 accepted

0 votes
Answer accepted
Ilya Turov
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.
July 3, 2019

I tried your code and it worked fine for me with workaround turned on, with workaround off I got same error you got (but I guess it's expected)

But for your first exception, it seems like it can't find issueToDelete. Try executing this:

issueManager.deleteIssue(runAsUser, null, EventDispatchOption.DO_NOT_DISPATCH, false)

and you'll see same error.

My best guesses would be:

1. log exactly issueToDelete to see if it's not null

2. reindex project, maybe that's why you are getting null issue objects

Jens Kisters __SeibertSolutions
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.
July 3, 2019

Hi Ilya, 

thanks for the fast answer,

i seem to have some "broken" issues in my issue index,

they still appeared in the issue search after deleting them via the jira UI until reindexing

 

kind regards

Jens

Jens Kisters __SeibertSolutions
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.
July 3, 2019

It seems to be an issue that surfaces from time to time, 

in about one of ten tries i am generating these broken issues appear. Strange...

0 votes
Renee Lyons June 7, 2021

Due to further changes in JIRA's available methods, I updated the script to the below:

import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.web.bean.PagerFilter

IssueManager issueManager = ComponentAccessor.getIssueManager()
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

def getIssueListByJql = { String jql ->
SearchService searchService = ComponentAccessor.getComponent(SearchService.class)
SearchService.ParseResult parseResult = searchService.parseQuery(user, jql);

if (parseResult.isValid()) {
SearchResults searchResult = searchService.search(user, parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
return searchResult.getResults();
} else {
throw new Exception("invalid jql:" + jql)
}
}

Iterator iterator = getIssueListByJql("<your JQL>").iterator()

Boolean workaround = true
while (iterator.hasNext()) {
Issue issue = (Issue) iterator.next()
log.error "To DEL " +issue.getKey() + "," + user + "," + issue + "," + EventDispatchOption.ISSUE_DELETED + "," + issueManager
Issue issueToDelete = issue
if (workaround) {
issueToDelete = issueManager.getIssueByKeyIgnoreCase(issue.getKey())
}
issueManager.deleteIssue(user, issueToDelete, EventDispatchOption.DO_NOT_DISPATCH, false)
log.error "DELETED " +issue.getKey()
}

0 votes
Jens Kisters __SeibertSolutions
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.
July 3, 2019

At one point i got another even more weird exception, but i can't reproduce that now:

2019-07-03 10:57:04,778 http-nio-127.0.0.1-8080-exec-85 ERROR admin 657x15478x1 mjhtgh 80.147.223.246,127.0.0.1 /rest/scriptrunner/latest/user/exec/ [c.o.scriptrunner.runner.ScriptRunnerImpl] To DEL PAGE-20,admin(admin),DocumentIssueImpl[issueKey=PAGE-20],com.atlassian.jira.event.type.EventDispatchOption$EventDispatchOptionImpl@652f8345,com.atlassian.jira.issue.managers.RequestCachingIssueManager@54a6a389
at postFunctionDeleteInvoicePages.run(postFunctionDeleteInvoicePages.groovy:27)
at com.atlassian.jira.issue.IssueManager$deleteIssue$4.call(Unknown Source)
at com.atlassian.jira.issue.managers.RequestCachingIssueManager.deleteIssue(RequestCachingIssueManager.java:225)
at com.atlassian.jira.issue.managers.DefaultIssueManager.deleteIssue(DefaultIssueManager.java:717)
at com.atlassian.jira.issue.managers.DefaultIssueDeleteHelper.deleteIssue(DefaultIssueDeleteHelper.java:120)
at com.atlassian.jira.issue.managers.DefaultIssueDeleteHelper$DeletedIssueEventData.<init>(DefaultIssueDeleteHelper.java:274)
java.lang.NullPointerException

Suggest an answer

Log in or Sign up to answer