How to add a current user to the project role by post-function

Elena Oleksenko
Contributor
February 22, 2019

Hello!

I have a post-function which should add a user to a project role, depending on value of custom field selected. 

I created a script based on samples which I found here on the Atlassian answers. 

My script is working but only with one condition: if I define the user name directly in the script: 

def actors = ['lsmith']

but I need script to work with current user, without defining their username in a code.  So that the current user will be added to selected project role. 

I tried this: 

def user = (ApplicationUser) ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("test_mav_user_picker").getValue(issue)

and then: 

projectRoleService.addActorsToProjectRole(currentUser,
projectRole1,
project,
ProjectRoleActor.USER_ROLE_ACTOR_TYPE,
errorCollection)

but this does not work and show an error: "Cannot find matching method"

Please help to understand how can I add a current user to a project role in a post-function? 

 

here is the full script: 

import com.atlassian.jira.bc.projectroles.ProjectRoleService
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleActor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.util.SimpleErrorCollection
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.issue.Issue
import com.atlassian.jira.bc.issue.IssueService
import com.atlassian.jira.issue.IssueInputParameters
import groovy.transform.Field
import com.atlassian.jira.issue.customfields.option.LazyLoadedOption
import com.atlassian.jira.user.ApplicationUser


def customFieldManager = ComponentAccessor.getCustomFieldManager()
def projectManager = ComponentAccessor.getProjectManager()
def projectRoleService = ComponentAccessor.getComponent(ProjectRoleService)
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)
def errorCollection = new SimpleErrorCollection()

//name of the custom field
def customFieldName = 'Notification type'
//get the custom field object
def customFieldObject = customFieldManager.getCustomFieldObjectByName(customFieldName)
Issue issue = issue

def customFieldValues = customFieldObject.getValue(issue)

def user = (ApplicationUser) ComponentAccessor.getCustomFieldManager().getCustomFieldObjectByName("test_mav_user_picker").getValue(issue)


def project = projectManager.getProjectObjByKey("ATL")
def actors = ['lsmith']
def currentUser = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()


customFieldValues.each { LazyLoadedOption it ->
def optionValue = it.getValue()
switch (optionValue){

case "Comments":
def projectRole = projectRoleManager.getProjectRole("Comments")
projectRoleService.addActorsToProjectRole(actors,
projectRole1,
project,
ProjectRoleActor.USER_ROLE_ACTOR_TYPE,
errorCollection)
break
case "Transition":
def projectRole1 = projectRoleManager.getProjectRole("Transition")
projectRoleService.addActorsToProjectRole(actors,
projectRole1,
project,
ProjectRoleActor.USER_ROLE_ACTOR_TYPE,
errorCollection)
break
case "Issue Update":
def projectRole2 = projectRoleManager.getProjectRole("Issue Update")
projectRoleService.addActorsToProjectRole(actors,
projectRole2,
project,
ProjectRoleActor.USER_ROLE_ACTOR_TYPE,
errorCollection)
break
case "Issue resolved":
def projectRole3 = projectRoleManager.getProjectRole("Issue resolved")
projectRoleService.addActorsToProjectRole(actors,
projectRole3,
project,
ProjectRoleActor.USER_ROLE_ACTOR_TYPE,
errorCollection)
break
}
}

 

2 answers

1 accepted

0 votes
Answer accepted
Ismael Jimoh
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 22, 2019
Elena Oleksenko
Contributor
February 22, 2019

Hello Ismael Jimoh!

Thank you for the reply!

I tried to use getLoggedInUser() and then add the result  to the addActorsToProjectRole but this is not working: error-.png

Ismael Jimoh
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 23, 2019

Hi @Elena Oleksenko 

The following script works for me.

Can you adjust yours to match?

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.issue.CustomFieldManager
import com.atlassian.jira.issue.*
import com.atlassian.jira.user.ApplicationUser
import com.atlassian.jira.user.util.*
import com.atlassian.jira.event.type.EventDispatchOption
import com.atlassian.jira.security.roles.ProjectRoleActor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.util.SimpleErrorCollection
import com.atlassian.jira.bc.projectroles.ProjectRoleService
import com.atlassian.jira.security.roles.ProjectRole
import com.atlassian.jira.util.SimpleErrorCollection
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.project.Project

IssueManager issueManager = ComponentAccessor.getIssueManager()
Issue issue = issue //Manager.getIssueByCurrentKey("ATM-262")


def projectRoleService = ComponentAccessor.getComponent(ProjectRoleService);
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager);
def projectManager = ComponentAccessor.getComponent(ProjectManager);
def errorCollection = new SimpleErrorCollection()

log.warn(issue.getProjectObject().key)

ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
Collection<String> actorCollection = new ArrayList<>();
actorCollection.add(user.getUsername());

log.warn(user)
Project project = projectManager.getProjectObjByKey(issue.getProjectObject().key)
//def projectRole = projectRoleManager.getProjectRole("Service Desk Customers")
ProjectRole projectRoleObject = projectRoleManager.getProjectRole("Service Desk Customers")
//def actors = ['actor1Key', 'actor2Key']
projectRoleService.addActorsToProjectRole(actorCollection,
projectRoleObject,
project,
ProjectRoleActor.USER_ROLE_ACTOR_TYPE,
errorCollection)

Do let me know if you have any further problems.

Like Elena Oleksenko likes this
Elena Oleksenko
Contributor
February 25, 2019

It works for me, thank you!

Like Ismael Jimoh likes this
Phillip Yohner August 1, 2019

This works great, except I encounter a "We can't find '<username>' in any accessible user directory. Check they're still active and available, and try again.error when the user is not in the Jira Internal Directory.  Almost all my users are in a second user directory that's connected to our LDAP, so for them the error is produced and the user is not added to the role.  Any thoughts on how to resolve this?

Fabiano Nascimento Leite
Contributor
May 12, 2020

Same problem about @Phillip Yohner 

Fredrik Ivansson Flinta
Contributor
June 30, 2021

I solved it by getting the "Actor" from a custom user picker field and then regexp a little.

Like: 

def actorCF= customFieldManager.getCustomFieldObjectByName("actor example")
def actorCFValue = issue.getCustomFieldValue(actorCF);
def actorString = actorCFValue.toString()

Will result in a string like: userID(userID)

By .replaceAll before ( and keep everything after ). Then add the string to the "actor collection for addActorsToProjectRole

Vedant Kulkarni
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 6, 2021

getUsername() didn't work for me. It resoved by getKey()

def Creators = new Long(10200)
ProjectRole projectRoleObject = projectRoleManager.getProjectRole(Creators)
def projectManager = ComponentAccessor.getComponent(ProjectManager)
def errorCollection = new SimpleErrorCollection()
ApplicationUser user = ComponentAccessor.getJiraAuthenticationContext().getLoggedInUser()
Collection<String> actorCollection = new ArrayList<>();
actorCollection.add(user.getKey())
Project project = projectManager.getProjectObjByKey(issue.getProjectObject().key)
projectRoleService.addActorsToProjectRole(actorCollection, projectRoleObject, project, ProjectRoleActor.USER_ROLE_ACTOR_TYPE, errorCollection)
Like # people like this
Ismael Jimoh
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 6, 2021

Please note that this solution would likely not work. 

Atlassian has moved away from using Usernames in Cloud hence the numerous errors reported.

See @Vedant Kulkarni suggestion of using getKey() instead.

Thanks.

Like Vedant Kulkarni likes this
0 votes
Eduard Diez
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 22, 2020

One question that I've... for a search a role developer in this scripts. How I can do?

you search:

def projectRoleService = ComponentAccessor.getComponent(ProjectRoleService);
def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager);

and if I need 

def projectRoleDeveloper = ???

Thank's for all

Suggest an answer

Log in or Sign up to answer