Forums

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

Retrieving users from with a specific project role and removing them from that project role

Ruben Segura July 14, 2023 edited

I have hobbled together a script which find users with a specific project role and then removes them from that project role.  I am running into an error that I haven't been able to fix and hoping for guidance from you.  

I have tried adding the user key as part of a one set collection then as a string, I have broken the script into two parts with one part retrieving the users with the designated role and the second part deleting a set of users within the same project with the designated project role with both parts working by themselves.  There are only two variables whose values change - the user key and the project key.  

Here is the error:

Error: groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.bc.projectroles.DefaultProjectRoleService.removeActorsFromProjectRole() is applicable for argument types: (String, com.atlassian.jira.security.roles.ProjectRoleImpl, String...) values: [JIRAUSER00000, Developers, ZEBRA, atlassian-user-role-actor, ...] Possible solutions: removeActorsFromProjectRole(java.util.Collection, com.atlassian.jira.security.roles.ProjectRole, com.atlassian.jira.project.Project, java.lang.String, com.atlassian.jira.util.ErrorCollection), removeActorsFromProjectRole(com.atlassian.jira.user.ApplicationUser, java.util.Collection, com.atlassian.jira.security.roles.ProjectRole, com.atlassian.jira.project.Project, java.lang.String, com.atlassian.jira.util.ErrorCollection) at Script7533$_run_closure1$_closure2.doCall(Script7533.groovy:61) at Script7533$_run_closure1.doCall(Script7533.groovy:53) at Script7533.run(Script7533.groovy:37)

------------------------------------------

Here is the code:

import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.security.roles.ProjectRoleManager
import com.atlassian.jira.bc.projectroles.ProjectRoleService
import com.atlassian.jira.project.ProjectManager
import com.atlassian.jira.security.roles.RoleActor

def result = ""
//to verify parameters are being passed to remove function

def projectManager = ComponentAccessor.getComponent(ProjectManager)
//Declare a Project Manager
def projectList = projectManager.getProjectObjects().name


def projectRoleManager = ComponentAccessor.getComponent(ProjectRoleManager)

//Declare a list of projects
def projectRole = projectRoleManager.getProjectRole("Developers")
//Define a role object. In this example the role is Developers

def userManager = ComponentAccessor.userManager
def projectRoleService = ComponentAccessor.getComponent(ProjectRoleService)

def roleActorType = "atlassian-user-role-actor"

//iterate through each project to find users in the designated role
projectList.each{ eachProject ->

    def project = projectManager.getProjectObjByName(eachProject)
    //Define a project object.  We're looping through all of the projects

    def projectKey = project.getKey()

    def usersInRole = projectRoleManager.getProjectRoleActors(projectRole, project).getApplicationUsers().toList()
    //We feed the projectRoleManager two already-defined arguments, projectRole and project
    //The results are added to a list

    def roleActor = projectRoleManager.getProjectRoleActors(projectRole,project)
    //The results are added to a list


    //iterate through each user of the project with the designated role
    usersInRole.each{

        users ->
            if  (users.name != null) {
                def uNameKey = users.key.toString()
                if (uNameKey != null) {

                    result+= "projectRoleService.removeActorsFromProjectRole(" + uNameKey + "," + projectRole + "," + projectKey + "," + roleActorType + ", null)<br /><br />"
                    projectRoleService.removeActorsFromProjectRole(uNameKey, projectRole, projectKey, roleActorType, null)
               }
            }
    }
}

return result
-------------------------------------------------
Output when this line is commented out
// projectRoleService.removeActorsFromProjectRole(uNameKey, projectRole, projectKey, roleActorType, null)
projectRoleService.removeActorsFromProjectRole(JIRAUSER00000,Developers,ZEBRA,atlassian-user-role-actor, null)

projectRoleService.removeActorsFromProjectRole(000000,Developers,ZEBRA,atlassian-user-role-actor, null)

projectRoleService.removeActorsFromProjectRole(JIRAUSER00000,Developers,ZEBRA,atlassian-user-role-actor, null)

projectRoleService.removeActorsFromProjectRole(000000,Developers,ZEBRA,atlassian-user-role-actor, null)
...etc.
----------------
Output when this line is not commented out
projectRoleService.removeActorsFromProjectRole(uNameKey, projectRole, projectKey, roleActorType, null)
Error: groovy.lang.MissingMethodException: No signature of method: com.atlassian.jira.bc.projectroles.DefaultProjectRoleService.removeActorsFromProjectRole() is applicable for argument types: (String, com.atlassian.jira.security.roles.ProjectRoleImpl, String...) values: [JIRAUSER00000, Developers, ZEBRA, atlassian-user-role-actor, ...] Possible solutions: removeActorsFromProjectRole(java.util.Collection, com.atlassian.jira.security.roles.ProjectRole, com.atlassian.jira.project.Project, java.lang.String, com.atlassian.jira.util.ErrorCollection), removeActorsFromProjectRole(com.atlassian.jira.user.ApplicationUser, java.util.Collection, com.atlassian.jira.security.roles.ProjectRole, com.atlassian.jira.project.Project, java.lang.String, com.atlassian.jira.util.ErrorCollection) at Script7533$_run_closure1$_closure2.doCall(Script7533.groovy:61) at Script7533$_run_closure1.doCall(Script7533.groovy:53) at Script7533.run(Script7533.groovy:37)
Thank you.
Ruben

1 answer

1 accepted

Suggest an answer

Log in or Sign up to answer
0 votes
Answer accepted
PD Sheehan
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 14, 2023

The clue is in here:

Possible solutions: removeActorsFromProjectRole(java.util.Collection, com.atlassian.jira.security.roles.ProjectRole, com.atlassian.jira.project.Project, java.lang.String, com.atlassian.jira.util.ErrorCollection)

The uNameKey must be in a collection (of size 1).

And a project object is expected rather than a project key string.

And rather than passing a null ErrorCollection, create one so you can examine the result

 Try

import com.atlassian.jira.util.ErrorCollections
def errorCollection = ErrorCollections.empty()
projectRoleService.removeActorsFromProjectRole([uNameKey], projectRole, project, roleActorType, errorCollection)
if (errorCollection.hasAnyErrors()) {
log.error errorCollection.toString()
}
Ruben Segura July 17, 2023

Thank you so much!  I had realized that I needed to change uNameKey to a collection but changing project to the project object instead of the project key was the extra piece that I was missing.  

TAGS
AUG Leaders

Atlassian Community Events