Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #49 from juergh/add-included-regions
[FIXED JENKINS-11749] Included regions feature for GitSCM plugin
  • Loading branch information
ndeloof committed Feb 23, 2012
2 parents 5f7ade3 + 1575592 commit 57f504f
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 18 deletions.
54 changes: 46 additions & 8 deletions src/main/java/hudson/plugins/git/GitSCM.java
Expand Up @@ -141,6 +141,7 @@ public class GitSCM extends SCM implements Serializable {
private String gitConfigName;
private String gitConfigEmail;
private boolean skipTag;
private String includedRegions;
private String scmName;

public Collection<SubmoduleConfig> getSubmoduleCfg() {
Expand Down Expand Up @@ -172,7 +173,7 @@ public GitSCM(String repositoryUrl) {
false, Collections.<SubmoduleConfig>emptyList(), false,
false, new DefaultBuildChooser(), null, null, false, null,
null,
null, null, null, false, false, false, false, null, null, false);
null, null, null, false, false, false, false, null, null, false, null);
}

@DataBoundConstructor
Expand All @@ -199,7 +200,8 @@ public GitSCM(
boolean remotePoll,
String gitConfigName,
String gitConfigEmail,
boolean skipTag) {
boolean skipTag,
String includedRegions) {

this.scmName = scmName;

Expand Down Expand Up @@ -262,6 +264,7 @@ public GitSCM(
this.gitConfigName = gitConfigName;
this.gitConfigEmail = gitConfigEmail;
this.skipTag = skipTag;
this.includedRegions = includedRegions;
buildChooser.gitSCM = this; // set the owner
}

Expand Down Expand Up @@ -393,6 +396,20 @@ public Object readResolve() {
return this;
}

public String getIncludedRegions() {
return includedRegions;
}

public String[] getIncludedRegionsNormalized() {
return (includedRegions == null || includedRegions.trim().equals(""))
? null : includedRegions.split("[\\r\\n]+");
}

private Pattern[] getIncludedRegionsPatterns() {
String[] included = getIncludedRegionsNormalized();
return getRegionsPatterns(included);
}

public String getExcludedRegions() {
return excludedRegions;
}
Expand All @@ -404,12 +421,16 @@ public String[] getExcludedRegionsNormalized() {

private Pattern[] getExcludedRegionsPatterns() {
String[] excluded = getExcludedRegionsNormalized();
if (excluded != null) {
Pattern[] patterns = new Pattern[excluded.length];
return getRegionsPatterns(excluded);
}

private Pattern[] getRegionsPatterns(String[] regions) {
if (regions != null) {
Pattern[] patterns = new Pattern[regions.length];

int i = 0;
for (String excludedRegion : excluded) {
patterns[i++] = Pattern.compile(excludedRegion);
for (String region : regions) {
patterns[i++] = Pattern.compile(region);
}

return patterns;
Expand Down Expand Up @@ -1722,6 +1743,7 @@ private boolean isRevExcluded(IGitAPI git, Revision r, TaskListener listener) {

GitChangeSet change = new GitChangeSet(revShow, authorOrCommitter);

Pattern[] includedPatterns = getIncludedRegionsPatterns();
Pattern[] excludedPatterns = getExcludedRegionsPatterns();
Set<String> excludedUsers = getExcludedUsersNormalized();

Expand All @@ -1738,9 +1760,25 @@ private boolean isRevExcluded(IGitAPI git, Revision r, TaskListener listener) {
return false;
}

// Assemble the list of included paths
List<String> includedPaths = new ArrayList<String>();
if (includedPatterns.length > 0) {
for (String path : paths) {
for (Pattern pattern : includedPatterns) {
if (pattern.matcher(path).matches()) {
includedPaths.add(path);
break;
}
}
}
} else {
includedPaths = paths;
}

// Assemble the list of excluded paths
List<String> excludedPaths = new ArrayList<String>();
if (excludedPatterns.length > 0) {
for (String path : paths) {
for (String path : includedPaths) {
for (Pattern pattern : excludedPatterns) {
if (pattern.matcher(path).matches()) {
excludedPaths.add(path);
Expand All @@ -1751,7 +1789,7 @@ private boolean isRevExcluded(IGitAPI git, Revision r, TaskListener listener) {
}

// If every affected path is excluded, return true.
if (paths.size() == excludedPaths.size()) {
if (includedPaths.size() == excludedPaths.size()) {
listener.getLogger().println("Ignored commit " + r.getSha1String()
+ ": Found only excluded paths: "
+ Util.join(excludedPaths, ", "));
Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/hudson/plugins/git/GitSCM/config.jelly
Expand Up @@ -66,6 +66,9 @@
</f:entry>

<f:advanced>
<f:entry title="${%Included Regions}" field="includedRegions" help="/plugin/git/help-includedRegions.html">
<f:textarea />
</f:entry>
<f:entry title="${%Excluded Regions}" field="excludedRegions" help="/plugin/git/help-excludedRegions.html">
<f:textarea />
</f:entry>
Expand Down
19 changes: 19 additions & 0 deletions src/main/webapp/help-includedRegions.html
@@ -0,0 +1,19 @@
<div>
If set, and Jenkins is set to poll for changes, Jenkins will honor any files and/or
folders in this list when determining if a build needs to be triggered.
<p/>
Each inclusion uses regular expression pattern matching, and must be separated by a new line.
An empty list implies that everything is included.
<p/>
<pre>
myapp/src/main/web/.*\.html
myapp/src/main/web/.*\.jpeg
myapp/src/main/web/.*\.gif
</pre>
The example above illustrates that a build will only occur, if html/jpeg/gif files
have been committed to the SCM. Exclusions take precedence over inclusions, if there is
an overlap between included and excluded regions.
<p/>
More information on regular expressions can be found
<a href="http://www.regular-expressions.info/" target="_blank">here</a>.
</div>
53 changes: 43 additions & 10 deletions src/test/java/hudson/plugins/git/GitSCMTest.java
Expand Up @@ -66,7 +66,7 @@ public void testBasic() throws Exception {

public void testBasicRemotePoll() throws Exception {
// FreeStyleProject project = setupProject("master", true, false);
FreeStyleProject project = setupProject("master", false, null, null, null, true);
FreeStyleProject project = setupProject("master", false, null, null, null, true, null);
// create initial commit and then run the build against it:
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, "Commit number 1");
Expand All @@ -87,9 +87,38 @@ public void testBasicRemotePoll() throws Exception {
assertFalse("scm polling should not detect any more changes after build", project.pollSCMChanges(listener));
}

public void testBasicIncludedRegion() throws Exception {
FreeStyleProject project = setupProject("master", false, null, null, null, ".*3");

// create initial commit and then run the build against it:
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, "Commit number 1");
build(project, Result.SUCCESS, commitFile1);

assertFalse("scm polling should not detect any more changes after build", project.pollSCMChanges(listener));

final String commitFile2 = "commitFile2";
commit(commitFile2, janeDoe, "Commit number 2");
assertFalse("scm polling detected commit2 change, which should not have been included", project.pollSCMChanges(listener));

final String commitFile3 = "commitFile3";
commit(commitFile3, johnDoe, "Commit number 3");
assertTrue("scm polling did not detect commit3 change", project.pollSCMChanges(listener));

//... and build it...
final FreeStyleBuild build2 = build(project, Result.SUCCESS, commitFile2, commitFile3);
final Set<User> culprits = build2.getCulprits();
assertEquals("The build should have two culprit", 2, culprits.size());
assertEquals("", johnDoe.getName(), ((User)culprits.toArray()[0]).getFullName());
assertEquals("", janeDoe.getName(), ((User)culprits.toArray()[1]).getFullName());
assertTrue(build2.getWorkspace().child(commitFile2).exists());
assertTrue(build2.getWorkspace().child(commitFile3).exists());
assertBuildStatusSuccess(build2);
assertFalse("scm polling should not detect any more changes after build", project.pollSCMChanges(listener));
}

public void testBasicExcludedRegion() throws Exception {
FreeStyleProject project = setupProject("master", false, null, ".*2", null);
FreeStyleProject project = setupProject("master", false, null, ".*2", null, null);

// create initial commit and then run the build against it:
final String commitFile1 = "commitFile1";
Expand Down Expand Up @@ -145,7 +174,7 @@ public void testExcludedRegionMultiCommit() throws Exception {/*
*/}

public void testBasicExcludedUser() throws Exception {
FreeStyleProject project = setupProject("master", false, null, null, "Jane Doe");
FreeStyleProject project = setupProject("master", false, null, null, "Jane Doe", null);

// create initial commit and then run the build against it:
final String commitFile1 = "commitFile1";
Expand Down Expand Up @@ -484,27 +513,30 @@ private FreeStyleProject setupProject(String branchString, boolean authorOrCommi

private FreeStyleProject setupProject(String branchString, boolean authorOrCommitter,
String relativeTargetDir) throws Exception {
return setupProject(branchString, authorOrCommitter, relativeTargetDir, null, null);
return setupProject(branchString, authorOrCommitter, relativeTargetDir, null, null, null);
}

private FreeStyleProject setupProject(String branchString, boolean authorOrCommitter,
String relativeTargetDir,
String excludedRegions,
String excludedUsers) throws Exception {
return setupProject(branchString, authorOrCommitter, relativeTargetDir, excludedRegions, excludedUsers, null, false);
String excludedUsers,
String includedRegions) throws Exception {
return setupProject(branchString, authorOrCommitter, relativeTargetDir, excludedRegions, excludedUsers, null, false, includedRegions);
}

private FreeStyleProject setupProject(String branchString, boolean authorOrCommitter,
String relativeTargetDir,
String excludedRegions,
String excludedUsers,
boolean fastRemotePoll) throws Exception {
return setupProject(branchString, authorOrCommitter, relativeTargetDir, excludedRegions, excludedUsers, null, fastRemotePoll);
boolean fastRemotePoll,
String includedRegions) throws Exception {
return setupProject(branchString, authorOrCommitter, relativeTargetDir, excludedRegions, excludedUsers, null, fastRemotePoll, includedRegions);
}

private FreeStyleProject setupProject(String branchString, boolean authorOrCommitter,
String relativeTargetDir, String excludedRegions,
String excludedUsers, String localBranch, boolean fastRemotePoll) throws Exception {
String excludedUsers, String localBranch, boolean fastRemotePoll,
String includedRegions) throws Exception {
FreeStyleProject project = createFreeStyleProject();
project.setScm(new GitSCM(
null,
Expand All @@ -513,7 +545,8 @@ private FreeStyleProject setupProject(String branchString, boolean authorOrCommi
null,
false, Collections.<SubmoduleConfig>emptyList(), false,
false, new DefaultBuildChooser(), null, null, authorOrCommitter, relativeTargetDir, null,
excludedRegions, excludedUsers, localBranch, false, false, false, fastRemotePoll, null, null, false));
excludedRegions, excludedUsers, localBranch, false, false, false, fastRemotePoll, null, null, false,
includedRegions));
project.getBuildersList().add(new CaptureEnvironmentBuilder());
return project;
}
Expand Down

0 comments on commit 57f504f

Please sign in to comment.