Auto Formatting comments across JIRA w/ Scriptrunner

Nick July 29, 2018

Hi,

I have a question on Functionality and/or custom code for ScriptRunner.

If I want to mass replace all comments containing

Feature: XYZ-anything-here

with

*Feature: XYZ-same-anything-here*

e.g. to simply bold that part of the comment across the whole instance of JIRA.

How can I achieve this?

3 answers

1 accepted

2 votes
Answer accepted
Mark Markov
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.
July 30, 2018

Hello @Nick

You can easily do first part with Scriprunner. But it can cause some performance problen with your jira instance depending how large it is. So i strongly reccommend you to run this script in test environment before production. Here is the script, run it in script console.

import com.atlassian.jira.component.ComponentAccessor

def projectManger = ComponentAccessor.getProjectManager()
def commentManager = ComponentAccessor.getCommentManager()
def issueManager = ComponentAccessor.getIssueManager()

def projects = projectManger.getProjects()
projects.each { project ->
log.error("Searching comments in project: ${project.getName()}")
def issues = issueManager.getIssueIdsForProject(project.id)
issues.each {issueId ->
def issue = issueManager.getIssueObject(issueId)
log.error("Checking issue: ${issue.getKey()}")
def comments = commentManager.getComments(issue)
comments.each {comment ->
if(comment.getBody().contains("Feature: XYZ-anything-here")){
log.error("Find comment to replace: ${comment.getBody()}")
def mComment = commentManager.getMutableComment(comment.id)
log.error("Replacing...")
mComment.setBody(comment.getBody().replaceAll("Feature: XYZ-anything-here", "*Feature: XYZ-same-anything-here*"))
commentManager.update(mComment, false)
}
}
}
}

 I ve added some log messages in script, so you can check how script goes and what has been changed in atlassian-jira.log

 

As for second part. You cannot change author of comment with jira API since comment created. The only possible way is to change it in DB, but this method strongly not recommended.

As workaround you can find issues with comment that contains "Author:XYZ" and create new comment with same body authored by XYZ and remove old one.

For this method you can slightly modify script above.

Nick July 30, 2018

Thank you, that's amazing!

Mark Markov
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.
July 30, 2018

You re welcome! If it helps you, please mark answer as accepted. So that, other people will be able to find this answer easily for similar questions :)

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.
July 30, 2018

Hi Nick,

This is totally doable. The question here is when exactly this should trigger? Whenever a comment is added? Also do you also want to make these changes in already existing comments?

Nick July 30, 2018

It would be primarily targeted at all existing comments.

(both in an ideal world)

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.
July 30, 2018

I see. Do you want to do this in a single project or across all projects in your jira instance? My concern is while the logic is simple enough (get all issues, then get all comments in those issues and match them against a regex, then replace the text accordingly), the sheer amount of comments to process might put a strain on your system. That you might wanna consider doing it in portions.

Nick July 30, 2018

Across whole instance/all projects of JIRA - yep

There's a lot of instances as it's how we first imported into JIRA. We can run it overnight.

0 votes
Nick July 29, 2018

Part 2 of this (and much more advanced) would be:

 

If I have text:


Author: XYZ

how could I replace the actual Comment Author with XYZ, rather than simply having it written in a comment.

 

Thanks!

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events