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
}
}
There is a method called getloggedinuser or so: https://community.atlassian.com/t5/Marketplace-Apps-questions/Problem-with-JiraAuthenticationContext-getLoggedInUser/qaq-p/6668
Similar to the above.
Hope it helps.
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:
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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?
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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)
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
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
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.