Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #235 from galbramc/master
[FIXED JENKINS-23179] git pre-merge fails with matrix project
  • Loading branch information
MarkEWaite committed Aug 3, 2014
2 parents e1ea04c + 400f3e3 commit bbef8c0
Show file tree
Hide file tree
Showing 3 changed files with 97 additions and 27 deletions.
62 changes: 35 additions & 27 deletions src/main/java/hudson/plugins/git/GitSCM.java
Expand Up @@ -809,51 +809,59 @@ public EnvVars getEnvironment() {
final TaskListener listener) throws IOException, InterruptedException {
PrintStream log = listener.getLogger();

// every MatrixRun should build the exact same commit ID
Revision marked = null;

// every MatrixRun should build the same marked commit ID
if (build instanceof MatrixRun) {
MatrixBuild parentBuild = ((MatrixRun) build).getParentBuild();
if (parentBuild != null) {
BuildData parentBuildData = getBuildData(parentBuild);
if (parentBuildData != null) {
Build lastBuild = parentBuildData.lastBuild;
if (lastBuild!=null)
return lastBuild;
marked = lastBuild.getMarked();
}
}
}

// parameter forcing the commit ID to build
final RevisionParameterAction rpa = build.getAction(RevisionParameterAction.class);
if (rpa != null)
return new Build(rpa.toRevision(git), build.getNumber(), null);
if( marked == null ) {
// parameter forcing the commit ID to build
final RevisionParameterAction rpa = build.getAction(RevisionParameterAction.class);
if (rpa != null)
return new Build(rpa.toRevision(git), build.getNumber(), null);

final String singleBranch = environment.expand( getSingleBranch(environment) );
final String singleBranch = environment.expand( getSingleBranch(environment) );

final BuildChooserContext context = new BuildChooserContextImpl(build.getParent(), build, environment);
Collection<Revision> candidates = getBuildChooser().getCandidateRevisions(
false, singleBranch, git, listener, buildData, context);
final BuildChooserContext context = new BuildChooserContextImpl(build.getParent(), build, environment);
Collection<Revision> candidates = getBuildChooser().getCandidateRevisions(
false, singleBranch, git, listener, buildData, context);

if (candidates.size() == 0) {
// getBuildCandidates should make the last item the last build, so a re-build
// will build the last built thing.
throw new AbortException("Couldn't find any revision to build. Verify the repository and branch configuration for this job.");
}
if (candidates.size() == 0) {
// getBuildCandidates should make the last item the last build, so a re-build
// will build the last built thing.
throw new AbortException("Couldn't find any revision to build. Verify the repository and branch configuration for this job.");
}

if (candidates.size() > 1) {
log.println("Multiple candidate revisions");
Job<?, ?> job = build.getParent();
if (job instanceof AbstractProject) {
AbstractProject project = (AbstractProject) job;
if (!project.isDisabled()) {
log.println("Scheduling another build to catch up with " + project.getFullDisplayName());
if (!project.scheduleBuild(0, new SCMTrigger.SCMTriggerCause())) {
log.println("WARNING: multiple candidate revisions, but unable to schedule build of " + project.getFullDisplayName());
if (candidates.size() > 1) {
log.println("Multiple candidate revisions");
Job<?, ?> job = build.getParent();
if (job instanceof AbstractProject) {
AbstractProject project = (AbstractProject) job;
if (!project.isDisabled()) {
log.println("Scheduling another build to catch up with " + project.getFullDisplayName());
if (!project.scheduleBuild(0, new SCMTrigger.SCMTriggerCause())) {
log.println("WARNING: multiple candidate revisions, but unable to schedule build of " + project.getFullDisplayName());
}
}
}

}
}

marked = candidates.iterator().next();
}
Revision rev = candidates.iterator().next();
Revision marked = rev;

Revision rev = marked;
//Modify the revision based on extensions
for (GitSCMExtension ext : extensions) {
rev = ext.decorateRevisionToBuild(this,build,git,listener,rev);
}
Expand Down
15 changes: 15 additions & 0 deletions src/test/java/hudson/plugins/git/AbstractGitTestCase.java
Expand Up @@ -3,6 +3,8 @@
import static org.apache.commons.lang.StringUtils.isBlank;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.matrix.MatrixBuild;
import hudson.matrix.MatrixProject;
import hudson.model.FreeStyleBuild;
import hudson.model.Hudson;
import hudson.model.Result;
Expand Down Expand Up @@ -229,6 +231,19 @@ protected FreeStyleBuild build(final FreeStyleProject project, final String pare
}
return build;
}

protected MatrixBuild build(final MatrixProject project, final Result expectedResult, final String...expectedNewlyCommittedFiles) throws Exception {
final MatrixBuild build = project.scheduleBuild2(0, new Cause.UserCause()).get();
System.out.println(build.getLog());
for(final String expectedNewlyCommittedFile : expectedNewlyCommittedFiles) {
assertTrue(expectedNewlyCommittedFile + " file not found in workspace", build.getWorkspace().child(expectedNewlyCommittedFile).exists());
}
if(expectedResult != null) {
assertBuildStatus(expectedResult, build);
}
return build;
}


protected EnvVars getEnvVars(FreeStyleProject project) {
for (hudson.tasks.Builder b : project.getBuilders()) {
Expand Down
47 changes: 47 additions & 0 deletions src/test/java/hudson/plugins/git/GitSCMTest.java
Expand Up @@ -4,6 +4,10 @@

import hudson.EnvVars;
import hudson.FilePath;
import hudson.matrix.Axis;
import hudson.matrix.AxisList;
import hudson.matrix.MatrixBuild;
import hudson.matrix.MatrixProject;
import hudson.model.*;
import hudson.plugins.git.GitSCM.BuildChooserContextImpl;
import hudson.plugins.git.browser.GitRepositoryBrowser;
Expand Down Expand Up @@ -997,6 +1001,49 @@ public void testMergeFailedWithSlave() throws Exception {
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
}


public void testMergeWithMatrixBuild() throws Exception {

//Create a matrix project and a couple of axes
MatrixProject project = createMatrixProject("xyz");
project.setAxes(new AxisList(new Axis("VAR","a","b")));

GitSCM scm = new GitSCM(
createRemoteRepositories(),
Collections.singletonList(new BranchSpec("*")),
false, Collections.<SubmoduleConfig>emptyList(),
null, null,
Collections.<GitSCMExtension>emptyList());
scm.getExtensions().add(new PreBuildMerge(new UserMergeOptions("origin", "integration", null)));
project.setScm(scm);

// create initial commit and then run the build against it:
commit("commitFileBase", johnDoe, "Initial Commit");
testRepo.git.branch("integration");
build(project, Result.SUCCESS, "commitFileBase");


testRepo.git.checkout(null, "topic1");
final String commitFile1 = "commitFile1";
commit(commitFile1, johnDoe, "Commit number 1");
final MatrixBuild build1 = build(project, Result.SUCCESS, commitFile1);
assertTrue(build1.getWorkspace().child(commitFile1).exists());

assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
// do what the GitPublisher would do
testRepo.git.deleteBranch("integration");
testRepo.git.checkout("topic1", "integration");

testRepo.git.checkout("master", "topic2");
final String commitFile2 = "commitFile2";
commit(commitFile2, johnDoe, "Commit number 2");
assertTrue("scm polling did not detect commit2 change", project.poll(listener).hasChanges());
final MatrixBuild build2 = build(project, Result.SUCCESS, commitFile2);
assertTrue(build2.getWorkspace().child(commitFile2).exists());
assertBuildStatusSuccess(build2);
assertFalse("scm polling should not detect any more changes after build", project.poll(listener).hasChanges());
}

public void testEnvironmentVariableExpansion() throws Exception {
FreeStyleProject project = createFreeStyleProject();
project.setScm(new GitSCM("${CAT}"+testRepo.gitDir.getPath()));
Expand Down

0 comments on commit bbef8c0

Please sign in to comment.