Groovy Post Function: Assign issue to the currently logged-in user if a condition is met

Nishant Kansal February 12, 2018

Hello,

I am trying to develop a post function that shall assign the issue to the currently logged-in user, if a condition is met.

I am able to fetch the current logged-in user using below:

// Get the current logged in user
def user = ComponentAccessor.getJiraAuthenticationContext().getUser().getName()

Now, I have to assign the issue to this user. For that, I am trying

issue.setAssigneeId(user)

However, it is resulting "null" and issue remains unassigned.

Can anyone please let me know, what I am missing and how I can achieve it?

Thanks.

Nishant

1 answer

1 accepted

0 votes
Answer accepted
Alexey Matveev
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.
February 12, 2018

Your code should be like this

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

issue.setAssigneeId(user)
Nishant Kansal February 12, 2018

I tried it..It is giving below error:

No signature of method: com.atlassian.jira.issue.IssueImpl.setAssigneeId() is applicable for argument types: (com.atlassian.jira.user.BridgedDirectoryUser) values: [Nishant.Kansal@abc.com:10001] Possible solutions: setAssigneeId(java.lang.String), getAssigneeId(), setAssignee(com.atlassian.crowd.embedded.api.User), getAssignee()

Alexey Matveev
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.
February 12, 2018
def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

issue.setAssignee(user)

What is your Jira version?  

Nishant Kansal February 12, 2018

Atlassian JIRA Project Management Software (v6.3.12#6343-sha1:0d8f4aa) 

Alexey Matveev
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.
February 12, 2018

Did the lines in my previous comment work? In Jira version 6 there were problems with ApplicationUser and User classes. Some methods worked with ApplicationUser and some with User and those classes always had to be converted to each other. Kindly try the lines in my previous comment.

Nishant Kansal February 12, 2018

No, it didn't work.

I checked few other scripts available in this forum and a validation need to be in place. Can you please guide me on the same?

Alexey Matveev
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.
February 13, 2018

What is the error?

Nishant Kansal February 15, 2018

After going through couple of articles, I worked out below script. It seems, I am so near and yet not able to get it through.

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import com.atlassian.jira.event.type.EventDispatchOption
//import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.UpdateIssueRequest
import org.apache.log4j.Level
import org.apache.log4j.Logger

def LOG = Logger.getLogger("com.onresolve.jira.groovy")

LOG.error("Updating Issue.");

def user = ComponentAccessor.getJiraAuthenticationContext().getUser().getName()

MutableIssue updatedIssue = ComponentAccessor.getIssueManager().getIssueObject(currentIssue.getKey())

updatedIssue.setAssigneeId(user)

IssueManager issueManager = ComponentAccessor.getIssueManager();
issueManager.updateIssue(user, updatedIssue, EventDispatchOption.ISSUE_UPDATED, true)

It is giving me below error: 

No signature of method: com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue() is applicable for argument types: (java.lang.String, com.atlassian.jira.issue.IssueImpl, com.atlassian.jira.event.type.EventDispatchOption$EventDispatchOptionImpl, java.lang.Boolean). 

Possible solutions: updateIssue(com.atlassian.crowd.embedded.api.User, com.atlassian.jira.issue.MutableIssue, com.atlassian.jira.event.type.EventDispatchOption, boolean), updateIssue(com.atlassian.jira.user.ApplicationUser, com.atlassian.jira.issue.MutableIssue, com.atlassian.jira.issue.UpdateIssueRequest)

Tried the possible solutions, however, no success.

Please help.

Alexey Matveev
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.
February 15, 2018

It is because your user variable is a String and you need to pass com.atlassian.jira.user.ApplicationUser. That is why I rewrote it like this

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

issue.setAssignee(user)

If you do not want to change your code much than it could be like this

 

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import com.atlassian.jira.event.type.EventDispatchOption
//import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.UpdateIssueRequest
import org.apache.log4j.Level
import org.apache.log4j.Logger

def LOG = Logger.getLogger("com.onresolve.jira.groovy")

LOG.error("Updating Issue.");

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
MutableIssue updatedIssue = ComponentAccessor.getIssueManager().getIssueObject(currentIssue.getKey())

updatedIssue.setAssigneeId(user.getName())

IssueManager issueManager = ComponentAccessor.getIssueManager();
issueManager.updateIssue(user, updatedIssue, EventDispatchOption.ISSUE_UPDATED, true)
Nishant Kansal February 15, 2018

Thank you, Alexey for pointing out the mistake in my latest code. It is working fine now. However, if I am using below method, it is giving this warning. This method is not advocated since 6.0 Jira version. Please advise.

def user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()

image.png

Alexey Matveev
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.
February 15, 2018

Ah, ok. Try to use the getUser(). It is fine

Nishant Kansal February 16, 2018

When I am running the below code (with getUser())

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.ComponentManager
import com.atlassian.jira.issue.MutableIssue
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.IssueManager
import com.atlassian.jira.user.util.UserManager
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import com.atlassian.jira.event.type.EventDispatchOption
//import com.atlassian.crowd.embedded.api.User
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.UpdateIssueRequest
import org.apache.log4j.Level
import org.apache.log4j.Logger

def LOG = Logger.getLogger("com.onresolve.jira.groovy")

LOG.error("Updating Issue.");

def user = ComponentAccessor.getJiraAuthenticationContext().getUser()
MutableIssue updatedIssue = ComponentAccessor.getIssueManager().getIssueObject("TIK-16894")

updatedIssue.setAssigneeId(user.getName())

IssueManager issueManager = ComponentAccessor.getIssueManager();
issueManager.updateIssue(user, updatedIssue, EventDispatchOption.ISSUE_UPDATED, true)

It is giving below error:

 

No signature of method: com.atlassian.jira.issue.managers.DefaultIssueManager.updateIssue() is applicable for argument types: (com.atlassian.jira.user.DelegatingApplicationUser, com.atlassian.jira.issue.IssueImpl, com.atlassian.jira.event.type.EventDispatchOption$EventDispatchOptionImpl, java.lang.Boolean)

Possible solutions: updateIssue(com.atlassian.crowd.embedded.api.User, com.atlassian.jira.issue.MutableIssue, com.atlassian.jira.event.type.EventDispatchOption, boolean), updateIssue(com.atlassian.jira.user.ApplicationUser, com.atlassian.jira.issue.MutableIssue, com.atlassian.jira.issue.UpdateIssueRequest) 

When I am trying the possible solutions, same error is coming in one way or other.

Please help. 

Alexey Matveev
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.
February 16, 2018

I think you should use getLoggedInUser. Do not pay attention to the warning

Nishant Kansal February 21, 2018

It seems that is the only option for the time being. The latest implementation is not returning the value per my requirement. Thank you so much for all your help, Alexey.

Suggest an answer

Log in or Sign up to answer
TAGS
AUG Leaders

Atlassian Community Events