Script to delete all comments

Alex Piwowar December 26, 2019

I am attempting to write a bit of code that will be used as a scriptrunner postfunction. The goal is that when an issue gets transitioned all of the comments get deleted.

 

So far I have come up with this

delete("/rest/api/2/issue/22718")
.header("Content-Type", "application/json")
.body([
fields:[
comment
]
])

 

but I get an error about the comment

CorrelationId: 894d7210-838d-4549-b5ea-cf7bbb005c07
RUN Script identifier: 9e6a3000-0bd1-4fa4-9545-d301488a8259  Took: 100ms Logs:
2019-12-26 23:20:37.553 ERROR - No such property: comment for class: Script1 on line 4
2019-12-26 23:20:37.572 ERROR - Class: com.adaptavist.sr.cloud.events.ConsoleScriptExecution, Config: [:]

 

What am I doing wrong here? Im having trouble finding information about deleting with scriptrunner.

 

Ive also looked at the atlassian documentation and the delete comment command but I dont really understand how to translate the api request into a scriptrunner script.

 

https://developer.atlassian.com/cloud/jira/platform/rest/v3/#api-rest-api-3-issue-issueIdOrKey-comment-id-delete

 

any help would be appreciated.

 

1 answer

0 votes
brbojorque
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 26, 2019

Can you include your script so we can take a look?

Alex Piwowar December 27, 2019

Hi Bryan,

This is what I was trying

 

delete("/rest/api/2/issue/22718")
.header("Content-Type", "application/json")
.body([
fields:[
comment
]
])
brbojorque
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 29, 2019

Hi @Alex Piwowar ,

Apologies for the delay, please do check the script I have attached.

Pseudo code:

  • Get all the comments in an issue
  • Loop over the comments and delete it.
def response = get('/rest/api/3/issue/TP4-1/comment').header('Content-Type', 'application/json').asObject(Map)

def comments = response.body.comments

if(comments){
comments.each{
delete(it.self.toString()).header('Content-Type', 'application/json').asObject(Map)
}
}
Like # people like this
Alex Piwowar January 8, 2020

Oh wow that worked, thanks.

 

May I ask, how do you figure all of this out?

 

In the first line what does the .header do? and the Map?

Where do you the it.self from in the loop?

brbojorque
Community Leader
Community Leader
Community Leaders are connectors, ambassadors, and mentors. On the online community, they serve as thought leaders, product experts, and moderators.
January 12, 2020

Hi @Alex Piwowar ,

I referred the ScriptRunner cloud documentation.

https://scriptrunner-docs.connect.adaptavist.com/jiracloud/

It should help you if you want to integrate additional functionality using ScriptRunner.

Suggest an answer

Log in or Sign up to answer