From 5976fc8d8a3d851cb32efc381723b2c575952e6b Mon Sep 17 00:00:00 2001 From: Karol Stepniewski Date: Fri, 23 Jun 2017 18:07:40 -0700 Subject: [PATCH] [JENKINS-32898] Fix Disable Strict Forbidden File Verification option When Strict Forbidden File Verification option is disabled, job can still be triggered if file paths are mixed, i.e. there are some files that match forbidden file paths, and some that only match included file paths. However, when ALL files match forbidden file paths, job should not be triggered. This patch ensures this by checking whether all files match forbidden file paths if Strict Forbidden File Verification Option is disabled. --- .../trigger/hudsontrigger/data/FilePath.java | 9 +++ .../hudsontrigger/data/GerritProject.java | 78 +++++++++++++------ 2 files changed, 62 insertions(+), 25 deletions(-) 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());