Skip to content

Commit

Permalink
Merge pull request #107 from cjo9900/JENKINS-15160
Browse files Browse the repository at this point in the history
Jenkins 15160
  • Loading branch information
ndeloof committed Oct 27, 2012
2 parents 513d89b + e442a79 commit 17132ba
Show file tree
Hide file tree
Showing 5 changed files with 279 additions and 7 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
76 changes: 69 additions & 7 deletions src/main/java/hudson/plugins/git/RevisionParameterAction.java
Expand Up @@ -23,25 +23,39 @@
*/
package hudson.plugins.git;

import hudson.model.Action;
import hudson.model.InvisibleAction;
import org.eclipse.jgit.lib.ObjectId;
import hudson.model.queue.FoldableAction;
import hudson.model.Queue;
import hudson.model.Queue.QueueAction;
import hudson.Util;

import org.eclipse.jgit.lib.ObjectId;
import java.io.Serializable;
import java.util.List;
import java.util.logging.Logger;


/**
* Used as a build parameter to specify the revision to be built.
*
* @author Kohsuke Kawaguchi
* @author Chris Johnson
*/
public class RevisionParameterAction extends InvisibleAction implements Serializable {
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 @@ -52,10 +66,58 @@ 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.
* from {@link #QueueAction}
*/
public boolean shouldSchedule(List<Action> actions) {
/* Called in two cases
1. On the action attached to an existing queued item
2. On the action attached to the new item to add.
Behaviour
If actions contain a RevisionParameterAction with a matching commit to this one, we do not need to schedule
in all other cases we do.
*/
List<RevisionParameterAction> otherActions = Util.filter(actions,RevisionParameterAction.class);
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>
163 changes: 163 additions & 0 deletions src/test/java/hudson/plugins/git/RevisionParameterActionTest.java
@@ -0,0 +1,163 @@
/*
* The MIT License
*
* Copyright (c) 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.
*/
package hudson.plugins.git;

import hudson.model.FreeStyleProject;
import org.jvnet.hudson.test.HudsonTestCase;

import java.util.concurrent.Future;
import java.util.Collections;

/**
* Tests for {@link RevisionParameterAction}
*
* @author Chris Johnson
*/
public class RevisionParameterActionTest extends HudsonTestCase {

/**
* Test covering the behaviour after 1.1.26 where passing different revision
* actions to a job in the queue creates seperate builds
*/
public void testCombiningScheduling() 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")));
Future b2 = fs.scheduleBuild2(20, null, Collections.singletonList(new RevisionParameterAction("FREED456")));

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

// Check that only one build occured
waitUntilNoActivity();
assertEquals(fs.getBuilds().size(),2);
}
/** test when existing revision is already in the queue
*/
public void testCombiningScheduling2() 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")));
Future b2 = fs.scheduleBuild2(20, null, Collections.singletonList(new RevisionParameterAction("DEADBEEF")));

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

// Check that only one build occured
waitUntilNoActivity();
assertEquals(fs.getBuilds().size(),1);
}
/** test when there is no revision on the item in the queue
*/
public void testCombiningScheduling3() 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")));

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

// Check that only one build occured
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 17132ba

Please sign in to comment.