diff --git a/README.md b/README.md index a95b585..4c0c847 100644 --- a/README.md +++ b/README.md @@ -20,13 +20,13 @@ You can now declare and use file parameters via Declarative Pipeline syntax: pipeline { agent any parameters { - base64File(name: 'small') - stashedFile(name: 'large') + base64File 'small' + stashedFile 'large' } stages { stage('Example') { steps { - withFileParameter(name: 'small') { + withFileParameter('small') { sh 'cat $small' } unstash 'large' diff --git a/src/main/java/io/jenkins/plugins/file_parameters/AbstractFileParameterDefinition.java b/src/main/java/io/jenkins/plugins/file_parameters/AbstractFileParameterDefinition.java index d7e3309..97bca44 100644 --- a/src/main/java/io/jenkins/plugins/file_parameters/AbstractFileParameterDefinition.java +++ b/src/main/java/io/jenkins/plugins/file_parameters/AbstractFileParameterDefinition.java @@ -32,14 +32,37 @@ import java.io.InputStream; import java.util.Base64; import javax.servlet.ServletException; +import jenkins.model.Jenkins; import net.sf.json.JSONObject; import org.apache.commons.fileupload.FileItem; +import org.kohsuke.stapler.DataBoundSetter; import org.kohsuke.stapler.StaplerRequest; abstract class AbstractFileParameterDefinition extends ParameterDefinition { - protected AbstractFileParameterDefinition(String name, String description) { - super(name, description); + private String description; + + protected AbstractFileParameterDefinition(String name) { + super(name); + } + + @Override public String getDescription() { + return description; + } + + /** + * Allows parameters to have a single-arg constructor. + */ + @DataBoundSetter public void setDescription(String description) { + this.description = description; + } + + @Override public String getFormattedDescription() { + try { + return Jenkins.get().getMarkupFormatter().translate(getDescription()); + } catch (IOException x) { + return ""; + } } protected abstract Class valueType(); diff --git a/src/main/java/io/jenkins/plugins/file_parameters/Base64FileParameterDefinition.java b/src/main/java/io/jenkins/plugins/file_parameters/Base64FileParameterDefinition.java index 6fc3aba..07e35d8 100644 --- a/src/main/java/io/jenkins/plugins/file_parameters/Base64FileParameterDefinition.java +++ b/src/main/java/io/jenkins/plugins/file_parameters/Base64FileParameterDefinition.java @@ -33,10 +33,10 @@ public final class Base64FileParameterDefinition extends AbstractFileParameterDefinition { - @DataBoundConstructor public Base64FileParameterDefinition(String name, String description) { - super(name, description); + @DataBoundConstructor public Base64FileParameterDefinition(String name) { + super(name); } - + @Override protected Class valueType() { return Base64FileParameterValue.class; } diff --git a/src/main/java/io/jenkins/plugins/file_parameters/StashedFileParameterDefinition.java b/src/main/java/io/jenkins/plugins/file_parameters/StashedFileParameterDefinition.java index ee0f19b..a0998ab 100644 --- a/src/main/java/io/jenkins/plugins/file_parameters/StashedFileParameterDefinition.java +++ b/src/main/java/io/jenkins/plugins/file_parameters/StashedFileParameterDefinition.java @@ -33,8 +33,8 @@ public final class StashedFileParameterDefinition extends AbstractFileParameterDefinition { - @DataBoundConstructor public StashedFileParameterDefinition(String name, String description) { - super(name, description); + @DataBoundConstructor public StashedFileParameterDefinition(String name) { + super(name); } @Override protected Class valueType() { diff --git a/src/main/resources/io/jenkins/plugins/file_parameters/Base64FileParameterDefinition/help.html b/src/main/resources/io/jenkins/plugins/file_parameters/Base64FileParameterDefinition/help.html index 823f65c..e5a7081 100644 --- a/src/main/resources/io/jenkins/plugins/file_parameters/Base64FileParameterDefinition/help.html +++ b/src/main/resources/io/jenkins/plugins/file_parameters/Base64FileParameterDefinition/help.html @@ -8,7 +8,7 @@ pipeline { agent any parameters { - base64File(name: 'FILE') + base64File 'FILE' } stages { stage('Example') { diff --git a/src/main/resources/io/jenkins/plugins/file_parameters/FileParameterWrapper/help.html b/src/main/resources/io/jenkins/plugins/file_parameters/FileParameterWrapper/help.html index 435e20c..38a6f90 100644 --- a/src/main/resources/io/jenkins/plugins/file_parameters/FileParameterWrapper/help.html +++ b/src/main/resources/io/jenkins/plugins/file_parameters/FileParameterWrapper/help.html @@ -8,7 +8,7 @@ pipeline { agent any parameters { - base64File(name: 'THEFILE') + base64File 'THEFILE' } stages { stage('Example') { @@ -29,7 +29,7 @@ pipeline { agent any parameters { - base64File(name: 'THEFILE') + base64File 'THEFILE' } stages { stage('Example') { diff --git a/src/main/resources/io/jenkins/plugins/file_parameters/StashedFileParameterDefinition/help.html b/src/main/resources/io/jenkins/plugins/file_parameters/StashedFileParameterDefinition/help.html index 5c9f84b..5f0d3e5 100644 --- a/src/main/resources/io/jenkins/plugins/file_parameters/StashedFileParameterDefinition/help.html +++ b/src/main/resources/io/jenkins/plugins/file_parameters/StashedFileParameterDefinition/help.html @@ -7,7 +7,7 @@ pipeline { agent any parameters { - stashedFile(name: 'assets.zip') + stashedFile 'assets.zip' } stages { stage('Example') { diff --git a/src/test/java/io/jenkins/plugins/file_parameters/AbstractFileParameterDefinitionTest.java b/src/test/java/io/jenkins/plugins/file_parameters/AbstractFileParameterDefinitionTest.java index 23c81de..5838bb9 100644 --- a/src/test/java/io/jenkins/plugins/file_parameters/AbstractFileParameterDefinitionTest.java +++ b/src/test/java/io/jenkins/plugins/file_parameters/AbstractFileParameterDefinitionTest.java @@ -47,7 +47,7 @@ public class AbstractFileParameterDefinitionTest { // adapted from BuildCommandTest.fileParameter @Test public void cli() throws Exception { WorkflowJob p = r.createProject(WorkflowJob.class, "myjob"); - p.addProperty(new ParametersDefinitionProperty(new Base64FileParameterDefinition("FILE", null))); + p.addProperty(new ParametersDefinitionProperty(new Base64FileParameterDefinition("FILE"))); p.setDefinition(new CpsFlowDefinition("echo(/received: $FILE/)", true)); assertThat(new CLICommandInvoker(r, "build"). withStdin(new ByteArrayInputStream("uploaded content here".getBytes())). diff --git a/src/test/java/io/jenkins/plugins/file_parameters/FileParameterWrapperTest.java b/src/test/java/io/jenkins/plugins/file_parameters/FileParameterWrapperTest.java index 96e1199..e3e1be1 100644 --- a/src/test/java/io/jenkins/plugins/file_parameters/FileParameterWrapperTest.java +++ b/src/test/java/io/jenkins/plugins/file_parameters/FileParameterWrapperTest.java @@ -35,7 +35,6 @@ import org.jenkinsci.plugins.workflow.job.WorkflowJob; import org.jenkinsci.plugins.workflow.job.WorkflowRun; import org.junit.ClassRule; -import org.junit.Ignore; import org.junit.Test; import static org.junit.Assert.*; import org.junit.Rule; @@ -51,7 +50,7 @@ public class FileParameterWrapperTest { @Test public void base64() throws Exception { r.createSlave("remote", null, null); WorkflowJob p = r.createProject(WorkflowJob.class, "myjob"); - p.addProperty(new ParametersDefinitionProperty(new Base64FileParameterDefinition("FILE", null))); + p.addProperty(new ParametersDefinitionProperty(new Base64FileParameterDefinition("FILE"))); p.setDefinition(new CpsFlowDefinition("node('remote') {withFileParameter('FILE') {echo(/loaded '${readFile(FILE).toUpperCase(Locale.ROOT)}' from $FILE/)}}", true)); assertThat(new CLICommandInvoker(r, "build"). withStdin(new ByteArrayInputStream("uploaded content here".getBytes())). @@ -65,11 +64,11 @@ public class FileParameterWrapperTest { @Test public void base64UndefinedFail() throws Exception { r.createSlave("remote", null, null); WorkflowJob p = r.createProject(WorkflowJob.class, "myjob"); - p.addProperty(new ParametersDefinitionProperty(new Base64FileParameterDefinition("FILE", null))); + p.addProperty(new ParametersDefinitionProperty(new Base64FileParameterDefinition("FILE"))); String pipeline = "pipeline {\n" + " agent any\n" + " parameters {\n" + - " base64File(name:'FILE')\n" + + " base64File 'FILE'\n" + " }\n" + " stages {\n" + " stage('Example') {\n" + @@ -91,11 +90,11 @@ public class FileParameterWrapperTest { @Test public void base64WithAllowNoFile() throws Exception { r.createSlave("remote", null, null); WorkflowJob p = r.createProject(WorkflowJob.class, "myjob"); - p.addProperty(new ParametersDefinitionProperty(new Base64FileParameterDefinition("FILE", null))); + p.addProperty(new ParametersDefinitionProperty(new Base64FileParameterDefinition("FILE"))); String pipeline = "pipeline {\n" + " agent any\n" + " parameters {\n" + - " base64File(name:'FILE')\n" + + " base64File 'FILE'\n" + " }\n" + " stages {\n" + " stage('Example') {\n" + @@ -122,7 +121,7 @@ public class FileParameterWrapperTest { String pipeline = "pipeline {\n" + " agent any\n" + " parameters {\n" + - " base64File (name: 'FILE')\n" + + " base64File 'FILE'\n" + " }\n" + " stages {\n" + " stage('Example') {\n" + @@ -158,7 +157,7 @@ public class FileParameterWrapperTest { @Test public void stashed() throws Exception { r.createSlave("remote", null, null); WorkflowJob p = r.createProject(WorkflowJob.class, "myjob"); - p.addProperty(new ParametersDefinitionProperty(new StashedFileParameterDefinition("FILE", null))); + p.addProperty(new ParametersDefinitionProperty(new StashedFileParameterDefinition("FILE"))); p.setDefinition(new CpsFlowDefinition("node('remote') {withFileParameter('FILE') {echo(/loaded '${readFile(FILE).toUpperCase(Locale.ROOT)}' from $FILE/)}}", true)); assertThat(new CLICommandInvoker(r, "build"). withStdin(new ByteArrayInputStream("uploaded content here".getBytes())). @@ -176,7 +175,7 @@ public class FileParameterWrapperTest { String pipeline = "pipeline {\n" + " agent any\n" + " parameters {\n" + - " stashedFile(name:'FILE')\n" + + " stashedFile 'FILE'\n" + " }\n" + " stages {\n" + " stage('Example') {\n" +