Skip to content

Commit

Permalink
[JENKINS-32898] Fix Disable Strict Forbidden File Verification option
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Karol Stepniewski committed Jun 26, 2017
1 parent 682ac6e commit 5976fc8
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ public boolean isInteresting(List<String> 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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,37 +213,32 @@ public void setForbiddenFilePaths(List<FilePath> forbiddenFilePaths) {
* @return true is the rules match.
*/
public boolean isInteresting(String project, String branch, String topic, List<String> 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;
}
Expand Down Expand Up @@ -302,6 +297,39 @@ private boolean isInterestingFile(List<String> 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<String> 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<GerritProject> getDescriptor() {
return Hudson.getInstance().getDescriptor(getClass());
Expand Down

0 comments on commit 5976fc8

Please sign in to comment.