Skip to content

Commit

Permalink
Merge pull request #10 from jglick/ctor
Browse files Browse the repository at this point in the history
Use a simplified constructor for prettier Declarative syntax
  • Loading branch information
jglick authored Jan 30, 2021
2 parents 53d59b0 + 404fdb5 commit 70e69d5
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 24 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends AbstractFileParameterValue> valueType();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends AbstractFileParameterValue> valueType() {
return Base64FileParameterValue.class;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<? extends AbstractFileParameterValue> valueType() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
pipeline {
agent any
parameters {
base64File(name: 'FILE')
base64File 'FILE'
}
stages {
stage('Example') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
pipeline {
agent any
parameters {
base64File(name: 'THEFILE')
base64File 'THEFILE'
}
stages {
stage('Example') {
Expand All @@ -29,7 +29,7 @@
pipeline {
agent any
parameters {
base64File(name: 'THEFILE')
base64File 'THEFILE'
}
stages {
stage('Example') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
pipeline {
agent any
parameters {
stashedFile(name: 'assets.zip')
stashedFile 'assets.zip'
}
stages {
stage('Example') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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())).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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())).
Expand All @@ -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" +
Expand All @@ -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" +
Expand All @@ -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" +
Expand Down Expand Up @@ -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())).
Expand All @@ -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" +
Expand Down

0 comments on commit 70e69d5

Please sign in to comment.