Can we use scripting (postman or scriptrunner)to remove users from Project roles(leaving only groups)
Hi,
I have made a script that removes a group from a project role on all projects in an instance:
import com.atlassian.crowd.embedded.api.Groupimport com.atlassian.jira.bc.projectroles.ProjectRoleServiceimport com.atlassian.jira.component.ComponentAccessorimport com.atlassian.jira.project.Projectimport com.atlassian.jira.project.ProjectManagerimport com.atlassian.jira.security.roles.ProjectRoleimport com.atlassian.jira.security.roles.ProjectRoleActorimport com.atlassian.jira.security.roles.ProjectRoleActorsimport com.atlassian.jira.security.roles.ProjectRoleManagerimport com.atlassian.jira.security.roles.RoleActorimport com.onresolve.scriptrunner.parameters.annotation.ProjectPickerimport com.onresolve.scriptrunner.parameters.annotation.ProjectRolePickerimport com.onresolve.scriptrunner.parameters.annotation.GroupPickerimport com.onresolve.scriptrunner.parameters.annotation.*import org.apache.log4j.Levelimport org.apache.log4j.Loggerimport static com.atlassian.jira.security.roles.ProjectRoleActor.GROUP_ROLE_ACTOR_TYPEimport static java.util.Collections.singletonListdef log = Logger.getLogger("be.script")log.setLevel(Level.INFO)GroupPicker(label = 'Group', description = 'Pick a group to remove from role', placeholder = 'Pick a group')Group INPUT_groupShortTextInput(label = "Project role", description = "")String INPUT_projectRoleProjectManager projectManager = ComponentAccessor.getProjectManager()ProjectRoleService projectRoleService = ComponentAccessor.getOSGiComponentInstanceOfType(ProjectRoleService.class)ProjectRoleManager projectRoleManager = ComponentAccessor.getOSGiComponentInstanceOfType(ProjectRoleManager.class)int count = 0ProjectRole projectRole = projectRoleManager.getProjectRole(INPUT_projectRole)List<Project> allProjects = projectManager.getProjects()for (Project currentProject: allProjects) {ProjectRoleActors projectRoleActors = projectRoleService.getProjectRoleActors(projectRole, currentProject, null)Optional<RoleActor> actorToRemove = projectRoleActors.getRoleActors().stream() .filter {it -> it.parameter.equalsIgnoreCase(INPUT_group.getName())} .findFirst()if (actorToRemove.isPresent()) { count ++projectRoleService.removeActorsFromProjectRole(singletonList(INPUT_group.getName()), projectRole, currentProject, GROUP_ROLE_ACTOR_TYPE, null)log.info("$count => project $currentProject") }}return "Done!"
With some tinkering you might be able to transform it so it removes a user form a project role in all projects or to whatever need you might have.
In the top of the script you will see the following:
GroupPicker(label = 'Group', description = 'Pick a group to remove from role', placeholder = 'Pick a group')Group INPUT_groupShortTextInput(label = "Project role", description = "")String INPUT_projectRole
In the script runner script console this shows a dropdown to pick a group and an input textbox to provide the project role. You can replace this with logic for a hardcoded user etc ...
Near the end you will see this line:
projectRoleService.removeActorsFromProjectRole(singletonList(INPUT_group.getName()), projectRole, currentProject, GROUP_ROLE_ACTOR_TYPE, null)
The param GROUP_ROLE_ACTOR_TYPE tells the service to remove groups provided in the other params. This params can also be USER_ROLE_ACTOR_TYPE, which will indicate to remove users provided.
With this you should come to a solution.
Jeroen
HI @Jeroen Poismans
Can we remove only users from project roles and we don't want to remove groups from project roles?
It looks like you're new here. Sign in or register to get started.