Update comment author

Hello,

I am trying to update comments for users that have been migrated from the internal Jira user database to a directory database. The code snippet I am using currently in a proof of concept is a groovy script with scriptrunner:

                    comments.each { comment ->
                        MutableComment mutableComment = commentManager.getMutableComment(comment.getId())
                        CommentService.CommentParameters commentParameters = new CommentService.CommentParameters.CommentParametersBuilder().author(userId).body(comment.getBody()).build()
                        CommentService.CommentUpdateValidationResult commentUpdateValidationResult = commentService.validateCommentUpdate(currentUser,mutableComment.getId(),commentParameters)
                        if (commentUpdateValidationResult.getWarningCollection().hasAnyWarnings()) {
                            log.debug "Comment Warnings: " + commentUpdateValidationResult.getWarningCollection().hasAnyWarnings()
                        }
                        if (commentUpdateValidationResult.getErrorCollection().hasAnyErrors()) {
                            log.debug "Comment Errors: " + commentUpdateValidationResult.getErrorCollection().hasAnyErrors()
                        }
                        if (commentUpdateValidationResult.isValid()) {
                            log.debug "Updating user comment ${comment.getId()} for issue ${comment.getIssue()} for user ${user.getKey()} to ${userId.getName()}"
                            commentService.update(currentUser, commentUpdateValidationResult, false)

The script compiles and runs without issues. It reports that the comment(s) are properly updated. But they are not. There is no error message or indication what might be wrong. Do you see any issue with the approach? Is there a working way?

Thank you and best regards,

Nico

1 Like

The only way I have found in groovy

import com.atlassian.jira.component.ComponentAccessor;
import com.atlassian.jira.issue.comments.CommentManager;

userUtil = ComponentAccessor.getUserUtil();
CM = ComponentAccessor.getComponent(CommentManager.class);

oldUser = userUtil.getUserByName("oldUser") 
newUser = userUtil.getUserByName("newUser")

// make a loop for issue
cmnts = CM.getCommentsForUser(issue, oldUser)

cmnts.each {
  CM.create(issue,newUser,newUser,it.getBody(),it.getGroupLevel(),it.getRoleLevelId(),it.getCreated(),it.getUpdated(),false)
  CM.delete(it);
}

update the Author does not work

1 Like

Thank you @yann,

I tried the outlined way on Jira 7.13.2 and it did not work either. I will now leave this issue as it is and argue that the revision safety of the system is responsible for that. :wink:

Best regards,
Nico

1 Like

Here is a working version for future visitors of this page:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.comments.CommentManager
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.user.util.UserManager

String issueKey = "JIRA-ISSUEKEY"
IssueManager issueManager = ComponentAccessor.getIssueManager()
UserManager userManager = ComponentAccessor.getUserManager()
Issue issue = issueManager.getIssueObject(issueKey)
CommentManager commentManager = ComponentAccessor.getCommentManager()

ApplicationUser oldUser = userManager.getUserByName("oldUserName") 
ApplicationUser newUser = userManager.getUserByName("newUserName")

// make a loop for issue
def cmnts = commentManager.getComments(issue)

cmnts.each {
  	log.error(it.getAuthorApplicationUser() == oldUser)
    if(it.getAuthorApplicationUser() == oldUser){
        log.error("Changing User from ${it.getAuthorApplicationUser()} to ${newUser}")
        commentManager.create(issue,newUser,newUser,it.getBody(),it.getGroupLevel(),it.getRoleLevelId(),it.getCreated(),it.getUpdated(),false)
  		commentManager.delete(it)
    }
}