Skip to content

Commit

Permalink
- correction of initialization: script not yet approved are not approved
Browse files Browse the repository at this point in the history
 any more (was a bug in initial commits of the PR)
- correct PERMISSIONS typo
- show approved / not yet approved scripts directly in the list
  • Loading branch information
Wadeck committed Feb 13, 2018
1 parent 0ccc56b commit 8589a22
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import hudson.init.InitMilestone;
import hudson.init.Initializer;
import hudson.security.Permission;
import hudson.security.PermissionGroup;
import hudson.security.PermissionScope;
Expand All @@ -50,15 +49,15 @@ public class ScriptlerPluginImpl extends Plugin {

private final static Logger LOGGER = Logger.getLogger(ScriptlerPluginImpl.class.getName());

public static final PermissionGroup SCRIPTLER_PERMISSONS = new PermissionGroup(ScriptlerManagement.class, Messages._permissons_title());
public static final PermissionGroup SCRIPTLER_PERMISSIONS = new PermissionGroup(ScriptlerManagement.class, Messages._permissons_title());

public static final Permission CONFIGURE = new Permission(
SCRIPTLER_PERMISSONS, "Configure",
SCRIPTLER_PERMISSIONS, "Configure",
Messages._permissons_configure_description(), Jenkins.RUN_SCRIPTS,
PermissionScope.JENKINS
);
public static final Permission RUN_SCRIPTS = new Permission(
SCRIPTLER_PERMISSONS, "RunScripts",
SCRIPTLER_PERMISSIONS, "RunScripts",
Messages._permissons_runScript_description(), Jenkins.RUN_SCRIPTS,
PermissionScope.JENKINS
);
Expand Down Expand Up @@ -95,38 +94,23 @@ private void synchronizeConfig() throws IOException {
cfg.save();
}

@Override
public void postInitialize() throws Exception {
@Initializer(after = InitMilestone.JOB_LOADED)
public static void afterJobLoaded() throws Exception {
setupExistingScripts();
}

private static void setupExistingScripts() throws Exception {
for (Script script : ScriptlerConfiguration.getConfiguration().getScripts()) {
File scriptFile = new File(ScriptlerManagement.getScriptDirectory(), script.getScriptPath());
try{
String scriptSource = FileUtils.readFileToString(scriptFile, "UTF-8");

// we cannot do that during start since the ScriptApproval is not yet loaded
// and only after JOB_LOADED to have the securityRealm configured
ScriptHelper.putScriptInApprovalQueueIfRequired(scriptSource);
}
catch(IOException e){
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Source file for the script [{0}] was not found", script.getId());
}
}
}

/**
* search into the declared backup directory for backup archives
*/
public List<File> getAvailableScripts() throws IOException {
File scriptDirectory = ScriptlerManagement.getScriptDirectory();
LOGGER.log(Level.FINE, "Listing files of {0}", scriptDirectory.getAbsoluteFile());

File[] scriptFiles = scriptDirectory.listFiles();

List<File> fileList;
if (scriptFiles == null) {
fileList = new ArrayList<File>();
} else {
fileList = Arrays.asList(scriptFiles);
}

return fileList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -99,15 +99,15 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
boolean isOk = false;
if (!script.nonAdministerUsing) {
listener.getLogger().println(Messages.scriptNotUsableInBuildStep(script.getName()));
LOGGER.log(Level.WARNING, "The script [{0} ({1})] is not allowed to be executed in a build, check its configuration. It concerns the build {2}:{3}",
LOGGER.log(Level.WARNING, "The script [{0} ({1})] is not allowed to be executed in a build, check its configuration. It concerns the build [{2}:{3}]",
new Object[]{script.getName(), script.getId(), build.getProject().getName(), build.getDisplayName()}
);
return false;
}

if(!ScriptHelper.isApproved(script.script)){
listener.getLogger().println(Messages.scriptNotApprovedYet(script.getName()));
LOGGER.log(Level.WARNING, "The script [{0} ({1})] is not approved yet, consider asking your administrator to approve it. It concerns the build {2}:{3}",
LOGGER.log(Level.WARNING, "The script [{0} ({1})] is not approved yet, consider asking your administrator to approve it. It concerns the build [{2}:{3}]",
new Object[]{script.getName(), script.getId(), build.getProject().getName(), build.getDisplayName()}
);
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
import org.jenkinsci.plugins.scriptler.util.ByIdSorter;

import com.thoughtworks.xstream.XStream;
import org.jenkinsci.plugins.scriptler.util.ScriptHelper;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.DoNotUse;

Expand Down Expand Up @@ -156,7 +157,7 @@ public boolean isAllowRunScriptPermission() {
}

@Restricted(DoNotUse.class) // for Jelly view
public List<Script> getSortedScripts(){
public List<ScriptAndApproved> getSortedScripts(){
List<Script> sortedScripts;
if(Jenkins.getInstance().hasPermission(ScriptlerPluginImpl.CONFIGURE)){
sortedScripts = new ArrayList<Script>(this.getScripts());
Expand All @@ -166,6 +167,34 @@ public List<Script> getSortedScripts(){

Collections.sort(sortedScripts, Script.COMPARATOR_BY_NAME);

return sortedScripts;
List<ScriptAndApproved> result = new ArrayList<ScriptAndApproved>(sortedScripts.size());
for (Script script : sortedScripts) {
Script scriptWithSrc = ScriptHelper.getScript(script.getId(), true);
Boolean approved = null;
if(scriptWithSrc != null && scriptWithSrc.script != null){
approved = ScriptHelper.isApproved(scriptWithSrc.script, false);
}
result.add(new ScriptAndApproved(script, approved));
}
return result;
}

@Restricted(DoNotUse.class) // for Jelly view
public static class ScriptAndApproved {
private Script script;
private Boolean approved;

private ScriptAndApproved(Script script, Boolean approved) {
this.script = script;
this.approved = approved;
}

public Script getScript() {
return script;
}

public Boolean getApproved() {
return approved;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import javax.annotation.CheckForNull;
import javax.servlet.ServletException;

import jenkins.model.Jenkins;
import jenkins.model.Jenkins.MasterComputer;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
Expand Down Expand Up @@ -157,7 +158,7 @@ public static String runScript(String node, String scriptTxt, Parameter[] parame
if (node != null && scriptTxt != null) {

try {
Computer comp = Hudson.getInstance().getComputer(node);
Computer comp = Jenkins.getInstance().getComputer(node);
if (comp == null && "(master)".equals(node)) {
output = MasterComputer.localChannel.call(new GroovyScript(scriptTxt, parameters, false, new StreamTaskListener(sos)));
} else if (comp == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,22 @@
</j:if>
<table class="pane">
<j:set var="items" value="${it.configuration.sortedScripts}" />
<j:forEach var="t" items="${items}">
<j:forEach var="i" items="${items}">
<j:set var="approved" value="${i.approved}" />
<j:set var="t" value="${i.script}" />
<tr valign="center" style="border-top: 0px;">
<td class="pane scriptler-nowrap" width="104">
<j:choose>
<j:when test="${approved == null}">
<img width="16" height="16" title="${%scriptNotFound}" src="${imagesURL}/16x16/warning.png" class="scriptler-nomargin" />
</j:when>
<j:when test="${approved == true}">
<img width="16" height="16" title="${%scriptApproved}" src="${imagesURL}/16x16/accept.png" class="scriptler-nomargin" />
</j:when>
<j:otherwise>
<img width="16" height="16" title="${%scriptNotYetApproved}" src="${imagesURL}/16x16/lock.png" class="scriptler-nomargin"/>
</j:otherwise>
</j:choose>
<j:choose>
<j:when test="${t.available == false}">
<img width="16" height="16" title="${%fileNotAvailable}" src="${imagesURL}/16x16/red.gif" class="scriptler-nomargin" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,6 @@ scriptdirectorytext=The scripts are saved at:
fileNotAvailable=This file is physically not available on the system.
noScriptsAvailable=Currently you do not have any scripts available. You can import scripts from a remote catalog or create your own.
usableInBuildStep=allowed to be used in Scriptler build step
scriptNotFound=Script not found
scriptApproved=Script approved
scriptNotYetApproved=Script not yet approved, consider asking an administrator to approve it before use

0 comments on commit 8589a22

Please sign in to comment.