Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[JENKINS-32898] Fix Disable Strict Forbidden File Verification option #318

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

probably needs a null check as you've done in isForbiddenFile

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