Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

Groovy script on post function - set last comment author as assignee

gareth.kinsella
I'm New Here
I'm New Here
Those new to the Atlassian Community have posted less than three times. Give them a warm welcome!
October 21, 2021

Hi all,

I'm trying to create a groovy script that runs on transition as a post function to take the author of the last comment and add them as the assignee of the issue. I have scriptrunner installed and am running JIRA server. I've written a script, which when run on transition, does not update the assignee when a comment exists, but also does not post any errors. 

Any idea what I'm doing wrong? Thanks in advance!

---------

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.bc.user.search.UserSearchService
import com.atlassian.jira.bc.user.search.UserSearchParams
import com.atlassian.jira.user.ApplicationUser

// get the latest comment
def commentauthor = ComponentAccessor.commentManager.getLastComment(issue)

// check to see if comments exist and return comment author
if (commentauthor != null) {
//return comment.authorFullName
commentauthor.authorFullName
}

return null

// turn author full name into a user name
def userSearchService = ComponentAccessor.getComponent(UserSearchService.class);
UserSearchParams userSearchParams = (new UserSearchParams.Builder()).allowEmptyQuery(true).includeActive(true).includeInactive(false).maxResults(100000).build();
def commentuserID = userSearchService.findUsers(commentauthor , userSearchParams) as ApplicationUser

//-------------------
//Assign the user to the issue

import com.atlassian.jira.component.ComponentAccessor

def issueManager = ComponentAccessor.getIssueManager()
def issueService = ComponentAccessor.getIssueService()
def userManager = ComponentAccessor.getUserManager()
def validateAssignResult = issueService.validateAssign(commentuserID, issue.id, issue.reporterId)
issueService.assign(commentuserID, validateAssignResult)

 

1 answer

0 votes
Evgenii
Community Champion
October 21, 2021

You have to make issue update, after assignment.

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()

def issueManager = ComponentAccessor.getIssueManager()

issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)

p.s. your code is very complicated, it can be done easier

Try this:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.event.type.EventDispatchOption

def user = ComponentAccessor.jiraAuthenticationContext.getLoggedInUser()
def issueManager = ComponentAccessor.getIssueManager()

// get the latest comment
def lastComment = ComponentAccessor.commentManager.getLastComment(issue)
// check to see if comments exist and set assignee
if (lastComment != null) {
issue.setAssignee(lastComment.authorApplicationUser)
issueManager.updateIssue(user, issue, EventDispatchOption.DO_NOT_DISPATCH, false)
}

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
SERVER
VERSION
8.7.1
TAGS
AUG Leaders

Atlassian Community Events