From f1ed725f051cf25c67827bb0297e29f4f673344b Mon Sep 17 00:00:00 2001 From: Ulli Hafner Date: Mon, 19 Aug 2024 13:22:29 +0200 Subject: [PATCH] [JENKINS-73505] When using a glob relativize the workspace path. --- .../jenkins/plugins/prism/SourceDirectoryFilter.java | 11 +++++++---- .../plugins/prism/SourceDirectoryFilterTest.java | 6 ++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/main/java/io/jenkins/plugins/prism/SourceDirectoryFilter.java b/src/main/java/io/jenkins/plugins/prism/SourceDirectoryFilter.java index 960440f..d7ac22b 100644 --- a/src/main/java/io/jenkins/plugins/prism/SourceDirectoryFilter.java +++ b/src/main/java/io/jenkins/plugins/prism/SourceDirectoryFilter.java @@ -111,8 +111,9 @@ private List findRelative(final String directory, final String pattern, } try { - PathMatcherFileVisitor visitor = new PathMatcherFileVisitor(pattern); - Files.walkFileTree(Paths.get(directory), visitor); + var workspace = Paths.get(directory); + PathMatcherFileVisitor visitor = new PathMatcherFileVisitor(workspace, pattern); + Files.walkFileTree(workspace, visitor); return visitor.getMatches(); } catch (IllegalArgumentException exception) { @@ -132,12 +133,14 @@ private boolean containsNoPathMatcherPattern(final String pattern) { } private static class PathMatcherFileVisitor extends SimpleFileVisitor { + private final Path workspace; private final PathMatcher pathMatcher; private final List matches = new ArrayList<>(); - PathMatcherFileVisitor(final String syntaxAndPattern) { + PathMatcherFileVisitor(final Path workspace, final String syntaxAndPattern) { super(); + this.workspace = workspace; pathMatcher = FileSystems.getDefault().getPathMatcher(syntaxAndPattern); } @@ -147,7 +150,7 @@ List getMatches() { @Override public FileVisitResult postVisitDirectory(final Path dir, final IOException exc) throws IOException { - if (pathMatcher.matches(dir)) { + if (pathMatcher.matches(dir) || pathMatcher.matches(workspace.relativize(dir))) { matches.add(PATH_UTIL.getAbsolutePath(dir)); } return FileVisitResult.CONTINUE; diff --git a/src/test/java/io/jenkins/plugins/prism/SourceDirectoryFilterTest.java b/src/test/java/io/jenkins/plugins/prism/SourceDirectoryFilterTest.java index e1c015f..cb36225 100644 --- a/src/test/java/io/jenkins/plugins/prism/SourceDirectoryFilterTest.java +++ b/src/test/java/io/jenkins/plugins/prism/SourceDirectoryFilterTest.java @@ -104,6 +104,12 @@ void shouldAllowPatternForRelativePaths() throws IOException { assertThat(allowedDirectories).map(s -> StringUtils.substringAfterLast(s, "/")) .containsExactlyInAnyOrder("ok-1", "ok-2", "ok-3"); + + var singleGlob = filter.getPermittedSourceDirectories(absoluteWorkspacePath(), + EMPTY, Set.of("glob:sub-folder/ok-*"), log); + + assertThat(singleGlob).map(s -> StringUtils.substringAfterLast(s, "/")) + .containsExactlyInAnyOrder("ok-1", "ok-2", "ok-3"); } @Test