Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

How to delete user comment

Oleksii Okhmush
Contributor
January 11, 2019 edited

I try to find and delete all comments from specific user in project, using scriptrunner:

import org.ofbiz.core.entity.GenericValue;
import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.ComponentManager;
import com.atlassian.jira.issue.Issue;
import com.atlassian.jira.issue.IssueManager;
import com.atlassian.jira.project.Project;
import com.atlassian.jira.project.ProjectManager;
import com.atlassian.jira.issue.MutableIssue;
import com.atlassian.jira.issue.comments.Comment;
import com.atlassian.jira.issue.comments.CommentManager

import java.awt.List;
ProjectManager projectManager = ComponentAccessor.projectManager
Project proj= projectManager.getProjectByCurrentKey("CW")
CommentManager commentManager = ComponentAccessor.commentManager
IssueManager issueManager = ComponentAccessor.issueManager
for (GenericValue issueValue: issueManager.getProjectIssues(proj.getGenericValue())){
String id = 'CW-' + issueValue.number

//Issue issue = issueManager.getIssueObject(issueValue)
MutableIssue missue = issueManager.getIssueObject(id)
List<Comment> comments = commentManager.getComments(missue) as List
comments.each { Comment comment ->
if (comment.getAuthorApplicationUser() == '222') {
commentManager.delete(comment)
}
}
}

 

What i do wrong? 

1 answer

1 accepted

3 votes
Answer accepted
Ivan Tovbin
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.
January 13, 2019 edited

Here's how you do it:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.sal.api.user.UserManager

Project project = ComponentAccessor.getProjectManager().getProjectByCurrentKey("ABC")
IssueManager issueManager = ComponentAccessor.getIssueManager()
CommentManager commentManager = ComponentAccessor.getCommentManager()

Collection<Issue> issues = issueManager.getIssueIdsForProject(project.getId()).collect{issueManager.getIssueObject(it)}

issues.each{issue ->
List<Comment> userComments = commentManager.getComments(issue).findAll{comment ->
comment.getAuthorApplicationUser() == getUser("username")
}
if(userComments){
userComments.each{comment ->
commentManager.delete(comment, false, getUser("username2"))
}
}
}

ApplicationUser getUser(String userName){
return ComponentAccessor.getUserManager().getUserByName(userName)
}

 

Oleksiy Ohmush January 13, 2019

yes, this is exactly what i am looking for!

 

What actually do username2? 

And something wrong with syntax 

 if(userComments){comment ->
Oleksii Okhmush
Contributor
January 14, 2019

Thanks!

Suggest an answer

Log in or Sign up to answer
TAGS
atlassian, mindful member, mindful member badge, atlassian community, community kudos, community giveaway, atlassian swag, community values, empathy and kindness, badge challenge, atlassian learning, community engagement, swag giveaway

Earn a Badge, Win a Prize 🎁

Earning the Mindful Member badge proves you know how to lead with kindness, plus it enters you into a giveaway for exclusive Atlassian swag. Take the quiz, grab the badge, and comment on our announcement article to spread the good vibes!

Start here
AUG Leaders

Atlassian Community Events