Skip to content

Commit

Permalink
Merge pull request #121 from yannack/forbiddenFilesOption
Browse files Browse the repository at this point in the history
Added a "Forbidden Files" optional parameter to Gerrit Projects [FIXED JENKINS-21232]
  • Loading branch information
rsandell committed Jan 15, 2014
2 parents b2028be + 4337255 commit 72cc129
Show file tree
Hide file tree
Showing 9 changed files with 150 additions and 22 deletions.
Expand Up @@ -60,6 +60,7 @@ public final class GerritDynamicUrlProcessor {
private static final String SHORTNAME_BRANCH = "b";
private static final String SHORTNAME_TOPIC = "t";
private static final String SHORTNAME_FILE = "f";
private static final String SHORTNAME_FORBIDDEN_FILE = "o";
private static final int SOCKET_READ_TIMEOUT = 10000;

/**
Expand All @@ -85,6 +86,7 @@ private static Pattern buildLinePattern() {
+ "|" + SHORTNAME_BRANCH
+ "|" + SHORTNAME_TOPIC
+ "|" + SHORTNAME_FILE
+ "|" + SHORTNAME_FORBIDDEN_FILE
+ ")";
String operators = "(";
boolean firstoperator = true;
Expand Down Expand Up @@ -119,6 +121,7 @@ private static List<GerritProject> readAndParseTriggerConfig(BufferedReader read
List<Branch> branches = null;
List<Topic> topics = null;
List<FilePath> filePaths = null;
List<FilePath> forbiddenFilePaths = null;
GerritProject dynamicGerritProject = null;

String line = "";
Expand Down Expand Up @@ -169,7 +172,8 @@ private static List<GerritProject> readAndParseTriggerConfig(BufferedReader read
branches = new ArrayList<Branch>();
topics = new ArrayList<Topic>();
filePaths = new ArrayList<FilePath>();
dynamicGerritProject = new GerritProject(type, text, branches, topics, filePaths);
forbiddenFilePaths = new ArrayList<FilePath>();
dynamicGerritProject = new GerritProject(type, text, branches, topics, filePaths, forbiddenFilePaths);
} else if (SHORTNAME_BRANCH.equals(item)) { // Branch
if (branches == null) {
throw new ParseException("Line " + lineNr + ": attempt to use 'Branch' before 'Project'", lineNr);
Expand All @@ -184,13 +188,20 @@ private static List<GerritProject> readAndParseTriggerConfig(BufferedReader read
Topic topic = new Topic(type, text);
topics.add(topic);
dynamicGerritProject.setTopics(topics);
} else { // FilePath (because it must be an 'f')
} else if (SHORTNAME_FILE.equals(item)) { // FilePath
if (filePaths == null) {
throw new ParseException("Line " + lineNr + ": attempt to use 'FilePath' before 'Project'", lineNr);
}
FilePath filePath = new FilePath(type, text);
filePaths.add(filePath);
dynamicGerritProject.setFilePaths(filePaths);
} else if (SHORTNAME_FORBIDDEN_FILE.equals(item)) { // ForbiddenFilePath
if (forbiddenFilePaths == null) {
throw new ParseException("Line " + lineNr + ": attempt to use 'ForbiddenFilePath' before 'Project'", lineNr);
}
FilePath filePath = new FilePath(type, text);
forbiddenFilePaths.add(filePath);
dynamicGerritProject.setForbiddenFilePaths(filePaths);
}
}

Expand Down
Expand Up @@ -53,7 +53,7 @@ public class GerritProject implements Describable<GerritProject> {
private List<Branch> branches;
private List<FilePath> filePaths;
private List<Topic> topics;

private List<FilePath> forbiddenFilePaths;

/**
* Default empty constructor.
Expand All @@ -69,20 +69,23 @@ public GerritProject() {
* @param branches the branch-rules
* @param topics the topic-rules
* @param filePaths the file-path rules.
* @param forbiddenFilePaths the forbidden file-path rules.
*/
@DataBoundConstructor
public GerritProject(
CompareType compareType,
String pattern,
List<Branch> branches,
List<Topic> topics,
List<FilePath> filePaths) {
List<FilePath> filePaths,
List<FilePath> forbiddenFilePaths) {

this.compareType = compareType;
this.pattern = pattern;
this.branches = branches;
this.topics = topics;
this.filePaths = filePaths;
this.forbiddenFilePaths = forbiddenFilePaths;
}

/**
Expand Down Expand Up @@ -165,6 +168,22 @@ public void setTopics(List<Topic> topics) {
this.topics = topics;
}

/**
* The list of the forbidden file-path rules.
* @return the forbidden file-path rules.
*/
public List<FilePath> getForbiddenFilePaths() {
return forbiddenFilePaths;
}

/**
* The list of the forbidden file-path rules.
* @param forbiddenFilePaths the forbidden file-path rules.
*/
public void setForbiddenFilePaths(List<FilePath> forbiddenFilePaths) {
this.forbiddenFilePaths = forbiddenFilePaths;
}

/**
* Compares the project, branch and files to see if the rules specified is a match.
* @param project the Gerrit project
Expand All @@ -177,6 +196,13 @@ public boolean isInteresting(String project, String branch, String topic, List<S
if (compareType.matches(pattern, project)) {
for (Branch b : branches) {
if (b.isInteresting(branch)) {
if (forbiddenFilePaths != null) {
for (FilePath ffp : forbiddenFilePaths) {
if (ffp.isInteresting(files)) {
return false;
}
}
}
if (isInterestingTopic(topic) && isInterestingFile(files)) {
return true;
}
Expand Down
Expand Up @@ -333,6 +333,39 @@
</f:repeatable>
</td>
</tr>
<tr>
<td colspan="4" valign="top" style="border-left: 1px solid black; border-right: 1px solid black; border-bottom: 1px solid black;">
<f:repeatable var="ff" field="forbiddenFilePaths" add="${%Add Forbidden File path}" minimum="0" header="">
<table width="100%">
<tr>
<td width="60">
${%Forbidden File}
</td>
<td width="80">
<select class="setting-input" name="_.compareType">
<j:forEach items="${types}" var="me">
<j:choose>
<j:when test="${loop.getCompareType().name()==me.name()}">
<option value="${me.name()}" selected="true">${me.getDisplayName()}</option>
</j:when>
<j:otherwise>
<option value="${me.name()}">${me.getDisplayName()}</option>
</j:otherwise>
</j:choose>
</j:forEach>
</select>
</td>
<td minwidth="150">
<f:textbox field="pattern" value="${b.pattern}" default=""/>
</td>
<td width="65">
<f:repeatableDeleteButton/>
</td>
</tr>
</table>
</f:repeatable>
</td>
</tr>
</j:if>
</table>
</f:repeatable>
Expand Down
Expand Up @@ -24,8 +24,9 @@
b for branch<br/>
t for topic<br />
f for file<br/>
o for forbidden file<br/>
= for plain syntax<br/>
^ for ANT style syntax<br/>
~ for regexp syntax<br/>
Branch, topic and file lines are assumed to be part of the closest preceding project line.
Branch, topic, file and forbidden file lines are assumed to be part of the closest preceding project line.
</p>
Expand Up @@ -79,7 +79,7 @@ public void testIt() throws IOException, InterruptedException {
trigger.setGerritProjects(Collections.singletonList(
new GerritProject(CompareType.PLAIN, event.getChange().getProject(),
Collections.singletonList(new Branch(CompareType.PLAIN, event.getChange().getBranch())),
null, null)
null, null, null)
));
trigger.setSilentMode(false);
trigger.setGerritBuildSuccessfulCodeReviewValue(1);
Expand Down
Expand Up @@ -63,7 +63,7 @@ private GerritProject createGerritProject(String pattern, CompareType compareTyp
List<Branch> branches = new LinkedList<Branch>();
Branch branch = new Branch(compareType, "master");
branches.add(branch);
GerritProject config = new GerritProject(CompareType.PLAIN, pattern, branches, null, null);
GerritProject config = new GerritProject(CompareType.PLAIN, pattern, branches, null, null, null);
return config;
}

Expand Down
Expand Up @@ -75,43 +75,43 @@ public static Collection getParameters() {
List<Topic> topics = new LinkedList<Topic>();
Branch branch = new Branch(CompareType.PLAIN, "master");
branches.add(branch);
GerritProject config = new GerritProject(CompareType.PLAIN, "project", branches, topics, null);
GerritProject config = new GerritProject(CompareType.PLAIN, "project", branches, topics, null, null);
parameters.add(new InterestingScenario[]{new InterestingScenario(config, "project", "master", null, true)});

branches = new LinkedList<Branch>();
branch = new Branch(CompareType.ANT, "**/master");
branches.add(branch);
config = new GerritProject(CompareType.PLAIN, "project", branches, topics, null);
config = new GerritProject(CompareType.PLAIN, "project", branches, topics, null, null);
parameters.add(new InterestingScenario[]{new InterestingScenario(config,
"project", "origin/master", null, true), });

branches = new LinkedList<Branch>();
branch = new Branch(CompareType.ANT, "**/master");
branches.add(branch);
config = new GerritProject(CompareType.PLAIN, "project", branches, topics, null);
config = new GerritProject(CompareType.PLAIN, "project", branches, topics, null, null);
parameters.add(new InterestingScenario[]{new InterestingScenario(config, "project", "master", null, true)});

branches = new LinkedList<Branch>();
branch = new Branch(CompareType.ANT, "**/master");
branches.add(branch);
branch = new Branch(CompareType.REG_EXP, "feature/.*master");
branches.add(branch);
config = new GerritProject(CompareType.PLAIN, "project", branches, topics, null);
config = new GerritProject(CompareType.PLAIN, "project", branches, topics, null, null);
parameters.add(new InterestingScenario[]{new InterestingScenario(config, "project", "master", null, true)});

branches = new LinkedList<Branch>();
branch = new Branch(CompareType.PLAIN, "olstorp");
branches.add(branch);
branch = new Branch(CompareType.REG_EXP, "feature/.*master");
branches.add(branch);
config = new GerritProject(CompareType.PLAIN, "project", branches, topics, null);
config = new GerritProject(CompareType.PLAIN, "project", branches, topics, null, null);
parameters.add(new InterestingScenario[]{new InterestingScenario(config,
"project", "feature/mymaster", null, true), });

branches = new LinkedList<Branch>();
branch = new Branch(CompareType.ANT, "**/master");
branches.add(branch);
config = new GerritProject(CompareType.ANT, "vendor/**/project", branches, topics, null);
config = new GerritProject(CompareType.ANT, "vendor/**/project", branches, topics, null, null);
parameters.add(new InterestingScenario[]{new InterestingScenario(config,
"vendor/semc/master/project", "origin/master", null, true), });

Expand All @@ -121,20 +121,20 @@ public static Collection getParameters() {
topics = new LinkedList<Topic>();
Topic topic = new Topic(CompareType.PLAIN, "topic");
topics.add(topic);
config = new GerritProject(CompareType.PLAIN, "project", branches, topics, null);
config = new GerritProject(CompareType.PLAIN, "project", branches, topics, null, null);
parameters.add(new InterestingScenario[]{new InterestingScenario(config, "project", "master", "topic", true)});

topics = new LinkedList<Topic>();
topic = new Topic(CompareType.ANT, "**/topic");
topics.add(topic);
config = new GerritProject(CompareType.PLAIN, "project", branches, topics, null);
config = new GerritProject(CompareType.PLAIN, "project", branches, topics, null, null);
parameters.add(new InterestingScenario[]{new InterestingScenario(config,
"project", "master", "team/topic", true), });

topics = new LinkedList<Topic>();
topic = new Topic(CompareType.REG_EXP, ".*_topic");
topics.add(topic);
config = new GerritProject(CompareType.PLAIN, "project", branches, topics, null);
config = new GerritProject(CompareType.PLAIN, "project", branches, topics, null, null);
parameters.add(new InterestingScenario[]{new InterestingScenario(config,
"project", "master", "team-wolf_topic", true), });

Expand Down
Expand Up @@ -76,7 +76,9 @@ public static Collection getParameters() {
List<FilePath> filePaths = new LinkedList<FilePath>();
FilePath filePath = new FilePath(CompareType.PLAIN, "test.txt");
filePaths.add(filePath);
GerritProject config = new GerritProject(CompareType.PLAIN, "project", branches, topics, filePaths);
List<FilePath> forbiddenFilePaths = null;
GerritProject config = new GerritProject(
CompareType.PLAIN, "project", branches, topics, filePaths, forbiddenFilePaths);
List<String> files = new LinkedList<String>();
files.add("test.txt");
parameters.add(new InterestingScenarioWithFiles[]{new InterestingScenarioWithFiles(
Expand All @@ -90,7 +92,8 @@ public static Collection getParameters() {
filePaths.add(filePath);
files = new LinkedList<String>();
files.add("tests/test.txt");
config = new GerritProject(CompareType.REG_EXP, "project.*5", branches, topics, filePaths);
forbiddenFilePaths = null;
config = new GerritProject(CompareType.REG_EXP, "project.*5", branches, topics, filePaths, forbiddenFilePaths);
parameters.add(new InterestingScenarioWithFiles[]{new InterestingScenarioWithFiles(
config, "projectNumber5", "feature/mymaster", null, files, true), });

Expand All @@ -100,7 +103,9 @@ public static Collection getParameters() {
filePaths = new LinkedList<FilePath>();
filePath = new FilePath(CompareType.ANT, "**/*test*");
filePaths.add(filePath);
config = new GerritProject(CompareType.ANT, "vendor/**/project", branches, topics, filePaths);
forbiddenFilePaths = null;
config = new GerritProject(
CompareType.ANT, "vendor/**/project", branches, topics, filePaths, forbiddenFilePaths);
files = new LinkedList<String>();
files.add("resources/test.xml");
parameters.add(new InterestingScenarioWithFiles[]{new InterestingScenarioWithFiles(
Expand All @@ -114,10 +119,62 @@ public static Collection getParameters() {
filePaths.add(filePath);
files = new LinkedList<String>();
files.add("notintests/test.txt");
config = new GerritProject(CompareType.REG_EXP, "project.*5", branches, topics, filePaths);
forbiddenFilePaths = null;
config = new GerritProject(CompareType.REG_EXP, "project.*5", branches, topics, filePaths, forbiddenFilePaths);
parameters.add(new InterestingScenarioWithFiles[]{new InterestingScenarioWithFiles(
config, "projectNumber5", "feature/mymaster", null, files, false), });

//Testing with Forbidden File Paths now
branches = new LinkedList<Branch>();
branch = new Branch(CompareType.PLAIN, "master");
branches.add(branch);
topics = new LinkedList<Topic>();
filePaths = new LinkedList<FilePath>();
filePath = new FilePath(CompareType.PLAIN, "test.txt");
filePaths.add(filePath);
forbiddenFilePaths = new LinkedList<FilePath>();
FilePath forbiddenFilePath = new FilePath(CompareType.PLAIN, "test2.txt");
forbiddenFilePaths.add(forbiddenFilePath);
config = new GerritProject(CompareType.PLAIN, "project", branches, topics, filePaths, forbiddenFilePaths);
files = new LinkedList<String>();
files.add("test.txt");
files.add("test2.txt");
parameters.add(new InterestingScenarioWithFiles[]{new InterestingScenarioWithFiles(
config, "project", "master", null, files, false), });

branches = new LinkedList<Branch>();
branch = new Branch(CompareType.REG_EXP, "feature/.*master");
branches.add(branch);
filePaths = new LinkedList<FilePath>();
filePath = new FilePath(CompareType.REG_EXP, "tests/.*");
filePaths.add(filePath);
forbiddenFilePaths = new LinkedList<FilePath>();
forbiddenFilePath = new FilePath(CompareType.REG_EXP, "tests/.*2.*");
forbiddenFilePaths.add(forbiddenFilePath);
files = new LinkedList<String>();
files.add("tests/test.txt");
files.add("tests/test2.txt");
config = new GerritProject(CompareType.REG_EXP, "project.*5", branches, topics, filePaths, forbiddenFilePaths);
parameters.add(new InterestingScenarioWithFiles[]{new InterestingScenarioWithFiles(
config, "projectNumber5", "feature/mymaster", null, files, false), });

branches = new LinkedList<Branch>();
branch = new Branch(CompareType.ANT, "**/master");
branches.add(branch);
filePaths = new LinkedList<FilePath>();
filePath = new FilePath(CompareType.ANT, "**/*test*");
filePaths.add(filePath);
forbiddenFilePaths = new LinkedList<FilePath>();
forbiddenFilePath = new FilePath(CompareType.ANT, "**/*skip*");
forbiddenFilePaths.add(forbiddenFilePath);
config = new GerritProject(
CompareType.ANT, "vendor/**/project", branches, topics, filePaths, forbiddenFilePaths);
files = new LinkedList<String>();
files.add("resources/test.xml");
files.add("files/skip.txt");
parameters.add(new InterestingScenarioWithFiles[]{new InterestingScenarioWithFiles(
config, "vendor/semc/master/project", "origin/master", null, files, false), });

return parameters;
}

Expand Down

0 comments on commit 72cc129

Please sign in to comment.