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

[JENKINS-51913] Prevent archiveArtifacts from displaying a stack trace when no artifacts are found #6475

Merged
merged 9 commits into from
Apr 15, 2022
18 changes: 17 additions & 1 deletion core/src/main/java/hudson/FilePath.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -3054,6 +3055,21 @@ 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);

/**
* A dedicated subtype of {@link InterruptedException} for when no matching Ant file mask
* matches are found.
*
* @see ArtifactArchiver
*/
@Restricted(NoExternalUse.class)
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.
Expand Down Expand Up @@ -3223,7 +3239,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;
Expand Down
2 changes: 2 additions & 0 deletions core/src/main/java/hudson/tasks/ArtifactArchiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ public void perform(Run<?, ?> build, FilePath ws, EnvVars environment, Launcher
if (msg != null) {
listener.getLogger().println(msg);
}
} catch (FilePath.FileMaskNoMatchesFoundException e) {
listener.getLogger().println(e.getMessage());
} catch (Exception e) {
Functions.printStackTrace(e, listener.getLogger());
}
Expand Down
14 changes: 14 additions & 0 deletions test/src/test/java/hudson/tasks/ArtifactArchiverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@ public void testAllowEmptyArchive() throws Exception {
assertFalse(project.getBuildByNumber(1).getHasArtifacts());
}

@Test
@Issue("JENKINS-51913")
public void testFilePathNoMaskFoundException() throws Exception {
FreeStyleProject project = j.createFreeStyleProject();
String pattern = "dir/**";
ArtifactArchiver aa = new ArtifactArchiver(pattern);
aa.setAllowEmptyArchive(true);
project.getPublishersList().replaceBy(Collections.singleton(aa));
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)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or more directly could do something along the lines of

assertThat("No stacktrace shown", build.getLog(), not(containsString("\tat ")));

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But then that would be inconsistent with other code in this file.

}

@Issue("JENKINS-21958")
@Test public void symlinks() throws Exception {
FreeStyleProject p = j.createFreeStyleProject();
Expand Down