Forums

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

Delete a user with issue comments

Name
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
January 11, 2019

Hello!

 

I'm trying to delete all comments from specific user in project

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)
}
}
}

But have no results. Any ideas? 

Thanks for help! 

1 answer

0 votes
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

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

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){comment ->
commentManager.delete(comment, false, getUser("username2"))
}
}

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

Suggest an answer

Log in or Sign up to answer