Delete issue right after it was created

Dan27
Contributor
November 30, 2020

Hello,

 

I want to delete an issue that it's summary starts with 'XXX' right after it was created.

In the workflow (create transition) I have some other actions , and at the end of the workflow I put a custom script that supposed to delete the issue- it is not working.

I tried to put the same script in script console and it worked.

I tried to create a listener with the same script (issue created event) and it didn't work.

I got this exception:

2020-11-30 08:36:30,832 ERROR [properties.PropertiesUtil]: Exception whilst trying to find value for property 'jira.diagnostics.thresholds.slow-query-millis'. Defaulting to 5000. java.lang.NumberFormatException: For input string: "5000 "

 

How can I delete this issue ?

This is my script:

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor

def issue = ComponentAccessor.getIssueManager().getIssueObject("QAA-760");

//Delete issue if starts with XXX
if(issue.getSummary().toString().startsWith("XXX"))
{
log.warn "inside loop"
log.warn issue.getSummary().toString();
IssueService issueService = ComponentAccessor.getIssueService()
def userManager = ComponentAccessor.getUserManager()
def userDelete = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()


log.warn("Delete issue $issue with user $userDelete")
if (issue) {
def validationResult = issueService.validateDelete(userDelete, issue.id)
if (validationResult.errorCollection.hasAnyErrors()) {
log.warn("Can't delete issue")
}
else {
issueService.delete(userDelete, validationResult)
}
}
}

 

 

 

Thank you,

Daniel

3 answers

2 accepted

0 votes
Answer accepted
Muhammad Ramzan_Atlassian Certified Master_
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.
November 30, 2020

@Dan27 , What is happening here, when you crating the issue, on same time you are deleting, on backend the issue is deleted.

You need to take care

1- Delete the issue in new thread

2- Execute the script with a specific user which you confirmed have delete rights

3- Creating a new thread in Jira is tricky.

 

I have updated your script , hopefully it will work for you.

 

package com.techclimbs.scriptrunner.CommonGroovyCode

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.issue.IssueEvent
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.util.thread.JiraThreadLocalUtil
import groovy.transform.Field
import org.apache.log4j.Logger


//def issue = ComponentAccessor.getIssueManager().getIssueObject("EMC-151");
def issueEvent = event as IssueEvent
def issue=issueEvent.issue
logs(issueEvent.eventTypeId)
//Delete issue if starts with XXX
def jtlu
try {
if (issue.getSummary().toString().startsWith("test")) {
Thread.start {
jtlu = ComponentAccessor.getComponent(JiraThreadLocalUtil)
jtlu.preCall()
deleteIssue(issue)
}
}
}catch(Exception e){
logs("Error occurred while deleting the issue")

}
finally {
jtlu.postCall(Logger.getLogger("com.onresolve.jira.groovy"))

}
def deleteIssue(issue) {
Thread.sleep(5000)
logs(issue.getSummary().toString())
IssueService issueService = ComponentAccessor.getIssueService()
def userManager = ComponentAccessor.getUserManager()
ApplicationUser userDelete = ComponentAccessor.getUserManager().getUserByName("seeramzan")
logs("Delete issue $issue with user $userDelete")
def validationResult = issueService.validateDelete(userDelete, issue.id)
if (validationResult.isValid()) {
def results=issueService.delete(userDelete, validationResult)
logs("issue delete results: ${results.errorMessages}")

} else {

logs("Unable to delete issue because ${validationResult.errorCollection}")
}


}


def logs(logs) {
def lg = Logger.getLogger("com.onresolve.jira.groovy")
lg.debug(logs)
}
Dan27
Contributor
November 30, 2020

Thank you very much @Muhammad Ramzan_Atlassian Certified Master_ !

I have this error, maybe I need an import to solve it?

error.png

Muhammad Ramzan_Atlassian Certified Master_
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.
November 30, 2020

these are static errors, you can ignore them

Dan27
Contributor
November 30, 2020

Thank you @Muhammad Ramzan_Atlassian Certified Master_ ,

The issue deleted, but I got a lot of errors in the log file: 

Cannot invoke method postCall() on null object

 

How can I solve those errors? I can't leave it like this..

Thanks,

Daniel 

Muhammad Ramzan_Atlassian Certified Master_
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.
November 30, 2020

Please take some time to look into the script, I just created quickly for you, you might need to do some cleanup ,

 

Regarding the  above error post call is invoking the logger, it means the logger string is not there.

 

What other errors you facing

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
December 1, 2020

@Muhammad Ramzan_Atlassian Certified Master_ good job, I learned something new :)

0 votes
Answer accepted
Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 30, 2020

Hi @Dan27 is it failing on row "issueService.delete(userDelete, validationResult)"?

Dan27
Contributor
November 30, 2020

Yes I think.. Because it didn't print the log "Can't delete issue"

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 30, 2020

ok, I think it can be some "data synchronization" problem. Please try to adjust script to be ran in new Thread. You have to put your code:

Thread executorThread = new Thread(new Runnable() {
void run() {
Thread.sleep(10000)
// PUT YOUR CODE HERE
}
})
executorThread.start()

It is not the nicest code, but let's just check if synchronization is a problem and we can solve it nicely later.

I have also have to suggest you to not to delete issue but for example move it to the status "Automation done" and "hide" issues in this status for regular users. 

Dan27
Contributor
November 30, 2020

Hi @Martin Bayer _MoroSystems_ s_r_o__  thanks for your answer,

 

The issue created but not deleted...

How much time it supposed to wait before running the script?

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 30, 2020

I put there 10s (10000ms). Is there any message in the log file?

Dan27
Contributor
November 30, 2020

No errors or exceptions

 

This is my script eith your updates:

 

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessordef issue = ComponentAccessor.getIssueManager().getIssueByCurrentKey(event.getIssue().getKey());

Thread executorThread = new Thread(new Runnable() {
void run() {
Thread.sleep(10000)


if(issue.getSummary().toString().startsWith("XXX"))
{
log.warn "inside loop"
log.warn issue.getSummary().toString();
IssueService issueService = ComponentAccessor.getIssueService()
def userManager = ComponentAccessor.getUserManager()
def userDelete = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()


log.warn("Delete issue $issue with user $userDelete")
if (issue) {
def validationResult = issueService.validateDelete(userDelete, issue.id)
if (validationResult.errorCollection.hasAnyErrors()) {
log.warn("Can't delete issue")
}
else {
issueService.delete(userDelete, validationResult)
}
}
}



}
})
executorThread.start()

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 30, 2020

Ok it might be that it is not logged from the new Thread. Let's try to simplify it

import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.component.ComponentAccessor

Thread.sleep(10000)

def issue = ComponentAccessor.getIssueManager().getIssueObject("QAA-760");

//Delete issue if starts with XXX
if(issue.getSummary().toString().startsWith("XXX"))
{
log.warn "inside loop"
log.warn issue.getSummary().toString();
IssueService issueService = ComponentAccessor.getIssueService()
def userManager = ComponentAccessor.getUserManager()
def userDelete = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()


log.warn("Delete issue $issue with user $userDelete")
if (issue) {
def validationResult = issueService.validateDelete(userDelete, issue.id)
if (validationResult.errorCollection.hasAnyErrors()) {
log.warn("Can't delete issue")
}
else {
issueService.delete(userDelete, validationResult)
}
}
}
Dan27
Contributor
November 30, 2020

Now I can't create the issue at all, I got this error inside the create screen:

errorCreateScreen.JPGand inside the log file I got this exception:

Exception whilst trying to find value for property 'jira.diagnostics.thresholds.slow-query-millis'. Defaulting to 5000. java.lang.NumberFormatException: For input string: "5000 "
Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 30, 2020

Oh :D trial and error... I'm thinking about it... you are trying to delete issue on which you are still working on (listener is executed).

Is there a chance that issue will exist for some time in required status and you will implement service which will be ran on regular basis (at night probably)?
https://scriptrunner.adaptavist.com/4.3.0/jira/services.html

Dan27
Contributor
November 30, 2020

it is an interesting idea, I will try it.

Thank you!

0 votes
Muhammad Ramzan_Atlassian Certified Master_
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.
November 30, 2020

I would suggest to go with listener , instead of post function.

Martin Bayer _MoroSystems_ s_r_o__
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
November 30, 2020

hi @Muhammad Ramzan_Atlassian Certified Master_ , I guess @Dan27 mentioned he is trying to do it as part of the listener :)

Dan27
Contributor
November 30, 2020

@Martin Bayer _MoroSystems_ s_r_o__ @Muhammad Ramzan_Atlassian Certified Master_  yes, I already tried with listener without success

Muhammad Ramzan_Atlassian Certified Master_
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.
November 30, 2020

Please check my new answer , i updated your script

Suggest an answer

Log in or Sign up to answer