Create
cancel
Showing results for 
Search instead for 
Did you mean: 
Sign up Log in
It's not the same without you

Join the community to find out what other Atlassian users are discussing, debating and creating.

Atlassian Community Hero Image Collage

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

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 Community Leader Feb 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 Community Leader Feb 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

It works for me, thank you!

Like Ismael Jimoh likes this

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?

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

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 Community Leader Jul 06, 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

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
TAGS
Community showcase
Published in Jira

Do you use Jira Cloud with Microsoft Teams?

Hi, Jira users! Do you use Jira alongside Microsoft Teams? We want to hear how you’ve used the power of Jira Cloud and Microsoft Teams (via the Jira Cloud for Microsoft Teams app) to achieve a team...

463 views 3 7
Read article

Community Events

Connect with like-minded Atlassian users at free events near you!

Find an event

Connect with like-minded Atlassian users at free events near you!

Unfortunately there are no Community Events near you at the moment.

Host an event

You're one step closer to meeting fellow Atlassian users at your local event. Learn more about Community Events

Events near you