Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fixed JENKINS-44037] Add a new
BuildData
action if PreBuildMerge f…
…ails This fixes a buggy behavior where BuildData from the previous Run is used instead, which corrupts build history.
- Loading branch information
Dan Alvizu
committed
May 3, 2017
1 parent
b0a5ebf
commit 6b9bb1b
Showing
3 changed files
with
125 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
src/test/java/hudson/plugins/git/extensions/impl/PreBuildMergeTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
package hudson.plugins.git.extensions.impl; | ||
|
||
import hudson.model.FreeStyleBuild; | ||
import hudson.model.FreeStyleProject; | ||
import hudson.model.Result; | ||
import hudson.plugins.git.GitSCM; | ||
import hudson.plugins.git.Revision; | ||
import hudson.plugins.git.TestGitRepo; | ||
import hudson.plugins.git.UserMergeOptions; | ||
import hudson.plugins.git.extensions.GitSCMExtension; | ||
import hudson.plugins.git.extensions.GitSCMExtensionTest; | ||
import hudson.plugins.git.util.BuildData; | ||
import org.jenkinsci.plugins.gitclient.MergeCommand; | ||
import org.junit.Test; | ||
|
||
import static org.hamcrest.Matchers.containsString; | ||
import static org.junit.Assert.*; | ||
|
||
/** | ||
* @author dalvizu | ||
*/ | ||
public class PreBuildMergeTest extends GitSCMExtensionTest | ||
{ | ||
private FreeStyleProject project; | ||
|
||
private TestGitRepo repo; | ||
|
||
private String MASTER_FILE = "commitFileBase"; | ||
|
||
public void before() throws Exception { | ||
repo = new TestGitRepo("repo", tmp.newFolder(), listener); | ||
project = setupBasicProject(repo); | ||
// make an initial commit to master | ||
repo.commit(MASTER_FILE, repo.johnDoe, "Initial Commit"); | ||
// create integration branch | ||
repo.git.branch("integration"); | ||
} | ||
|
||
@Test | ||
public void testBasicPreMerge() throws Exception { | ||
FreeStyleBuild firstBuild = build(project, Result.SUCCESS); | ||
} | ||
|
||
@Test | ||
public void testFailedMerge() throws Exception { | ||
FreeStyleBuild firstBuild = build(project, Result.SUCCESS); | ||
assertEquals(GitSCM.class, project.getScm().getClass()); | ||
GitSCM gitSCM = (GitSCM)project.getScm(); | ||
BuildData buildData = gitSCM.getBuildData(firstBuild); | ||
assertNotNull("Build data not found", buildData); | ||
assertEquals(firstBuild.getNumber(), buildData.lastBuild.getBuildNumber()); | ||
Revision firstMarked = buildData.lastBuild.getMarked(); | ||
Revision firstRevision = buildData.lastBuild.getRevision(); | ||
assertNotNull(firstMarked); | ||
assertNotNull(firstRevision); | ||
|
||
// pretend we merged and published it successfully | ||
repo.git.deleteBranch("integration"); | ||
repo.git.checkoutBranch("integration", "master"); | ||
repo.commit(MASTER_FILE, "new content on integration branch", repo.johnDoe, repo.johnDoe, "Commit which should fail!"); | ||
repo.git.checkout("master"); | ||
|
||
// make a new commit in master branch, this commit should not merge cleanly! | ||
assertFalse("SCM polling should not detect any more changes after build", project.poll(listener).hasChanges()); | ||
String conflictSha1 = repo.commit(MASTER_FILE, "new content - expect a merge conflict!", repo.johnDoe, repo.johnDoe, "Commit which should fail!"); | ||
assertTrue("SCM polling should detect changes", project.poll(listener).hasChanges()); | ||
|
||
FreeStyleBuild secondBuild = build(project, Result.FAILURE); | ||
assertThat(secondBuild.getLog(), containsString("Automatic merge failed; fix conflicts and then commit the result.")); | ||
assertEquals(secondBuild.getNumber(), gitSCM.getBuildData(secondBuild).lastBuild.getBuildNumber()); | ||
// buildData should mark this as built | ||
assertEquals(conflictSha1, gitSCM.getBuildData(secondBuild).lastBuild.getMarked().getSha1String()); | ||
assertEquals(conflictSha1, gitSCM.getBuildData(secondBuild).lastBuild.getRevision().getSha1String()); | ||
|
||
// Check to see that build data is not corrupted (JENKINS-44037) | ||
assertEquals(firstBuild.getNumber(), gitSCM.getBuildData(firstBuild).lastBuild.getBuildNumber()); | ||
assertEquals(firstMarked, gitSCM.getBuildData(firstBuild).lastBuild.getMarked()); | ||
assertEquals(firstRevision, gitSCM.getBuildData(firstBuild).lastBuild.getRevision()); | ||
} | ||
|
||
@Override | ||
protected GitSCMExtension getExtension() { | ||
return new PreBuildMerge(new UserMergeOptions("origin", "integration", "default", | ||
MergeCommand.GitPluginFastForwardMode.FF)); | ||
} | ||
|
||
} |