Skip to content

Instantly share code, notes, and snippets.

@lhotari
Created July 29, 2014 04:04
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save lhotari/caafb4d394412f491861 to your computer and use it in GitHub Desktop.
Save lhotari/caafb4d394412f491861 to your computer and use it in GitHub Desktop.
Jira Groovy Script for cleaning comment spam from a certain user
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 spammerUsername = 'patrickmorton'
String jqlQuery = "issueFunction in commented(\"by ${spammerUsername}\")"
searchIssues(jqlQuery) { SearchResults results ->
def issues = results.issues
def deleteCount = 0
issues.each { issue ->
def deleted = deleteCommentsMatching(issue) { comment ->
spammerUsername.equalsIgnoreCase(comment.authorApplicationUser?.username)
}
deleteCount += deleted.size()
}
return "deleted ${deleteCount} comments by ${spammerUsername} from ${issues.size()} issues"
}
@lhotari
Copy link
Author

lhotari commented Jul 30, 2014

Usage:

  • Install Script Runner Plugin to Jira.
  • Restart the Jira instance.
  • Do re-indexing in Jira admin
  • Run the script in the Script Console in "Administration -> Add-ons" section

@lhotari
Copy link
Author

lhotari commented Jul 30, 2014

Another script for deleting all remote links in a single issue:
https://gist.github.com/lhotari/7ab0a97cc677338e658d

@ehoungues
Copy link

Hello!
I never used ScriptRunner but I need to correct useless comments that have polluted my base Jira 7 May at 19:35.
This incident occurred due to mishandling in Jira.
I want to delete all comments made by me, user = ehoungues May 7, 2015 between 7:35 p.m. and 20.30, comment ="La remise est retardée"
Could you help me to write this script.
I found one of your scripts that could help me: https://gist.github.com/lhotari/caafb4d394412f491861
Thank you in advance for your help

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment