Skip to content

Commit

Permalink
[JENKINS-15160] add support for combining git hashs when passed downs…
Browse files Browse the repository at this point in the history
…tream

Don't combine with manually started builds
  • Loading branch information
cjo9900 committed Oct 26, 2012
1 parent 0d21dcc commit e442a79
Show file tree
Hide file tree
Showing 5 changed files with 161 additions and 14 deletions.
11 changes: 11 additions & 0 deletions src/main/java/hudson/plugins/git/GitRevisionBuildParameters.java
Expand Up @@ -39,7 +39,14 @@
* @author Kohsuke Kawaguchi
*/
public class GitRevisionBuildParameters extends AbstractBuildParameters {

private boolean combineQueuedCommits = false;

@DataBoundConstructor
public GitRevisionBuildParameters(boolean combineQueuedCommits) {
this.combineQueuedCommits = combineQueuedCommits;
}

public GitRevisionBuildParameters() {
}

Expand All @@ -54,6 +61,10 @@ public Action getAction(AbstractBuild<?,?> build, TaskListener listener) {
return new RevisionParameterAction(data.getLastBuiltRevision().getSha1String());
}

public boolean getCombineQueuedCommits() {
return combineQueuedCommits;
}

@Extension(optional=true)
public static class DescriptorImpl extends Descriptor<AbstractBuildParameters> {
@Override
Expand Down
60 changes: 46 additions & 14 deletions src/main/java/hudson/plugins/git/RevisionParameterAction.java
Expand Up @@ -25,6 +25,8 @@

import hudson.model.Action;
import hudson.model.InvisibleAction;
import hudson.model.queue.FoldableAction;
import hudson.model.Queue;
import hudson.model.Queue.QueueAction;
import hudson.Util;

Expand All @@ -40,14 +42,20 @@
* @author Kohsuke Kawaguchi
* @author Chris Johnson
*/
public class RevisionParameterAction extends InvisibleAction implements Serializable,QueueAction {
public class RevisionParameterAction extends InvisibleAction implements Serializable,QueueAction,FoldableAction {
/**
* SHA1, ref name, etc. that can be "git rev-parse"d into a specific commit.
*/
public final String commit;
public final String commit;
public final boolean combineCommits;

public RevisionParameterAction(String commit) {
this(commit, false);
}

public RevisionParameterAction(String commit, boolean combineCommits) {
this.commit = commit;
this.combineCommits = combineCommits;
}

public Revision toRevision(IGitAPI git) {
Expand All @@ -58,16 +66,16 @@ public Revision toRevision(IGitAPI git) {
return revision;
}

@Override
public String toString() {
return super.toString()+"[commit="+commit+"]";
}
@Override
public String toString() {
return super.toString()+"[commit="+commit+"]";
}

/**
* Returns whether the new item should be scheduled.
* An action should return true if the associated task is 'different enough' to warrant a separate execution.
/**
* Returns whether the new item should be scheduled.
* An action should return true if the associated task is 'different enough' to warrant a separate execution.
* from {@link #QueueAction}
*/
*/
public boolean shouldSchedule(List<Action> actions) {
/* Called in two cases
1. On the action attached to an existing queued item
Expand All @@ -77,16 +85,40 @@ public boolean shouldSchedule(List<Action> actions) {
in all other cases we do.
*/
List<RevisionParameterAction> otherActions = Util.filter(actions,RevisionParameterAction.class);

for (RevisionParameterAction action: otherActions) {
if(this.commit.equals(action.commit))
if(combineCommits) {
// we are combining commits so we never need to schedule another run.
// unless other job does not have a RevisionParameterAction (manual build)
if(otherActions.size() != 0)
return false;
} else {
for (RevisionParameterAction action: otherActions) {
if(this.commit.equals(action.commit))
return false;
}
}

// if we get to this point there were no matching actions so a new build is required
return true;
}

/**
* Folds this Action into another action already associated with item
* from {@link #FoldableAction}
*/
public void foldIntoExisting(Queue.Item item, Queue.Task owner, List<Action> otherActions) {
// only do this if we are asked to.
if(combineCommits) {
RevisionParameterAction existing = item.getAction(RevisionParameterAction.class);
if (existing!=null) {
//because we cannot modify the commit in the existing action remove it and add self
item.getActions().remove(existing);
item.getActions().add(this);
return;
}
// no CauseAction found, so add a copy of this one
item.getActions().add(this);
}
}

private static final long serialVersionUID = 1L;
private static final Logger LOGGER = Logger.getLogger(RevisionParameterAction.class.getName());
}
Expand Down
@@ -0,0 +1,30 @@
<!--
The MIT License
Copyright (c) 2004-2012, Chris Johnson
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.
-->

<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:entry field="combineQueuedCommits"
title="${%Combine Queued git hashes}">
<f:checkbox />
</f:entry>
</j:jelly>
@@ -0,0 +1,6 @@
<div>
This checkbox cause the all revisions to be be ingnored apart from the last one if a build is already in the queue.<br/>
Does not combine entries with builds of manually started downstream job that are queued. (Ones that do no have a git hash attached to them)<br/>
Warning: There is no consideration for multiple branches, or any other behaviour,
it is your responsibility to make sure that the hashes provided come from the same branch.
</div>
68 changes: 68 additions & 0 deletions src/test/java/hudson/plugins/git/RevisionParameterActionTest.java
Expand Up @@ -92,4 +92,72 @@ public void testCombiningScheduling3() throws Exception {
waitUntilNoActivity();
assertEquals(fs.getBuilds().size(),2);
}

/** test when a different revision is already in the queue, and combine requests is required.
*/
public void testCombiningScheduling4() throws Exception {

FreeStyleProject fs = createFreeStyleProject("freestyle");

// scheduleBuild2 returns null if request is combined into an existing item. (no new item added to queue)
Future b1 = fs.scheduleBuild2(20, null, Collections.singletonList(new RevisionParameterAction("DEADBEEF", true)));
Future b2 = fs.scheduleBuild2(20, null, Collections.singletonList(new RevisionParameterAction("FFEEFFEE", true)));

// Check that we have the correct futures.
assertNotNull(b1);
assertNull(b2);

// Check that only one build occured
waitUntilNoActivity();
assertEquals(fs.getBuilds().size(),1);

//check that the correct commit id is present in build
assertEquals(fs.getBuilds().get(0).getAction(RevisionParameterAction.class).commit, "FFEEFFEE");

}

/** test when a same revision is already in the queue, and combine requests is required.
*/
public void testCombiningScheduling5() throws Exception {

FreeStyleProject fs = createFreeStyleProject("freestyle");

// scheduleBuild2 returns null if request is combined into an existing item. (no new item added to queue)
Future b1 = fs.scheduleBuild2(20, null, Collections.singletonList(new RevisionParameterAction("DEADBEEF", true)));
Future b2 = fs.scheduleBuild2(20, null, Collections.singletonList(new RevisionParameterAction("DEADBEEF", true)));

// Check that we have the correct futures.
assertNotNull(b1);
assertNull(b2);

// Check that only one build occured
waitUntilNoActivity();
assertEquals(fs.getBuilds().size(),1);

//check that the correct commit id is present in build
assertEquals(fs.getBuilds().get(0).getAction(RevisionParameterAction.class).commit, "DEADBEEF");
}

/** test when a job already in the queue with no revision(manually started), and combine requests is required.
*/
public void testCombiningScheduling6() throws Exception {

FreeStyleProject fs = createFreeStyleProject("freestyle");

// scheduleBuild2 returns null if request is combined into an existing item. (no new item added to queue)
Future b1 = fs.scheduleBuild2(20);
Future b2 = fs.scheduleBuild2(20, null, Collections.singletonList(new RevisionParameterAction("DEADBEEF", true)));

// Check that we have the correct futures.
assertNotNull(b1);
assertNotNull(b2);

// Check that only one build occured
waitUntilNoActivity();
assertEquals(fs.getBuilds().size(),2);

//check that the correct commit id is present in 2nd build
// list is reversed indexed so first item is latest build
assertEquals(fs.getBuilds().get(0).getAction(RevisionParameterAction.class).commit, "DEADBEEF");
}
}

0 comments on commit e442a79

Please sign in to comment.