Skip to content

Commit eb27d02

Browse files
author
Karol Stepniewski
committed
[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.
1 parent 682ac6e commit eb27d02

File tree

2 files changed

+62
-25
lines changed

2 files changed

+62
-25
lines changed

src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/FilePath.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,15 @@ public boolean isInteresting(List<String> files) {
103103
return false;
104104
}
105105

106+
/**
107+
* Tells if the given file is matched by this rule.
108+
* @param file the file path to match against.
109+
* @return true if the file matches.
110+
*/
111+
public boolean isInterestingFile(String file) {
112+
return compareType.matches(pattern, file);
113+
}
114+
106115
/**
107116
* The Descriptor for the FilePath.
108117
*/

src/main/java/com/sonyericsson/hudson/plugins/gerrit/trigger/hudsontrigger/data/GerritProject.java

Lines changed: 53 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -213,37 +213,32 @@ public void setForbiddenFilePaths(List<FilePath> forbiddenFilePaths) {
213213
* @return true is the rules match.
214214
*/
215215
public boolean isInteresting(String project, String branch, String topic, List<String> files) {
216-
if (compareType.matches(pattern, project)) {
217-
for (Branch b : branches) {
218-
boolean foundInterestingForbidden = false;
219-
boolean foundInterestingTopicOrFile = false;
220-
if (b.isInteresting(branch)) {
221-
if (forbiddenFilePaths != null) {
222-
for (FilePath ffp : forbiddenFilePaths) {
223-
if (ffp.isInteresting(files)) {
224-
foundInterestingForbidden = true;
225-
break;
226-
}
227-
}
228-
}
229-
if (isInterestingTopic(topic) && isInterestingFile(files)) {
230-
foundInterestingTopicOrFile = true;
216+
if (!compareType.matches(pattern, project)) {
217+
return false;
218+
}
219+
for (Branch b : branches) {
220+
if (!b.isInteresting(branch)) {
221+
continue;
222+
}
223+
224+
if (forbiddenFilePaths != null) {
225+
// Forbidden file paths take precedence over included file paths.
226+
if (disableStrictForbiddenFileVerification) {
227+
// We need to check if all paths match forbidden file paths.
228+
if (areAllFilesForbidden(files)) {
229+
return false;
231230
}
232-
if (disableStrictForbiddenFileVerification) {
233-
// Here we want to be able to trigger a build if the event contains
234-
// wanted topics or file paths even though there may be a forbidden file
235-
return foundInterestingTopicOrFile;
236-
} else {
237-
if (foundInterestingForbidden) {
238-
// we have a forbidden file and a wanted file path.
231+
} else {
232+
for (FilePath ffp : forbiddenFilePaths) {
233+
if (ffp.isInteresting(files)) {
239234
return false;
240-
} else if (foundInterestingTopicOrFile) {
241-
// we DO not have a forbidden file and but we have a wanted file path.
242-
return true;
243235
}
244236
}
245237
}
246238
}
239+
240+
return isInterestingTopic(topic) && isInterestingFile(files));
241+
247242
}
248243
return false;
249244
}
@@ -302,6 +297,39 @@ private boolean isInterestingFile(List<String> files) {
302297
return true;
303298
}
304299

300+
/**
301+
* Check if all files from the list match any of forbidden file paths.
302+
*
303+
* @param files the files to check.
304+
* @return true if all files match forbidden file paths.
305+
*/
306+
private boolean areAllFilesForbidden(List<String> files) {
307+
for (String file : files) {
308+
if (!isForbiddenFile(file)) {
309+
return false;
310+
}
311+
}
312+
return true;
313+
}
314+
315+
/**
316+
* Checks if file matches any of the forbidden file paths.
317+
*
318+
* @param file the file path to check.
319+
* @return true if any of the forbidden path rules match.
320+
*/
321+
private boolean isForbiddenFile(String file) {
322+
if (forbiddenFilePaths == null || forbiddenFilePaths.size() == 0) {
323+
return false;
324+
}
325+
for (FilePath f : forbiddenFilePaths) {
326+
if (f.isInterestingFile(file)) {
327+
return true;
328+
}
329+
}
330+
return false;
331+
}
332+
305333
@Override
306334
public Descriptor<GerritProject> getDescriptor() {
307335
return Hudson.getInstance().getDescriptor(getClass());

0 commit comments

Comments
 (0)