From 776b27c9aef44a066fdb11b48bb8558c7d52ae9f Mon Sep 17 00:00:00 2001 From: Greg Swift Date: Wed, 13 Apr 2022 09:55:35 -0500 Subject: [PATCH 1/6] [JENKINS-51913] Create specific exception for ant file mask no matches found --- core/src/main/java/hudson/FilePath.java | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/hudson/FilePath.java b/core/src/main/java/hudson/FilePath.java index e4172a306a91..8ddaeb9d1617 100644 --- a/core/src/main/java/hudson/FilePath.java +++ b/core/src/main/java/hudson/FilePath.java @@ -3054,6 +3054,17 @@ public String validateAntFileMask(final String fileMasks, final boolean caseSens @SuppressFBWarnings(value = "MS_SHOULD_BE_FINAL", justification = "for script console") public static int VALIDATE_ANT_FILE_MASK_BOUND = SystemProperties.getInteger(FilePath.class.getName() + ".VALIDATE_ANT_FILE_MASK_BOUND", 10000); + /** + * Provide an explicit exception {@link InterruptedException} for when no matching ant file mask matches are found + */ + public static class FileMaskNoMatchesFoundException extends InterruptedException { + private FileMaskNoMatchesFoundException(String message) { + super(message); + } + + private static final long serialVersionUID = 1L; + } + /** * Validates the ant file mask (like "foo/bar/*.txt, zot/*.jar") against this directory, and try to point out the problem. * This performs only a bounded number of operations. @@ -3223,7 +3234,7 @@ class Cancel extends RuntimeException {} if (ds.getIncludedFilesCount() != 0 || ds.getIncludedDirsCount() != 0) { return true; } else { - throw (InterruptedException) new InterruptedException("no matches found within " + bound).initCause(c); + throw (FileMaskNoMatchesFoundException) new FileMaskNoMatchesFoundException("no matches found within " + bound).initCause(c); } } return ds.getIncludedFilesCount() != 0 || ds.getIncludedDirsCount() != 0; From 187717abd4271e9be70bbd0ba1d84b79cd96ce48 Mon Sep 17 00:00:00 2001 From: Greg Swift Date: Wed, 13 Apr 2022 10:20:52 -0500 Subject: [PATCH 2/6] [JENKINS-51913] Prevent archiveArtifacts from dumping a stacktrace when empty results are allowed --- .../main/java/hudson/tasks/ArtifactArchiver.java | 2 ++ .../java/hudson/tasks/ArtifactArchiverTest.java | 13 +++++++++++++ 2 files changed, 15 insertions(+) diff --git a/core/src/main/java/hudson/tasks/ArtifactArchiver.java b/core/src/main/java/hudson/tasks/ArtifactArchiver.java index 0f72c72319eb..5cfcc693df3a 100644 --- a/core/src/main/java/hudson/tasks/ArtifactArchiver.java +++ b/core/src/main/java/hudson/tasks/ArtifactArchiver.java @@ -270,6 +270,8 @@ public void perform(Run build, FilePath ws, EnvVars environment, Launcher if (msg != null) { listener.getLogger().println(msg); } + } catch (FileMaskNoMatchesFoundException e) { + listener.getLogger().println(e.getMessage()); } catch (Exception e) { Functions.printStackTrace(e, listener.getLogger()); } diff --git a/test/src/test/java/hudson/tasks/ArtifactArchiverTest.java b/test/src/test/java/hudson/tasks/ArtifactArchiverTest.java index 282efe49a70d..e3d854afc2f8 100644 --- a/test/src/test/java/hudson/tasks/ArtifactArchiverTest.java +++ b/test/src/test/java/hudson/tasks/ArtifactArchiverTest.java @@ -131,6 +131,19 @@ public void testAllowEmptyArchive() throws Exception { assertFalse(project.getBuildByNumber(1).getHasArtifacts()); } + @Test + @Issue("JENKINS-51913") + public void testFilePathNoMaskFoundException() throws Exception { + FreeStyleProject project = j.createFreeStyleProject(); + ArtifactArchiver aa = new ArtifactArchiver("f"); + project.getPublishersList().replaceBy(Collections.singleton(aa)); + j.buildAndAssertSuccess(project); + project.getBuildByNumber(1).getHasArtifacts(); + String pattern = "dir/**" + j.assertLogContains("WARN: No artifacts found that match the file pattern " + pattern, build); + assertThat("No stacktrace shown", build.getLog(31), Matchers.iterableWithSize(lessThan(30))); + } + @Issue("JENKINS-21958") @Test public void symlinks() throws Exception { FreeStyleProject p = j.createFreeStyleProject(); From db575f02439f190b523d3aaeebe25c0ea615f1a1 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Wed, 13 Apr 2022 14:46:36 -0700 Subject: [PATCH 3/6] Fix compilation --- .../main/java/hudson/tasks/ArtifactArchiver.java | 2 +- .../java/hudson/tasks/ArtifactArchiverTest.java | 13 +++++++------ 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/core/src/main/java/hudson/tasks/ArtifactArchiver.java b/core/src/main/java/hudson/tasks/ArtifactArchiver.java index 5cfcc693df3a..efa0a63bed49 100644 --- a/core/src/main/java/hudson/tasks/ArtifactArchiver.java +++ b/core/src/main/java/hudson/tasks/ArtifactArchiver.java @@ -270,7 +270,7 @@ public void perform(Run build, FilePath ws, EnvVars environment, Launcher if (msg != null) { listener.getLogger().println(msg); } - } catch (FileMaskNoMatchesFoundException e) { + } catch (FilePath.FileMaskNoMatchesFoundException e) { listener.getLogger().println(e.getMessage()); } catch (Exception e) { Functions.printStackTrace(e, listener.getLogger()); diff --git a/test/src/test/java/hudson/tasks/ArtifactArchiverTest.java b/test/src/test/java/hudson/tasks/ArtifactArchiverTest.java index e3d854afc2f8..26efb0a3abd7 100644 --- a/test/src/test/java/hudson/tasks/ArtifactArchiverTest.java +++ b/test/src/test/java/hudson/tasks/ArtifactArchiverTest.java @@ -135,14 +135,15 @@ public void testAllowEmptyArchive() throws Exception { @Issue("JENKINS-51913") public void testFilePathNoMaskFoundException() throws Exception { FreeStyleProject project = j.createFreeStyleProject(); - ArtifactArchiver aa = new ArtifactArchiver("f"); + String pattern = "dir/**"; + ArtifactArchiver aa = new ArtifactArchiver(pattern); + aa.setAllowEmptyArchive(true); project.getPublishersList().replaceBy(Collections.singleton(aa)); - j.buildAndAssertSuccess(project); - project.getBuildByNumber(1).getHasArtifacts(); - String pattern = "dir/**" - j.assertLogContains("WARN: No artifacts found that match the file pattern " + pattern, build); + FreeStyleBuild build = j.buildAndAssertSuccess(project); + assertFalse(project.getBuildByNumber(1).getHasArtifacts()); + j.assertLogContains("No artifacts found that match the file pattern \"" + pattern + "\"", build); assertThat("No stacktrace shown", build.getLog(31), Matchers.iterableWithSize(lessThan(30))); - } + } @Issue("JENKINS-21958") @Test public void symlinks() throws Exception { From 78d1d365deb22391bb594a624192d4354bdb41db Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Wed, 13 Apr 2022 15:24:29 -0700 Subject: [PATCH 4/6] Improve Javadoc --- core/src/main/java/hudson/FilePath.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/hudson/FilePath.java b/core/src/main/java/hudson/FilePath.java index 8ddaeb9d1617..f47392e6530c 100644 --- a/core/src/main/java/hudson/FilePath.java +++ b/core/src/main/java/hudson/FilePath.java @@ -55,6 +55,7 @@ import hudson.remoting.Which; import hudson.security.AccessControlled; import hudson.slaves.WorkspaceList; +import hudson.tasks.ArtifactArchiver; import hudson.util.DaemonThreadFactory; import hudson.util.DirScanner; import hudson.util.ExceptionCatchingThreadFactory; @@ -3055,7 +3056,10 @@ public String validateAntFileMask(final String fileMasks, final boolean caseSens public static int VALIDATE_ANT_FILE_MASK_BOUND = SystemProperties.getInteger(FilePath.class.getName() + ".VALIDATE_ANT_FILE_MASK_BOUND", 10000); /** - * Provide an explicit exception {@link InterruptedException} for when no matching ant file mask matches are found + * A dedicated subtype of {@link InterruptedException} for when no matching Ant file mask + * matches are found. + * + * @see ArtifactArchiver */ public static class FileMaskNoMatchesFoundException extends InterruptedException { private FileMaskNoMatchesFoundException(String message) { From 24f64dbd9990356f3a0c27030c52d4e52ef105d9 Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Wed, 13 Apr 2022 15:25:37 -0700 Subject: [PATCH 5/6] Restrict new API --- core/src/main/java/hudson/FilePath.java | 1 + 1 file changed, 1 insertion(+) diff --git a/core/src/main/java/hudson/FilePath.java b/core/src/main/java/hudson/FilePath.java index f47392e6530c..6b22e21d8362 100644 --- a/core/src/main/java/hudson/FilePath.java +++ b/core/src/main/java/hudson/FilePath.java @@ -3061,6 +3061,7 @@ public String validateAntFileMask(final String fileMasks, final boolean caseSens * * @see ArtifactArchiver */ + @Restricted(NoExternalUse.class) public static class FileMaskNoMatchesFoundException extends InterruptedException { private FileMaskNoMatchesFoundException(String message) { super(message); From e4f960e200c8574c5eb5dee3cf5a9f1545aee34f Mon Sep 17 00:00:00 2001 From: Basil Crow Date: Wed, 13 Apr 2022 15:30:06 -0700 Subject: [PATCH 6/6] Fix name of test --- test/src/test/java/hudson/tasks/ArtifactArchiverTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/src/test/java/hudson/tasks/ArtifactArchiverTest.java b/test/src/test/java/hudson/tasks/ArtifactArchiverTest.java index 26efb0a3abd7..92dee288b429 100644 --- a/test/src/test/java/hudson/tasks/ArtifactArchiverTest.java +++ b/test/src/test/java/hudson/tasks/ArtifactArchiverTest.java @@ -133,7 +133,7 @@ public void testAllowEmptyArchive() throws Exception { @Test @Issue("JENKINS-51913") - public void testFilePathNoMaskFoundException() throws Exception { + public void testFileMaskNoMatchesFoundException() throws Exception { FreeStyleProject project = j.createFreeStyleProject(); String pattern = "dir/**"; ArtifactArchiver aa = new ArtifactArchiver(pattern);