Skip to content

Commit

Permalink
[JENKINS-21063] Changed failed badge to indicate uncertainty wrt rele…
Browse files Browse the repository at this point in the history
…ase and fixed backwards campatibility for builds by old versions of plugin.

Signed-off-by: Anders Hammar <anders@hammar.net>
  • Loading branch information
andham committed Jan 16, 2014
1 parent 32529b1 commit 1f8dc39
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 14 deletions.
Expand Up @@ -31,7 +31,7 @@

/**
* The M2ReleaseBadgeAction displays a small icon next to any release build in the build history.
*
*
* <p>
* This object also remembers the release in a machine readable form so that
* other plugins can introspect that the release has happened.
Expand All @@ -41,14 +41,34 @@
*/
public class M2ReleaseBadgeAction implements BuildBadgeAction, RunAction2 {

private transient Run run;
private transient Run<?, ?> run;

@Deprecated
private transient String tooltipText; // kept for backwards compatibility (very old versions of plugin)

@Deprecated
private transient Boolean isDryRun; // kept for backwards compatibility

/**
* Version number that was released.
*/
@Deprecated
private transient String versionNumber; // kept for backwards compatibility

/**
* Construct a new BadgeIcon to a Maven release build.
*/
public M2ReleaseBadgeAction() {
}

public Object readResolve() {
// try to recover versionNumber from tooltipText (for builds by very old versions of the plugin)
if (versionNumber == null && tooltipText != null && tooltipText.startsWith("Release - ")) {
versionNumber = tooltipText.substring("Release - ".length());
}
return this;
}

/**
* Gets the string to be displayed.
*
Expand Down Expand Up @@ -82,7 +102,7 @@ public String getUrlName() {
public String getTooltipText() {
StringBuilder str = new StringBuilder();

if (isFailedRelease()) {
if (isFailedBuild()) {
str.append("Failed release");
} else {
str.append("Release");
Expand All @@ -100,30 +120,44 @@ public String getTooltipText() {
* Gets the version number that was released.
*/
public String getVersionNumber() {
if (versionNumber != null) {
return versionNumber;
}
M2ReleaseArgumentsAction args = run.getAction(M2ReleaseArgumentsAction.class);
return args.getReleaseVersion();
if (args != null) {
return args.getReleaseVersion();
} else { // builds by old versions of the plugin
return null;
}
}

/**
* Returns if the release was a dryRun or not.
*/
public boolean isDryRun() {
if (isDryRun != null) {
return isDryRun;
}
M2ReleaseArgumentsAction args = run.getAction(M2ReleaseArgumentsAction.class);
return args.isDryRun();
if (args != null) {
return args.isDryRun();
} else { // builds by old versions of the plugin
return false; // we don't know
}
}

/**
* Returns <code>true</code> if building the release failed.
* Returns <code>true</code> if the release build job failed.
*/
public boolean isFailedRelease() {
return !isSuccessfulBuild(run);
}
public boolean isFailedBuild() {
return !isSuccessfulBuild(run);
}

private boolean isSuccessfulBuild(Run run) {
private boolean isSuccessfulBuild(Run<?, ?> run) {
Result result = run.getResult();
if (result != null) {
return result.isBetterOrEqualTo(Result.SUCCESS);
} else { // build is still in progress
} else { // build is not yet initiated
return true;
}
}
Expand Down
@@ -1,6 +1,6 @@
<j:jelly xmlns:j="jelly:core">
<j:choose>
<j:when test="${it.isFailedRelease()}">
<j:when test="${it.isFailedBuild()}">
<img width="16" height="16" title="${it.tooltipText}" src="${rootURL}/plugin/m2release/img/releasebadge-failure.png"/>
</j:when>
<j:when test="${it.isDryRun()}">
Expand Down
Binary file modified src/main/webapp/img/releasebadge-failure.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Expand Up @@ -23,9 +23,15 @@
*/
package org.jvnet.hudson.plugins.m2release;

import java.io.IOException;

import hudson.Launcher;
import hudson.maven.MavenModuleSet;
import hudson.maven.MavenModuleSetBuild;
import hudson.model.BuildListener;
import hudson.model.Result;
import hudson.model.AbstractBuild;
import hudson.tasks.Builder;
import hudson.tasks.Maven.MavenInstallation;

import org.jvnet.hudson.plugins.m2release.M2ReleaseBuildWrapper.DescriptorImpl;
Expand All @@ -40,7 +46,7 @@ public void testBadgeForSuccessfulDryRunRelease() throws Exception {
runDryRunRelease("maven2-project.zip", "pom.xml", mavenInstallation, Result.SUCCESS);
M2ReleaseBadgeAction badge = build.getAction(M2ReleaseBadgeAction.class);
assertTrue("Badge is not marked as dryRun", badge.isDryRun());
assertFalse("Badge should not have been marked as failed release", badge.isFailedRelease());
assertFalse("Badge should not have been marked as failed release", badge.isFailedBuild());
assertEquals("1.0", badge.getVersionNumber());
}

Expand All @@ -50,13 +56,35 @@ public void testBadgeForFailedDryRunRelease() throws Exception {
runDryRunRelease("maven3-failing-project.zip", "pom.xml", mavenInstallation, Result.FAILURE);
M2ReleaseBadgeAction badge = build.getAction(M2ReleaseBadgeAction.class);
assertTrue("Badge is not marked as dryRun", badge.isDryRun());
assertTrue("Badge should have been marked as failed release", badge.isFailedRelease());
assertTrue("Badge should have been marked as failed release", badge.isFailedBuild());
assertEquals("1.0", badge.getVersionNumber());
}

public void testBadgeForFailedPostBuildStepRelease() throws Exception {
MavenInstallation mavenInstallation = configureMaven3();
final MavenModuleSetBuild build =
runDryRunReleaseWithFailingPostStep("maven3-failing-project.zip", "pom.xml", mavenInstallation, Result.FAILURE);
M2ReleaseBadgeAction badge = build.getAction(M2ReleaseBadgeAction.class);
assertTrue("Badge should have been marked as failed release", badge.isFailedBuild());
assertEquals("1.0", badge.getVersionNumber());
}

private MavenModuleSetBuild runDryRunRelease(String projectZip, String unpackedPom,
MavenInstallation mavenInstallation, Result expectedResult)
throws Exception {
return runDryRunRelease(projectZip, unpackedPom, mavenInstallation, expectedResult, null);
}

private MavenModuleSetBuild runDryRunReleaseWithFailingPostStep(String projectZip, String unpackedPom,
MavenInstallation mavenInstallation, Result expectedResult)
throws Exception {
Builder failingPostStep = new FailingBuilder();
return runDryRunRelease(projectZip, unpackedPom, mavenInstallation, expectedResult, failingPostStep);
}

private MavenModuleSetBuild runDryRunRelease(String projectZip, String unpackedPom,
MavenInstallation mavenInstallation, Result expectedResult, Builder postStepBuilder)
throws Exception {
MavenModuleSet m = createMavenProject();
m.setRootPOM(unpackedPom);
m.setMaven(mavenInstallation.getName());
Expand All @@ -73,6 +101,18 @@ private MavenModuleSetBuild runDryRunRelease(String projectZip, String unpackedP
args.setDryRun(true);
m.getBuildWrappersList().add(wrapper);

if (postStepBuilder != null) {
m.getPostbuilders().add(postStepBuilder);
}

return assertBuildStatus(expectedResult, m.scheduleBuild2(0, new ReleaseCause(), args).get());
}

private static class FailingBuilder extends Builder {
@Override
public boolean perform(AbstractBuild<?, ?> build, Launcher launcher, BuildListener listener)
throws InterruptedException, IOException {
return false; // failing build
}
}
}

0 comments on commit 1f8dc39

Please sign in to comment.