Snippets

Adaptavist Stash - Update Projects with group write permissions

Created by Jamie Echlin last modified Mark McCormack
import com.atlassian.sal.api.component.ComponentLocator
import com.atlassian.stash.project.Project
import com.atlassian.stash.project.ProjectService
import com.atlassian.stash.project.ProjectUpdateRequest
import com.atlassian.stash.user.Permission
import com.atlassian.stash.user.PermissionAdminService
import com.atlassian.stash.user.SetPermissionRequest
import com.atlassian.stash.user.UserService
import com.atlassian.stash.util.Page
import com.atlassian.stash.util.PageProvider
import com.atlassian.stash.util.PageRequest
import com.atlassian.stash.util.PagedIterable

def projectService = ComponentLocator.getComponent(ProjectService)
def permissionAdminService = ComponentLocator.getComponent(PermissionAdminService)
def userService = ComponentLocator.getComponent(UserService)

new PagedIterable<Project>(new PageProvider<Project>() {
    @Override
    Page<Project> get(PageRequest pageRequest) {
        projectService.findAll(pageRequest) as Page<Project>
    }
}, 10).each { project ->
    log.debug("Update project ${project.key}")
    def setPermissionRequest = new SetPermissionRequest.Builder()
       .projectPermission(Permission.PROJECT_WRITE, project) 
       .group("developers") 
       .build()

    permissionAdminService.setPermission(setPermissionRequest)
}

Comments (0)

HTTPS SSH

You can clone a snippet to your computer for local editing. Learn more.