Can I delete all comments made by particular project Role?

Hi.

I need to delete all comments made by a particular project Role in JIRA. I tried to do this via direct database accept, but would prefer a Groovy solution.

Can anybody help me with this?

1 answer

1 vote
Vasiliy Zverev Community Champion Nov 28, 2016

Here is code to del all comments for given issue which were created by users into specified role:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.security.roles.ProjectRole
import com.atlassian.jira.security.roles.ProjectRoleManager

CommentManager commentManager = ComponentAccessor.getCommentManager();

Issue issue = ComponentAccessor.getIssueManager().getIssueObject("issueKey");

ProjectRoleManager prjRoleMng = ComponentAccessor.getComponent(ProjectRoleManager);
ProjectRole role = prjRoleMng.getProjectRole("Role");

for(Comment comment: commentManager.getComments(issue)){    
    if( prjRoleMng.isUserInProjectRole(comment.getAuthorApplicationUser(), role, issue.getProjectObject())){
        commentManager.delete(comment)
    }
}

And can I sanitize every matching comment in the system? (the system would be a test JIRA instance having only the needed projects)

 

Or, maybe, I can just run a DELETE against the jiraaction table? Are there any pitfalls in doing this? (like these comments cross-referenced somewhere in the DB?)

Tarun Sapra Community Champion Nov 28, 2016

The code is for a particular issue but the question is for all the comments in the system made by a particular role.

Vasiliy Zverev Community Champion Nov 28, 2016

Here is code for all issues into all projects:

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.security.roles.ProjectRole
import com.atlassian.jira.security.roles.ProjectRoleManager

CommentManager commentManager = ComponentAccessor.getCommentManager();
IssueManager issueManager = ComponentAccessor.getIssueManager();

ProjectRoleManager prjRoleMng = ComponentAccessor.getComponent(ProjectRoleManager);
ProjectRole role = prjRoleMng.getProjectRole("Role");

for(Project project: ComponentAccessor.getProjectManager().getProjectObjects()) {
    for (Issue curIssue : issueManager.getIssueObjects(issueManager.getIssueIdsForProject(project.getId()))) {
        for (Comment comment : commentManager.getComments(curIssue)) {
            if (prjRoleMng.isUserInProjectRole(comment.getAuthorApplicationUser(), role, project)) {
                commentManager.delete(comment)
            }
        }
    }
}

Suggest an answer

Log in or Sign up to answer
How to earn badges on the Atlassian Community

How to earn badges on the Atlassian Community

Badges are a great way to show off community activity, whether you’re a newbie or a Champion.

Learn more
Community showcase
Published yesterday in Jira

5 ways you can make the most of Jira Software and Bitbucket Cloud

As part of the Bitbucket product team I'm always interested in better understanding what kind of impact the use of our tools have on the way you work. In a recent study we conducted of software devel...

53 views 0 5
Read article

Atlassian User Groups

Connect with like-minded Atlassian users at free events near you!

Find a group

Connect with like-minded Atlassian users at free events near you!

Find my local user group

Unfortunately there are no AUG chapters near you at the moment.

Start an AUG

You're one step closer to meeting fellow Atlassian users at your local meet up. Learn more about AUGs

Groups near you