Permalink
Browse files

[FIXED JENKINS-42888] [FIXED ENKINS-44480] [FIXED JENKINS-45399] show…

… correct arguments for selected script and add details link to script
  • Loading branch information...
imod committed Aug 15, 2017
1 parent 895fe84 commit f691a78cce9bcce34a9d7e6ab270321b4a76ecd7
@@ -45,7 +45,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>config-file-provider</artifactId>
<version>2.15</version>
<version>2.16.3</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
@@ -0,0 +1,36 @@
package org.jenkinsci.plugins.managedscripts;

import hudson.model.Item;
import jenkins.model.Jenkins;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.configfiles.utils.DescriptionResponse;
import org.kohsuke.stapler.StaplerRequest;

public class DetailLinkDescription extends DescriptionResponse {
private DetailLinkDescription(String linkHtml) {
super(linkHtml);
}

public static DetailLinkDescription getDescription(StaplerRequest req, Item context, String fileId, String argumentDetails) {
return new DetailLinkDescription(getDetailsLink(req, context, fileId, argumentDetails));
}

private static String getDetailsLink(StaplerRequest req, Item context, String fileId, String argumentDetails) {
String link = req.getContextPath();
link = StringUtils.isNotBlank(context.getUrl()) ? link + "/" + context.getUrl() : link;
link = link + "configfiles/show?id=" + fileId;
String html = "<a target=\"_blank\" href=\"" + link + "\">view selected file</a>";

if (StringUtils.isNotBlank(argumentDetails)) {
html = html + "<br />" + argumentDetails;
}

// 1x16 spacer needed for IE since it doesn't support min-height
html = "<div class='ok'><img src='" +
req.getContextPath() + Jenkins.RESOURCE_PATH + "/images/none.gif' height=16 width=1>" +
html + "</div>";

return html;
}

}
@@ -1,30 +1,27 @@
package org.jenkinsci.plugins.managedscripts;

import hudson.model.*;
import hudson.model.Queue;
import hudson.util.ListBoxModel;
import org.jenkinsci.plugins.managedscripts.PowerShellConfig.Arg;
import hudson.Extension;
import hudson.ExtensionList;
import hudson.FilePath;
import hudson.model.*;
import hudson.model.Queue;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Builder;
import hudson.tasks.CommandInterpreter;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import org.jenkinsci.lib.configprovider.ConfigProvider;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.QueryParameter;
import org.jenkinsci.lib.configprovider.model.Config;
import org.jenkinsci.plugins.configfiles.ConfigFiles;
import org.jenkinsci.plugins.managedscripts.PowerShellConfig.Arg;
import org.kohsuke.stapler.*;
import org.kohsuke.stapler.bind.JavaScriptMethod;
import org.kohsuke.stapler.DataBoundConstructor;

import java.io.Serializable;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.jenkinsci.lib.configprovider.model.Config;
import org.jenkinsci.plugins.configfiles.ConfigFiles;
import hudson.FilePath;
import hudson.tasks.CommandInterpreter;

/**
* A project that uses this builder can choose a build step from a list of predefined powershell files that are used as command line scripts.
* <p>
@@ -120,21 +117,21 @@ public String getBuildStepId() {
protected String getContents() {

Executor executor = Executor.currentExecutor();
if(executor!=null) {
if (executor != null) {
Queue.Executable currentExecutable = executor.getCurrentExecutable();
if(currentExecutable != null) {
if (currentExecutable != null) {
Config buildStepConfig = ConfigFiles.getByIdOrNull((Run<?, ?>) currentExecutable, getBuildStepId());
if (buildStepConfig == null) {
throw new IllegalStateException(Messages.config_does_not_exist(getBuildStepId()));
}
return buildStepConfig.content + "\r\nexit $LastExitCode";
} else {
String msg = "current executable not accessable! can't get content of script: "+getBuildStepId();
String msg = "current executable not accessable! can't get content of script: " + getBuildStepId();
LOGGER.log(Level.SEVERE, msg);
throw new RuntimeException(msg);
}
} else {
String msg = "current executor not accessable! can't get content of script: "+getBuildStepId();
String msg = "current executor not accessable! can't get content of script: " + getBuildStepId();
LOGGER.log(Level.SEVERE, msg);
throw new RuntimeException(msg);
}
@@ -181,7 +178,7 @@ public String getDisplayName() {
* @param configId the config id to get the arguments description for
* @return the description
*/
private String getArgsDescription(@AncestorInPath ItemGroup context, String configId) {
private String getArgsDescription(@AncestorInPath Item context, String configId) {
final PowerShellConfig config = ConfigFiles.getByIdOrNull(context, configId);
if (config != null) {
if (config.args != null && !config.args.isEmpty()) {
@@ -202,25 +199,16 @@ private String getArgsDescription(@AncestorInPath ItemGroup context, String conf
return "please select a valid script!";
}

@JavaScriptMethod
public List<Arg> getArgs(@AncestorInPath ItemGroup context, String configId) {
final PowerShellConfig config = ConfigFiles.getByIdOrNull(context, configId);
if (config != null) {
return config.args;
}
return Collections.emptyList();
}

/**
* validate that an existing config was chosen
*
* @param buildStepId the buildStepId
* @return
*/
public FormValidation doCheckBuildStepId(@AncestorInPath ItemGroup context, @QueryParameter String buildStepId) {
public HttpResponse doCheckBuildStepId(StaplerRequest req, @AncestorInPath Item context, @QueryParameter String buildStepId) {
final PowerShellConfig config = ConfigFiles.getByIdOrNull(context, buildStepId);
if (config != null) {
return FormValidation.ok(getArgsDescription(context, buildStepId));
return DetailLinkDescription.getDescription(req, context, buildStepId, getArgsDescription(context, buildStepId));
} else {
return FormValidation.error("you must select a valid powershell file");
}
@@ -14,11 +14,7 @@
import org.jenkinsci.plugins.managedscripts.ScriptConfig.Arg;
import org.jenkinsci.plugins.managedscripts.ScriptConfig.ScriptConfigProvider;
import org.jenkinsci.plugins.tokenmacro.TokenMacro;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.HttpResponse;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.bind.JavaScriptMethod;
import org.kohsuke.stapler.*;

import java.io.IOException;
import java.util.*;
@@ -76,7 +72,7 @@ public ScriptBuildStep(String buildStepId, ScriptBuildStepArgs scriptBuildStepAr
this.buildStepId = buildStepId;
this.tokenized = scriptBuildStepArgs != null ? scriptBuildStepArgs.tokenized : false;
List<String> l = null;
if (scriptBuildStepArgs != null && scriptBuildStepArgs.defineArgs && scriptBuildStepArgs.buildStepArgs != null){
if (scriptBuildStepArgs != null && scriptBuildStepArgs.defineArgs && scriptBuildStepArgs.buildStepArgs != null) {
l = new ArrayList<String>();
for (ArgValue arg : scriptBuildStepArgs.buildStepArgs) {
l.add(arg.arg);
@@ -125,12 +121,12 @@ public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListen
EnvVars env = build.getEnvironment(listener);
String data = buildStepConfig.content;

if(workingDir != null) {
if (workingDir != null) {
/*
* Copying temporary file to remote execution host
*/
dest = workingDir.createTextTempFile("build_step_template", ".sh", data, false);
LOGGER.log(Level.FINE, "Wrote script to " + Computer.currentComputer().getDisplayName() + ":" + dest.getRemote());
dest = workingDir.createTextTempFile("build_step_template", ".sh", data, false);
LOGGER.log(Level.FINE, "Wrote script to " + Computer.currentComputer().getDisplayName() + ":" + dest.getRemote());

/*
* Analyze interpreter line (and use the desired interpreter)
@@ -257,7 +253,7 @@ public int compare(Config o1, Config o2) {
* @param configId the config id to get the arguments description for
* @return the description
*/
private String getArgsDescription(@AncestorInPath ItemGroup context, String configId) {
private String getArgsDescription(@AncestorInPath Item context, String configId) {
final ScriptConfig config = ConfigFiles.getByIdOrNull(context, configId);
if (config != null) {
if (config.args != null && !config.args.isEmpty()) {
@@ -278,26 +274,17 @@ private String getArgsDescription(@AncestorInPath ItemGroup context, String conf
return "please select a valid script!";
}

@JavaScriptMethod
public List<Arg> getArgs(@AncestorInPath ItemGroup context, String configId) {
final ScriptConfig config = ConfigFiles.getByIdOrNull(context, configId);
if (config != null) {
return config.args;
}
return Collections.emptyList();
}

/**
* validate that an existing config was chosen
*
* @param context the context to search within for the configuration file with the given id
* @param buildStepId the buildStepId
* @return whether the config existts or not
*/
public FormValidation doCheckBuildStepId(@AncestorInPath ItemGroup context, @QueryParameter String buildStepId) {
public HttpResponse doCheckBuildStepId(StaplerRequest req, @AncestorInPath Item context, @QueryParameter String buildStepId) {
final ScriptConfig config = ConfigFiles.getByIdOrNull(context, buildStepId);
if (config != null) {
return FormValidation.ok(getArgsDescription(context, buildStepId));
return DetailLinkDescription.getDescription(req, context, buildStepId, getArgsDescription(context, buildStepId));
} else {
return FormValidation.error("you must select a valid script");
}
@@ -1,26 +1,24 @@
package org.jenkinsci.plugins.managedscripts;

import hudson.*;
import hudson.Extension;
import hudson.ExtensionList;
import hudson.FilePath;
import hudson.model.*;
import hudson.model.Queue;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.Builder;
import hudson.tasks.CommandInterpreter;
import hudson.util.FormValidation;

import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

import hudson.util.ListBoxModel;
import org.jenkinsci.lib.configprovider.ConfigProvider;
import org.jenkinsci.lib.configprovider.model.Config;
import org.jenkinsci.plugins.configfiles.ConfigFiles;
import org.jenkinsci.plugins.managedscripts.WinBatchConfig.Arg;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.bind.JavaScriptMethod;
import org.kohsuke.stapler.*;

import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
* A project that uses this builder can choose a build step from a list of predefined windows batch files that are used as command line scripts.
@@ -201,7 +199,7 @@ public int compare(Config o1, Config o2) {
* @param configId the config id to get the arguments description for
* @return the description
*/
private String getArgsDescription(@AncestorInPath ItemGroup context, String configId) {
private String getArgsDescription(@AncestorInPath Item context, String configId) {
final WinBatchConfig config = ConfigFiles.getByIdOrNull(context, configId);
if (config != null) {
if (config.args != null && !config.args.isEmpty()) {
@@ -222,22 +220,16 @@ private String getArgsDescription(@AncestorInPath ItemGroup context, String conf
return "please select a valid script!";
}

@JavaScriptMethod
public List<Arg> getArgs(@AncestorInPath ItemGroup context, String configId) {
final WinBatchConfig config = ConfigFiles.getByIdOrNull(context, configId);
return config.args;
}

/**
* validate that an existing config was chosen
*
* @param buildStepId the buildStepId
* @return
*/
public FormValidation doCheckBuildStepId(@AncestorInPath ItemGroup context, @QueryParameter String buildStepId) {
public HttpResponse doCheckBuildStepId(StaplerRequest req, @AncestorInPath Item context, @QueryParameter String buildStepId) {
final WinBatchConfig config = ConfigFiles.getByIdOrNull(context, buildStepId);
if (config != null) {
return FormValidation.ok(getArgsDescription(context, buildStepId));
return DetailLinkDescription.getDescription(req, context, buildStepId, getArgsDescription(context, buildStepId));
} else {
return FormValidation.error("you must select a valid batch file");
}
@@ -1,11 +1,5 @@
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">

<st:adjunct assumes="org.kohsuke.stapler.framework.prototype.prototype" includes="org.kohsuke.stapler.bind"/>

<st:once>
<script type="text/javascript" src="${rootURL}/plugin/managed-scripts/js/managed-scripts.js" />
</st:once>

<f:entry title="${%Script}" field="buildStepContent">

<f:entry field="buildStepId">
@@ -38,27 +32,4 @@
</f:block>
</f:entry>

<st:bind var="desc" value="${descriptor}"/>
<st:once>
<script type="text/javascript">
function ms_updateDetailLink(event) {
fp_initDetailLink('<j:out value="${rootURL}" />', event.currentTarget, '_.buildStepId');
}

function ms_initAllDetailLinks() {
console.log('init powershell links...');
var all = new Array();
all = document.getElementsByName('_.buildStepId');
console.log('all...'+all);
for(var i = 0; i &lt; all.length; i++) {
console.log('found: '+all.item(i));
fp_initDetailLink('<j:out value="${rootURL}" />', all.item(i), '_.buildStepId');
Event.stopObserving(all.item(i), 'change', ms_updateDetailLink);
Event.observe(all.item(i), 'change', ms_updateDetailLink);
}
}

Event.observe(window, 'load', ms_initAllDetailLinks);
</script>
</st:once>
</j:jelly>
@@ -1,10 +1,5 @@
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">

<st:adjunct assumes="org.kohsuke.stapler.framework.prototype.prototype" includes="org.kohsuke.stapler.bind"/>

<st:once>
<script type="text/javascript" src="${rootURL}/plugin/managed-scripts/js/managed-scripts.js" />
</st:once>
<f:entry title="${%Script}" field="buildStepContent">
<f:entry field="buildStepId">
<f:select />
@@ -38,27 +33,4 @@
</table>
</f:block>
</f:entry>
<st:bind var="desc" value="${descriptor}"/>
<st:once>
<script type="text/javascript">
function ms_updateDetailLink(event) {
fp_initDetailLink('<j:out value="${rootURL}" />', event.currentTarget, '_.buildStepId');
}

function ms_initAllDetailLinks() {
console.log('init script links...');
var all = new Array();
all = document.getElementsByName('_.buildStepId');
console.log('all...'+all);
for(var i = 0; i &lt; all.length; i++) {
console.log('found: '+all.item(i));
fp_initDetailLink('<j:out value="${rootURL}" />', all.item(i), '_.buildStepId');
Event.stopObserving(all.item(i), 'change', ms_updateDetailLink);
Event.observe(all.item(i), 'change', ms_updateDetailLink);
}
}

Event.observe(window, 'load', ms_initAllDetailLinks);
</script>
</st:once>
</j:jelly>
Oops, something went wrong.

0 comments on commit f691a78

Please sign in to comment.