diff --git a/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/FilePath.java b/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/FilePath.java index 1d332fe3e..c5fedec64 100644 --- a/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/FilePath.java +++ b/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/FilePath.java @@ -103,6 +103,15 @@ public boolean isInteresting(List files) { return false; } + /** + * Tells if the given file is matched by this rule. + * @param file the file path to match against. + * @return true if the file matches. + */ + public boolean isInterestingFile(String file) { + return compareType.matches(pattern, file); + } + /** * The Descriptor for the FilePath. */ diff --git a/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProject.java b/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProject.java index 693e834e6..9ee39d560 100644 --- a/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProject.java +++ b/src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProject.java @@ -213,37 +213,32 @@ public void setForbiddenFilePaths(List forbiddenFilePaths) { * @return true is the rules match. */ public boolean isInteresting(String project, String branch, String topic, List files) { - if (compareType.matches(pattern, project)) { - for (Branch b : branches) { - boolean foundInterestingForbidden = false; - boolean foundInterestingTopicOrFile = false; - if (b.isInteresting(branch)) { - if (forbiddenFilePaths != null) { - for (FilePath ffp : forbiddenFilePaths) { - if (ffp.isInteresting(files)) { - foundInterestingForbidden = true; - break; - } - } - } - if (isInterestingTopic(topic) && isInterestingFile(files)) { - foundInterestingTopicOrFile = true; + if (!compareType.matches(pattern, project)) { + return false; + } + for (Branch b : branches) { + if (!b.isInteresting(branch)) { + continue; + } + + if (forbiddenFilePaths != null) { + // Forbidden file paths take precedence over included file paths. + if (disableStrictForbiddenFileVerification) { + // We need to check if all paths match forbidden file paths. + if (areAllFilesForbidden(files)) { + return false; } - if (disableStrictForbiddenFileVerification) { - // Here we want to be able to trigger a build if the event contains - // wanted topics or file paths even though there may be a forbidden file - return foundInterestingTopicOrFile; - } else { - if (foundInterestingForbidden) { - // we have a forbidden file and a wanted file path. + } else { + for (FilePath ffp : forbiddenFilePaths) { + if (ffp.isInteresting(files)) { return false; - } else if (foundInterestingTopicOrFile) { - // we DO not have a forbidden file and but we have a wanted file path. - return true; } } } } + + return isInterestingTopic(topic) && isInterestingFile(files); + } return false; } @@ -302,6 +297,39 @@ private boolean isInterestingFile(List files) { return true; } + /** + * Check if all files from the list match any of forbidden file paths. + * + * @param files the files to check. + * @return true if all files match forbidden file paths. + */ + private boolean areAllFilesForbidden(List files) { + for (String file : files) { + if (!isForbiddenFile(file)) { + return false; + } + } + return true; + } + + /** + * Checks if file matches any of the forbidden file paths. + * + * @param file the file path to check. + * @return true if any of the forbidden path rules match. + */ + private boolean isForbiddenFile(String file) { + if (forbiddenFilePaths == null || forbiddenFilePaths.size() == 0) { + return false; + } + for (FilePath f : forbiddenFilePaths) { + if (f.isInterestingFile(file)) { + return true; + } + } + return false; + } + @Override public Descriptor getDescriptor() { return Hudson.getInstance().getDescriptor(getClass());