Skip to main content
Example scripts
arrow icon
To homepage
Jira
Data centre icon
Data Center

Updates all the Projects provided with a single Permission Scheme.

Created 2 year(s) ago, Updated 5 month(s) ago
App in script
ScriptRunner For Jira
ScriptRunner For Jira
by Adaptavist
Compatibility
compatibility bullet
Jira (8.0 - 8.22)
compatibility bullet
ScriptRunner For Jira (6.56.0)
Language |
groovy
import com.atlassian.jira.component.ComponentAccessor
import com.atlassian.jira.permission.PermissionSchemeManager
import com.atlassian.jira.project.Project
import com.atlassian.jira.scheme.Scheme
import com.onresolve.scriptrunner.parameters.annotation.ProjectPicker
import com.onresolve.scriptrunner.parameters.annotation.ShortTextInput
import groovy.transform.Field

@ProjectPicker(label = "Project picker", description = "Projects to change their Permission Scheme", multiple = true)
List<Project> projectList
@ShortTextInput(label = "Permission Scheme ID", description = "Permission Scheme ID to associate the previous selected Projects. Eg: 10000")
String permissionSchemeId

// If any of the above fields are not filled in, stop the script and returns an error informing about it.
assert projectList && permissionSchemeId : "Please, fulfill all the fields before executing the Script"

@Field PermissionSchemeManager permissionSchemeManager = ComponentAccessor.permissionSchemeManager
def permissionScheme = permissionSchemeManager.getSchemeObject(permissionSchemeId.toLong())

// If no permissions scheme is found with the provided Id, stop the script and returns an error informing about it.
assert permissionScheme : "There is no Permission Scheme with the ID ${permissionSchemeId}"

// The empty Map to store the Project keys associated with their Permission Scheme before the change.
def currentPermissions = [:] as Map<String, List>

/**
 * Stores the Project keys associated with their Permission Scheme before the change and
 * updates the selected Projects with the Permission Scheme provided.
 */
projectList.each { project ->
    saveProjectKeyAssociatedToPermissionScheme(currentPermissions, project)
    associatePermissionSchemeToProject(project, permissionScheme)
}

// Shows the Project keys associated to his previous permission scheme.
currentPermissions

/**
 * Stores the Project key that belongs to his current Permissions Scheme.
 * The format stored is as following:
 *      Permission Scheme name: List of the current Project keys that belongs to that Permission Scheme.
 * @param Map <String, List> currentPermissions - the map to store the information.
 * @param Project project - the Project to store in the List of the Map.
 */
void saveProjectKeyAssociatedToPermissionScheme(Map<String, List> currentPermissions, Project project) {
    def currentPermissionSchemeName = permissionSchemeManager.getSchemeFor(project).name
    def currentPermissionSchemeProjects = currentPermissions[currentPermissionSchemeName]

    if (currentPermissionSchemeProjects) {
        currentPermissionSchemeProjects << project.key
    } else {
        currentPermissionSchemeProjects = [project.key]
    }

    currentPermissions << [(currentPermissionSchemeName): currentPermissionSchemeProjects]
}

/**
 * Associates a Project with a new Permission Scheme.
 * @param Project project - the Project to update.
 * @param Scheme permissionScheme - the Permission Scheme to associate with the project.
 */
void associatePermissionSchemeToProject(Project project, Scheme permissionScheme) {
    permissionSchemeManager.removeSchemesFromProject(project)
    permissionSchemeManager.addSchemeToProject(project, permissionScheme)
}
Having an issue with this script?
Report it here