Skip to content

Commit

Permalink
Merge pull request #175 from jenkinsci/glob-relative-to-workspace
Browse files Browse the repository at this point in the history
[JENKINS-73505] When using a glob relativize the workspace path
  • Loading branch information
uhafner authored Aug 19, 2024
2 parents bc87d03 + f1ed725 commit 49624ee
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ private List<String> 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) {
Expand All @@ -132,12 +133,14 @@ private boolean containsNoPathMatcherPattern(final String pattern) {
}

private static class PathMatcherFileVisitor extends SimpleFileVisitor<Path> {
private final Path workspace;
private final PathMatcher pathMatcher;
private final List<String> matches = new ArrayList<>();

PathMatcherFileVisitor(final String syntaxAndPattern) {
PathMatcherFileVisitor(final Path workspace, final String syntaxAndPattern) {
super();

this.workspace = workspace;
pathMatcher = FileSystems.getDefault().getPathMatcher(syntaxAndPattern);
}

Expand All @@ -147,7 +150,7 @@ List<String> 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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 49624ee

Please sign in to comment.