Skip to content

Commit

Permalink
Merge pull request #30 from jglick/filename
Browse files Browse the repository at this point in the history
Bind param_FILENAME whenever possible
  • Loading branch information
jglick authored Mar 1, 2021
2 parents cdb61e2 + c46eef9 commit fec41d2
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ protected AbstractFileParameterDefinition(String name) {
}
src.delete();
p.setDescription(getDescription());
p.filename = src.getName();
return p;
} catch (ServletException | IOException x) {
throw new RuntimeException(x);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,9 @@
/**
* Implement either {@link #open} and/or {@link #createTempFile}.
*/
abstract class AbstractFileParameterValue extends ParameterValue {
public abstract class AbstractFileParameterValue extends ParameterValue {

@CheckForNull public String filename;

protected AbstractFileParameterValue(String name) {
super(name);
Expand Down Expand Up @@ -88,5 +90,12 @@ public void doDownload(@AncestorInPath Run<?,?> build, StaplerResponse rsp) thro
}

// TODO equals/hashCode

@Override public void buildEnvironment(Run<?, ?> build, EnvVars env) {
super.buildEnvironment(build, env);
if (filename != null) {
env.put(name + "_FILENAME", filename);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ public final class Base64FileParameterValue extends AbstractFileParameterValue {

@DataBoundSetter public void setFile(FileItem file) throws IOException {
base64 = Base64.getEncoder().encodeToString(IOUtils.toByteArray(file.getInputStream()));
filename = file.getName();
file.delete();
}

Expand All @@ -60,6 +61,7 @@ public final class Base64FileParameterValue extends AbstractFileParameterValue {
}

@Override public void buildEnvironment(Run<?, ?> build, EnvVars env) {
super.buildEnvironment(build, env);
env.put(name, base64);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public final class StashedFileParameterValue extends AbstractFileParameterValue

@DataBoundConstructor public StashedFileParameterValue(String name, FileItem file) throws IOException {
this(name, file.getInputStream());
filename = file.getName();
file.delete();
}

Expand All @@ -61,6 +62,7 @@ public final class StashedFileParameterValue extends AbstractFileParameterValue
}

@Override public void buildEnvironment(Run<?, ?> build, EnvVars env) {
super.buildEnvironment(build, env);
if (tmp != null) {
TaskListener listener = TaskListener.NULL; // TODO no option to print to build log
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ THE SOFTWARE.

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:entry field="name" title="${%Name}" help="/help/parameter/name.html">
<f:entry field="name" title="${%Name}">
<f:textbox/>
</f:entry>
<f:entry field="description" title="${%Description}" help="/help/parameter/description.html">
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<p>
The name of the parameter.
Depending on the type of parameter, this may or may not be bound as an environment variable during the build.
</p>
<p>
If a local filename was given, an environment variable <code><i>paramname</i>_FILENAME</code> will also be set.
If the build is triggered via the CLI, the variable will not be set.
</p>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ THE SOFTWARE.
<j:invokeStatic var="encodedName" className="hudson.Util" method="rawEncode">
<j:arg value="${it.name}"/>
</j:invokeStatic>
<a href="parameter/${encodedName}/download">${%download}</a>
<a href="parameter/${encodedName}/download">${%download} <j:if test="${it.filename != null}"><code>${it.filename}</code></j:if></a>
</f:entry>
</j:jelly>
Original file line number Diff line number Diff line change
Expand Up @@ -65,34 +65,34 @@ public class AbstractFileParameterDefinitionTest {
@Test public void cli() throws Exception {
WorkflowJob p = r.createProject(WorkflowJob.class, "myjob");
p.addProperty(new ParametersDefinitionProperty(new Base64FileParameterDefinition("FILE")));
p.setDefinition(new CpsFlowDefinition("echo(/received: $FILE/)", true));
p.setDefinition(new CpsFlowDefinition("echo(/received $env.FILE_FILENAME: $FILE/)", true));
assertThat(new CLICommandInvoker(r, "build").
withStdin(new ByteArrayInputStream("uploaded content here".getBytes())).
invokeWithArgs("-f", "-p", "FILE=", "myjob"),
CLICommandInvoker.Matcher.succeeded());
WorkflowRun b = p.getBuildByNumber(1);
assertNotNull(b);
r.assertLogContains("received: dXBsb2FkZWQgY29udGVudCBoZXJl", b);
r.assertLogContains("received null: dXBsb2FkZWQgY29udGVudCBoZXJl", b);
}

@Test public void rest() throws Exception {
r.jenkins.setSecurityRealm(r.createDummySecurityRealm());
r.jenkins.setAuthorizationStrategy(new MockAuthorizationStrategy().grant(Jenkins.ADMINISTER).everywhere().to("admin"));
WorkflowJob p = r.createProject(WorkflowJob.class, "myjob");
p.addProperty(new ParametersDefinitionProperty(new Base64FileParameterDefinition("FILE")));
p.setDefinition(new CpsFlowDefinition("echo(/received: $FILE/)", true));
p.setDefinition(new CpsFlowDefinition("echo(/received $FILE_FILENAME: $FILE/)", true));
// Like: curl -u $auth -F FILE=@/tmp/f $jenkins/job/myjob/buildWithParameters
WebRequest req = new WebRequest(new URL(r.getURL() + "job/myjob/buildWithParameters"), HttpMethod.POST);
File f = tmp.newFile();
FileUtils.write(f, "uploaded content here", "UTF-8");
req.setEncodingType(FormEncodingType.MULTIPART);
req.setRequestParameters(Collections.<NameValuePair>singletonList(new KeyDataPair("FILE", f, "FILE", "text/plain", "UTF-8")));
req.setRequestParameters(Collections.<NameValuePair>singletonList(new KeyDataPair("FILE", f, "myfile.txt", "text/plain", "UTF-8")));
User.getById("admin", true); // TODO workaround for https://github.com/jenkinsci/jenkins-test-harness/pull/273
r.createWebClient().withBasicApiToken("admin").getPage(req);
r.waitUntilNoActivity();
WorkflowRun b = p.getBuildByNumber(1);
assertNotNull(b);
r.assertLogContains("received: dXBsb2FkZWQgY29udGVudCBoZXJl", b);
r.assertLogContains("received myfile.txt: dXBsb2FkZWQgY29udGVudCBoZXJl", b);
}

}

0 comments on commit fec41d2

Please sign in to comment.