diff --git a/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/mf/ReadManifestStepExecution.java b/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/mf/ReadManifestStepExecution.java index 2d616187..1f9b3d5b 100644 --- a/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/mf/ReadManifestStepExecution.java +++ b/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/conf/mf/ReadManifestStepExecution.java @@ -85,7 +85,7 @@ private SimpleManifest parseFile(String file) throws IOException, InterruptedExc } String lcName = path.getName().toLowerCase(); if(lcName.endsWith(".jar") || lcName.endsWith(".war") || lcName.endsWith(".ear")) { - Map mf = path.act(new UnZipStepExecution.UnZipFileCallable(listener, ws, "META-INF/MANIFEST.MF", true, "UTF-8")); + Map mf = path.act(new UnZipStepExecution.UnZipFileCallable(listener, ws, "META-INF/MANIFEST.MF", true, "UTF-8", false)); String text = mf.get("META-INF/MANIFEST.MF"); if (isBlank(text)) { throw new FileNotFoundException(path.getRemote() + " does not seem to contain a manifest."); diff --git a/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/zip/UnZipStep.java b/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/zip/UnZipStep.java index 31f7c5d2..865cd9de 100644 --- a/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/zip/UnZipStep.java +++ b/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/zip/UnZipStep.java @@ -57,6 +57,7 @@ public class UnZipStep extends Step { private String glob; private boolean test = false; private boolean read = false; + private boolean quiet = false; @DataBoundConstructor public UnZipStep(String zipFile) throws Descriptor.FormException { @@ -165,10 +166,10 @@ public boolean isRead() { public void setRead(boolean read) { this.read = read; } - + /** * Get the charset to use when unzipping the zip file. E.g. UTF-8 - * + * * String version = unzip zipFile: 'example.zip', glob: 'version.txt', read: true, charset: UTF-8 * * @return String specifying the charset, defaults to UTF-8 @@ -181,7 +182,7 @@ public String getCharset() /** * Set the charset to use when unzipping the zip file. - * + * * String version = unzip zipFile: 'example.zip', glob: 'version.txt', read: true , charset: UTF-8 * * @param charset @@ -193,6 +194,29 @@ public void setCharset(String charset) this.charset = (charset.trim().isEmpty()) ? "UTF-8" : charset; } + /** + * Suppress the verbose output that logs every single file that is dealt with. + * E.g. + * unzip zipFile: 'example.zip', glob: 'version.txt', quiet: true + * + * @return if verbose logging should be suppressed + */ + public boolean isQuiet() { + return quiet; + } + + /** + * Suppress the verbose output that logs every single file that is dealt with. + * E.g. + * unzip zipFile: 'example.zip', glob: 'version.txt', quiet: true + * + * @param quiet if verbose logging should be suppressed + */ + @DataBoundSetter + public void setQuiet(boolean quiet) { + this.quiet = quiet; + } + @Override public StepExecution start(StepContext context) throws Exception { return new UnZipStepExecution(this, context); diff --git a/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/zip/UnZipStepExecution.java b/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/zip/UnZipStepExecution.java index 82803857..c7667db4 100644 --- a/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/zip/UnZipStepExecution.java +++ b/src/main/java/org/jenkinsci/plugins/pipeline/utility/steps/zip/UnZipStepExecution.java @@ -86,7 +86,7 @@ protected Object run() throws Exception { if (!StringUtils.isBlank(step.getDir())) { destination = ws.child(step.getDir()); } - return source.act(new UnZipFileCallable(listener, destination, step.getGlob(), step.isRead(),step.getCharset())); + return source.act(new UnZipFileCallable(listener, destination, step.getGlob(), step.isRead(),step.getCharset(),step.isQuiet())); } private Boolean test() throws IOException, InterruptedException { @@ -113,14 +113,16 @@ public static class UnZipFileCallable extends MasterToSlaveFileCallable invoke(File zipFile, VirtualChannel channel) throws I Charset charsetForZip = Charset.forName(charset); zip = new ZipFile(zipFile, charsetForZip); Enumeration entries = zip.entries(); + Integer fileCount = 0; while (entries.hasMoreElements()) { ZipEntry entry = entries.nextElement(); if (doGlob && !matches(entry.getName(), glob)) { @@ -148,11 +151,15 @@ public Map invoke(File zipFile, VirtualChannel channel) throws I f.mkdirs(); } } else { + fileCount++; + if (!read) { - logger.print("Extracting: "); - logger.print(entry.getName()); - logger.print(" -> "); - logger.println(f.getRemote()); + if (!quiet) { + logger.print("Extracting: "); + logger.print(entry.getName()); + logger.print(" -> "); + logger.println(f.getRemote()); + } /* It is not by all means required to close the input streams of the zip file because they are @@ -165,8 +172,10 @@ public Map invoke(File zipFile, VirtualChannel channel) throws I outputStream.flush(); } } else { - logger.print("Reading: "); - logger.println(entry.getName()); + if (!quiet) { + logger.print("Reading: "); + logger.println(entry.getName()); + } try (InputStream is = zip.getInputStream(entry)) { strMap.put(entry.getName(), IOUtils.toString(is, Charset.defaultCharset())); @@ -175,8 +184,14 @@ public Map invoke(File zipFile, VirtualChannel channel) throws I } } if (read) { + logger.print("Read: "); + logger.print(fileCount); + logger.println(" files"); return strMap; } else { + logger.print("Extracted: "); + logger.print(fileCount); + logger.println(" files"); return null; } } finally { diff --git a/src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/zip/UnZipStep/config.groovy b/src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/zip/UnZipStep/config.groovy index 2e830863..92f1a907 100644 --- a/src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/zip/UnZipStep/config.groovy +++ b/src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/zip/UnZipStep/config.groovy @@ -48,5 +48,6 @@ f.entry(field: 'test', title: _('Test the archive')) { f.entry(field: 'read', title: _('Read the file contents')) { f.checkbox() } - - +f.entry(field: 'quiet', title: _('Suppress logging of each file')) { + f.checkbox() +} diff --git a/src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/zip/UnZipStep/help-quiet.html b/src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/zip/UnZipStep/help-quiet.html new file mode 100644 index 00000000..3eabad68 --- /dev/null +++ b/src/main/resources/org/jenkinsci/plugins/pipeline/utility/steps/zip/UnZipStep/help-quiet.html @@ -0,0 +1,30 @@ + +

+ Suppress the verbose output that logs every single file that is dealt with. + E.g. + + unzip zipFile: 'example.zip', quiet: true + +

diff --git a/src/test/java/org/jenkinsci/plugins/pipeline/utility/steps/zip/UnZipStepTest.java b/src/test/java/org/jenkinsci/plugins/pipeline/utility/steps/zip/UnZipStepTest.java index 90638b10..d2bb3093 100644 --- a/src/test/java/org/jenkinsci/plugins/pipeline/utility/steps/zip/UnZipStepTest.java +++ b/src/test/java/org/jenkinsci/plugins/pipeline/utility/steps/zip/UnZipStepTest.java @@ -61,6 +61,7 @@ public void configRoundTrip() throws Exception { step.setDir("base/"); step.setGlob("**/*.zip"); step.setRead(true); + step.setQuiet(false); step.setCharset(""); UnZipStep step2 = new StepConfigTester(j).configRoundTrip(step); @@ -222,4 +223,56 @@ public void testZipTestingOkayZip() throws Exception { "}", true)); j.assertBuildStatusSuccess(p.scheduleBuild2(0)); } + + @Test + public void unzipQuiet() throws Exception { + WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p"); + p.setDefinition(new CpsFlowDefinition( + "node('slaves') {\n" + + " dir('zipIt') {\n" + + " writeFile file: 'hello.txt', text: 'Hello World!'\n" + + " writeFile file: 'hello.dat', text: 'Hello World!'\n" + + " dir('two') {\n" + + " writeFile file: 'hello.txt', text: 'Hello World2!'\n" + + " }\n" + + " zip zipFile: '../hello.zip'\n" + + " }\n" + + " dir('unzip') {\n" + + " unzip zipFile: '../hello.zip', quiet: true\n" + + " String txt = readFile 'hello.txt'\n" + + " echo \"Reading: ${txt}\"\n" + + " txt = readFile 'two/hello.txt'\n" + + " echo \"Reading: ${txt}\"\n" + + " }\n" + + "}", true)); + WorkflowRun run = j.assertBuildStatusSuccess(p.scheduleBuild2(0)); + j.assertLogNotContains("Extracting: hello.txt ->", run); + j.assertLogNotContains("Extracting: two/hello.txt ->", run); + j.assertLogNotContains("Extracting: hello.dat ->", run); + j.assertLogContains("Extracted: 3 files", run); + j.assertLogContains("Reading: Hello World!", run); + j.assertLogContains("Reading: Hello World2!", run); + } + + @Test + public void unzipQuietReading() throws Exception { + WorkflowJob p = j.jenkins.createProject(WorkflowJob.class, "p"); + p.setDefinition(new CpsFlowDefinition( + "node('slaves') {\n" + + " dir('zipIt') {\n" + + " writeFile file: 'hello.txt', text: 'Hello World!'\n" + + " writeFile file: 'hello.dat', text: 'Hello World!'\n" + + " zip zipFile: '../hello.zip'\n" + + " }\n" + + " dir('unzip') {\n" + + " def txt = unzip zipFile: '../hello.zip', quiet: true, read: true\n" + + " echo \"Text: ${txt.values().join('\\n')}\"\n" + + " }\n" + + "}", false)); //For some reason the Sandbox forbids invoking Map.values? + WorkflowRun run = j.assertBuildStatusSuccess(p.scheduleBuild2(0)); + j.assertLogNotContains("Reading: hello.txt", run); + j.assertLogNotContains("Reading: hello.dat", run); + j.assertLogContains("Read: 2 files", run); + j.assertLogContains("Text: Hello World!", run); + } }