Skip to content

Commit

Permalink
Merge pull request #31 from solganik/master
Browse files Browse the repository at this point in the history
adding an option to ignore specific projects on scm poll.
  • Loading branch information
rsandell committed Feb 22, 2016
2 parents 71446a3 + fd0763a commit 153655b
Show file tree
Hide file tree
Showing 4 changed files with 122 additions and 1 deletion.
60 changes: 59 additions & 1 deletion src/main/java/hudson/plugins/repo/RepoScm.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,11 @@
import java.io.Serializable;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;

Expand Down Expand Up @@ -97,6 +101,7 @@ public class RepoScm extends SCM implements Serializable {
@CheckForNull private boolean trace;
@CheckForNull private boolean showAllChanges;
@CheckForNull private boolean noTags;
@CheckForNull private Set<String> ignoreProjects;

/**
* Returns the manifest repository URL.
Expand Down Expand Up @@ -221,6 +226,14 @@ public String getDestinationDir() {
return destinationDir;
}

/**
* returns list of ignore projects.
*/
@Exported
public String getIgnoreProjects() {
return StringUtils.join(ignoreProjects, '\n');
}

/**
* Returns the value of currentBranch.
*/
Expand Down Expand Up @@ -300,6 +313,7 @@ public boolean isForceSync() {
* executing "repo init" and "repo sync".
* @param showAllChanges If this value is true, add the "--first-parent" option to
* "git log" when determining changesets.
*
*/
@Deprecated
public RepoScm(final String manifestRepositoryUrl,
Expand Down Expand Up @@ -328,6 +342,7 @@ public RepoScm(final String manifestRepositoryUrl,
setTrace(trace);
setShowAllChanges(showAllChanges);
setRepoUrl(repoUrl);
ignoreProjects = Collections.<String>emptySet();
}

/**
Expand Down Expand Up @@ -355,6 +370,7 @@ public RepoScm(final String manifestRepositoryUrl) {
trace = false;
showAllChanges = false;
noTags = false;
ignoreProjects = Collections.<String>emptySet();
}

/**
Expand Down Expand Up @@ -553,6 +569,23 @@ public final void setNoTags(final boolean noTags) {
this.noTags = noTags;
}

/**
* Sets list of projects which changes will be ignored when
* calculating whether job needs to be rebuild. This field corresponds
* to serverpath i.e. "name" section of the manifest.
* @param ignoreProjects
* String representing project names separated by " ".
*/
@DataBoundSetter
public final void setIgnoreProjects(final String ignoreProjects) {
if (ignoreProjects == null) {
this.ignoreProjects = Collections.<String>emptySet();
return;
}
this.ignoreProjects = new LinkedHashSet<String>(
Arrays.asList(ignoreProjects.split("\\s+")));
}

@Override
public SCMRevisionState calcRevisionsFromBuild(
@Nonnull final Run<?, ?> build, @Nullable final FilePath workspace,
Expand All @@ -565,6 +598,26 @@ public SCMRevisionState calcRevisionsFromBuild(
return null;
}

private boolean shouldIgnoreChanges(final RevisionState current, final RevisionState baseline) {
List<ProjectState> changedProjects = current.whatChanged(baseline);
if ((changedProjects == null) || (ignoreProjects == null)) {
return false;
}
if (ignoreProjects.isEmpty()) {
return false;
}


// Check for every changed item if it is not contained in the
// ignored setting .. project must be rebuilt
for (ProjectState changed : changedProjects) {
if (!ignoreProjects.contains(changed.getServerPath())) {
return false;
}
}
return true;
}

@Override
public PollingResult compareRemoteRevisionWith(
@Nonnull final Job<?, ?> job, @Nullable final Launcher launcher,
Expand Down Expand Up @@ -605,11 +658,16 @@ public PollingResult compareRemoteRevisionWith(
getStaticManifest(launcher, repoDir, listener.getLogger()),
getManifestRevision(launcher, repoDir, listener.getLogger()),
expandedManifestBranch, listener.getLogger());

final Change change;
if (currentState.equals(myBaseline)) {
change = Change.NONE;
} else {
change = Change.SIGNIFICANT;
if (shouldIgnoreChanges(currentState, (RevisionState) myBaseline)) {
change = Change.NONE;
} else {
change = Change.SIGNIFICANT;
}
}
return new PollingResult(myBaseline, currentState, change);
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/resources/hudson/plugins/repo/RepoScm/config.jelly
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
<f:entry title="Group" help="/plugin/repo/help-manifestGroup.html">
<f:textbox name="repo.manifestGroup" value="${scm.manifestGroup}" />
</f:entry>

<f:entry title="IgnoreChanges" help="/plugin/repo/help-ignoreChanges.html">
<f:textarea name="repo.ignoreProjects" value="${scm.ignoreProjects}" rows="3" />
</f:entry>

<f:entry title="Destination Directory" help="/plugin/repo/help-destinationDir.html">
<f:textbox name="repo.destinationDir" value="${scm.destinationDir}" />
Expand Down
6 changes: 6 additions & 0 deletions src/main/webapp/help-ignoreChanges.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<div>
<p>
Specify projects changes in which would not be considered a change that requires project rebuild when polling scm.
Project should be specified as the name in the <code>name</code> section of the <code>project</code> declaration in manifest separated by spaces.
</p>
</div>
53 changes: 53 additions & 0 deletions src/test/java/hudson/plugins/repo/TestRepoScm.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* The MIT License
*
* Copyright (c) 2011, Brad Larson
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package hudson.plugins.repo;

import java.util.ArrayList;
import java.util.List;

import org.junit.Assert;

import junit.framework.TestCase;

/**
* Test cases for the {@link RevisionState} class.
*/
public class TestRepoScm extends TestCase {


public void testSetIgnoredProjects() {
RepoScm scm = new RepoScm("http://manifesturl");
scm.setIgnoreProjects("");
assertEquals("", scm.getIgnoreProjects());

}

public void testSetIgnoredProjectsKeepsOrder() {
RepoScm scm = new RepoScm("http://manifesturl");
scm.setIgnoreProjects("projecta projectb");
assertEquals("projecta\nprojectb", scm.getIgnoreProjects());
scm.setIgnoreProjects("projectb projecta");
assertEquals("projectb\nprojecta", scm.getIgnoreProjects());
}
}

0 comments on commit 153655b

Please sign in to comment.