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?
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)
}
yes, this is exactly what i am looking for!
What actually do username2?
And something wrong with syntax
if(userComments){comment ->
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.