Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make FileParameterWrapper work with StashedFileParameterValue #8

Merged
merged 2 commits into from
Jan 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ pipeline {
- [ ] stashed file parameter (suitable for larger files used in Pipeline)
- [X] implementation
- [X] inline help text
- [X] `withFileParameter` compatibility (manual test)
- [X] manual test
- [X] `withFileParameter` compatibility
- [ ] manual test
- [X] automated test
- [ ] sanity check against `artifact-manager-s3`
- [ ] archived file parameter (compatible with freestyle, and suitable if you want to ensure parameters are kept after the build ends)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,24 +24,64 @@

package io.jenkins.plugins.file_parameters;

import edu.umd.cs.findbugs.annotations.CheckForNull;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Util;
import hudson.model.ParameterValue;
import hudson.model.Run;
import hudson.model.TaskListener;
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.commons.io.IOUtils;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.StaplerResponse;

/**
* Implement either {@link #open} and/or {@link #createTempFile}.
*/
abstract class AbstractFileParameterValue extends ParameterValue {

protected AbstractFileParameterValue(String name) {
super(name);
}

protected abstract InputStream open() throws IOException;
protected InputStream open(@CheckForNull Run<?,?> build) throws IOException, InterruptedException {
assert Util.isOverridden(AbstractFileParameterValue.class, getClass(), "createTempFile", Run.class, FilePath.class, EnvVars.class, Launcher.class, TaskListener.class);
if (build == null) {
throw new IOException("Cannot operate outside of a build context");
}
FilePath tempDir = new FilePath(Util.createTempDir());
FilePath f = createTempFile(build, tempDir, new EnvVars(EnvVars.masterEnvVars), new Launcher.LocalLauncher(TaskListener.NULL), TaskListener.NULL);
return new FilterInputStream(f.read()) {
@Override
public void close() throws IOException {
super.close();
try {
tempDir.deleteRecursive();
} catch (InterruptedException x) {
throw new IOException(x);
}
}
};
}

protected FilePath createTempFile(@NonNull Run<?,?> build, @NonNull FilePath tempDir, @NonNull EnvVars env, @NonNull Launcher launcher, @NonNull TaskListener listener) throws IOException, InterruptedException {
assert Util.isOverridden(AbstractFileParameterValue.class, getClass(), "open", Run.class);
FilePath f = tempDir.createTempFile(name, null);
try (InputStream is = open(build)) {
f.copyFrom(is);
}
return f;
}

public void doDownload(StaplerResponse rsp) throws IOException {
public void doDownload(@AncestorInPath Run<?,?> build, StaplerResponse rsp) throws Exception {
rsp.setContentType("application/octet-stream");
try (InputStream is = open(); OutputStream os = rsp.getOutputStream()) {
try (InputStream is = open(build); OutputStream os = rsp.getOutputStream()) {
IOUtils.copy(is, os);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public final class Base64FileParameterValue extends AbstractFileParameterValue {
return base64;
}

@Override protected InputStream open() throws IOException {
@Override protected InputStream open(Run<?, ?> build) throws IOException {
return new ByteArrayInputStream(Base64.getDecoder().decode(base64));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,7 @@ public void setAllowNoFile(boolean allowNoFile) {
throw new AbortException("Missing workspace or could not make temp dir");
}
tempDir.mkdirs();
FilePath f = tempDir.createTempFile(name, null);
try (InputStream is = ((AbstractFileParameterValue) pv).open()) {
f.copyFrom(is);
}
FilePath f = ((AbstractFileParameterValue) pv).createTempFile(build, tempDir, initialEnvironment, launcher, listener);
context.env(name, f.getRemote());
context.setDisposer(new Delete(f.getRemote()));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,9 @@ public final class StashedFileParameterValue extends AbstractFileParameterValue
}
}

@Override protected InputStream open() throws IOException {
throw new IOException(); // TODO StashManager.unstash to a temp dir
@Override protected FilePath createTempFile(Run<?, ?> build, FilePath tempDir, EnvVars env, Launcher launcher, TaskListener listener) throws IOException, InterruptedException {
StashManager.unstash(build, name, tempDir, launcher, env, listener);
return tempDir.child(name);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,11 @@ 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-STASH", null)));
p.setDefinition(new CpsFlowDefinition("node('remote') {" +
" unstash \"FILE-STASH\"\n" +
" echo(/loaded '${readFile(\"FILE-STASH\").toUpperCase(Locale.ROOT)}'/)}", true));

p.addProperty(new ParametersDefinitionProperty(new StashedFileParameterDefinition("FILE", null)));
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())).
invokeWithArgs("-f", "-p", "FILE-STASH=", "myjob"),
invokeWithArgs("-f", "-p", "FILE=", "myjob"),
CLICommandInvoker.Matcher.succeeded());
WorkflowRun b = p.getBuildByNumber(1);
assertNotNull(b);
Expand All @@ -179,13 +176,14 @@ public class FileParameterWrapperTest {
String pipeline = "pipeline {\n" +
" agent any\n" +
" parameters {\n" +
" stashedFile(name:'FILE-STASH')\n" +
" stashedFile(name:'FILE')\n" +
" }\n" +
" stages {\n" +
" stage('Example') {\n" +
" steps {\n" +
" unstash \"FILE-STASH\"\n" +
" echo(/loaded '${readFile(\"./FILE-STASH\").toUpperCase(Locale.ROOT)}'/) \n" +
" withFileParameter('FILE') {\n" +
" echo(/loaded '${readFile(FILE).toUpperCase(Locale.ROOT)}'/)\n" +
" }\n" +
" }\n" +
" }\n" +
" }\n" +
Expand All @@ -198,13 +196,13 @@ public class FileParameterWrapperTest {
r.assertBuildStatus(Result.FAILURE, run);
ParametersDefinitionProperty pdp = p.getProperty(ParametersDefinitionProperty.class);
assertNotNull("parameters definition property is null", pdp);
ParameterDefinition pd = pdp.getParameterDefinition( "FILE-STASH");
ParameterDefinition pd = pdp.getParameterDefinition("FILE");
assertNotNull("parameters definition is null", pd);
assertEquals("parameter not type Base64FileParameterDefinition", StashedFileParameterDefinition.class, pd.getClass());

assertThat(new CLICommandInvoker(r, "build").
withStdin(new ByteArrayInputStream("uploaded content here".getBytes())).
invokeWithArgs("-f", "-p", "FILE-STASH=", "myjob"),
invokeWithArgs("-f", "-p", "FILE=", "myjob"),
CLICommandInvoker.Matcher.succeeded());
WorkflowRun b = p.getBuildByNumber(2);
assertNotNull(b);
Expand Down