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
@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)
}
Thank you very much @Muhammad Ramzan_Atlassian Certified Master_ !
I have this error, maybe I need an import to solve it?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
these are static errors, you can ignore them
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Muhammad Ramzan_Atlassian Certified Master_ good job, I learned something new :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi @Dan27 is it failing on row "issueService.delete(userDelete, validationResult)"?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I put there 10s (10000ms). Is there any message in the log file?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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()
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
}
}
}
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Now I can't create the issue at all, I got this error inside the create screen:
and 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 "
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
I would suggest to go with listener , instead of post function.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
hi @Muhammad Ramzan_Atlassian Certified Master_ , I guess @Dan27 mentioned he is trying to do it as part of the listener :)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
@Martin Bayer _MoroSystems_ s_r_o__ @Muhammad Ramzan_Atlassian Certified Master_ yes, I already tried with listener without success
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Please check my new answer , i updated your script
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.