Snippets

Adaptavist Remove Comments With Content Script

Created by Jon Mort last modified
// based on https://gist.github.com/lhotari/caafb4d394412f491861
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.comments.Comment
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.issue.search.SearchException
import com.atlassian.jira.bc.issue.search.SearchService
import com.atlassian.jira.web.bean.PagerFilter
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.search.SearchResults
import com.atlassian.jira.user.ApplicationUsers
import com.atlassian.crowd.embedded.api.User

def deleteCommentsMatching(Issue issue, Closure commentFilter) {
    CommentManager commentManager = ComponentAccessor.commentManager
    List<Comment> comments = commentManager.getComments(issue)
    def matchingComments = comments.findAll(commentFilter)
    matchingComments.each { comment ->
       commentManager.delete(comment)
    }
    matchingComments
}

def searchIssues(String jqlQuery, Closure issueResultsHandler) {
    SearchService searchService = ComponentAccessor.getComponent(SearchService)
    User user = ApplicationUsers.toDirectoryUser(ComponentAccessor.jiraAuthenticationContext.user)

    SearchService.ParseResult parseResult =
            searchService.parseQuery(user, jqlQuery)
    if (parseResult.isValid())
    {
        try
        {
            SearchResults results = searchService.search(user,
                    parseResult.getQuery(), PagerFilter.getUnlimitedFilter())
            return issueResultsHandler(results)
        }
        catch (SearchException e)
        {
            return "Error running search " + e
        }
    }
    else
    {
        return "Error parsing jqlQuery: " + parseResult.getErrors()
    }
}

def commentContent = REPLACE_THIS_WITH_COMMENT_CONTENT
String jqlQuery = REPLACE_THIS_WITH_JQL_QUERY_FOR_ISSUES

searchIssues(jqlQuery) { SearchResults results ->
    def issues = results.issues

    def deleteCount = 0

    issues.each { issue ->
        def deleted = deleteCommentsMatching(issue) { comment ->
            comment.body.equals(commentContent)
        }
        deleteCount += deleted.size()
    }

    return "deleted ${deleteCount} comments from ${issues.size()} issues"
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.