featured image SCM-Manager Plugins Part 10 - Script

June 27, 2016 / by Daniel Huchthausen / In Technology

SCM-Manager Plugins Part 10 - Script

+++This post was migrated from our former blog about SCM-Manager Universe. Therefore, the design is slightly different and some information might not be 100% applicable to Cloudogu EcoSystem. So don't be alarmed; enjoy reading.+++

If you want to automate activities or execute infrequent tasks, you can use the scm-script-plugin. It allows you to write and execute scripts for SCM-Manager in Groovy and ECMAScript. Scripts require less overhead than plugins, but their performance can be weaker.That’s why you should use them for infrequent tasks or activities that don’t require a high performance.

Script Plugin

After the installation of the scm-script-plugin via the “Config > Plugins” screen and the restart of the application you will see the new menu item “Script Console” in the SCM-Manager navigation menu. There you can enter and execute scripts.

To write and execute scripts you need administrative rights. Users without the required permission don’t see the navigation menu item “Script Console”. Example

We’ve written a sample Groovy script that collects all permissions of a user in SCM-Manager. Simply paste it in the “Script Console” screen and replace the username “scmadmin” with another username (line 9).


import sonia.scm.*;
import sonia.scm.config.*;
import sonia.scm.group.*;
import sonia.scm.user.*;
import sonia.scm.security.*;
import sonia.scm.repository.*;
import sonia.scm.repository.api.*;

def username = "scmadmin"

// check for admin

def userManager = injector.getInstance(UserManager.class);
def user = userManager.get(username);

println ""

if (user.isActive()){
    println "${username} is active"
} else {
    println "${username} is deactivated"
}

def configuration = injector.getInstance(ScmConfiguration.class);

// find groups
def groupAdmin = false;
def groups = ["_authenticated"]
def adminGroups = configuration.getAdminGroups(); if (adminGroups == null){
    adminGroups = [];
}

def groupManager = injector.getInstance(GroupManager.class);
for ( def group : groupManager.getGroupsForMember(username) ){
    groups.add(group.getName())
    if (adminGroups.contains(group.getName())){
        groupAdmin = true;
    }
}

// is admin?
def adminUsers = configuration.getAdminUsers(); if (adminUsers == null){
    adminUsers = [];
}
if ( user.isAdmin() || adminUsers.contains(username) || groupAdmin){
    println "${username} has administrator privileges"
}

println ""
println "Global Permissions"
println ""

// find global permissions
def securitySystem = injector.getInstance(SecuritySystem.class);
for (def permission : securitySystem.getAllPermissions()){
    if (!permission.isGroupPermission() && username.equals(permission.getName())){
        println("  "  + permission.getPermission())
    } else if ( permission.isGroupPermission() &&
groups.contains(permission.getName()) ){
    println("  " + permission.getPermission())
    }
}

println ""
println "Repository Permissions"
println ""

// find reposiotry permissions
def repositoryManager = injector.getInstance(RepositoryManager.class);
for (def repository : repositoryManager.getAll()){
    for (def permission : repository.getPermissions()){
        if (!permission.isGroupPermission() && username.equals(permission.getName())){
            println("  " + repository.getType() + "/" +
repository.getName() + " - " + permission.getType())
    } else if ( permission.isGroupPermission() &&
groups.contains(permission.getName()) ){
    println("  " + repository.getType() + "/" +
repository.getName() + " - " + permission.getType())
        }
    }
}

Conclusion

The scm-script-plugin allows you to easily execute custom scripts and automate tasks. They require less overhead than plugins but have a weaker performance. If you want to execute them regularly, you simply have to configure a cron job to execute your script.

With kind regards, your SCM-Manager Universe Team


Daniel Huchthausen
Daniel Huchthausen

- Consultant -

When he is not exploring the wilderness, Daniel keeps himself busy with topics such as quality assurance, testing and PM methods.