Skip to content

Commit

Permalink
[JENKINS-21063] Badge now retrieves dryRun, version, and failedBuild …
Browse files Browse the repository at this point in the history
…info from build instead of storing it.

As a side effect, variables in M2ReleaseBadgeAction were removed which will affect builds persisted with older versions of the plugin.
Also, required version of Jenkins was bumped to 1.509.3.

Signed-off-by: Anders Hammar <anders@hammar.net>
  • Loading branch information
andham committed Dec 27, 2013
1 parent 8a166ef commit 32529b1
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 45 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -9,7 +9,7 @@
<parent>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>plugin</artifactId>
<version>1.509.2</version>
<version>1.509.3</version>
</parent>

<name>Jenkins Maven Release Plug-in Plug-in</name>
Expand Down
Expand Up @@ -25,6 +25,9 @@
package org.jvnet.hudson.plugins.m2release;

import hudson.model.BuildBadgeAction;
import hudson.model.Result;
import hudson.model.Run;
import jenkins.model.RunAction2;

/**
* The M2ReleaseBadgeAction displays a small icon next to any release build in the build history.
Expand All @@ -36,35 +39,14 @@
* @author domi
* @author teilo
*/
public class M2ReleaseBadgeAction implements BuildBadgeAction {
public class M2ReleaseBadgeAction implements BuildBadgeAction, RunAction2 {

@Deprecated
private transient String tooltipText; // kept for backwards compatibility

private boolean isDryRun;
private transient Run run;

/**
* Version number that was released.
* Construct a new BadgeIcon to a Maven release build.
*/
private String versionNumber;

private boolean failedBuild;

/**
* Construct a new BadgeIcon to a Maven release build. The build is set as successful.
*/
public M2ReleaseBadgeAction(String versionNumber, boolean isDryRun) {
this.versionNumber = versionNumber;
this.isDryRun = isDryRun;
this.failedBuild = false;
}

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

/**
Expand Down Expand Up @@ -100,7 +82,7 @@ public String getUrlName() {
public String getTooltipText() {
StringBuilder str = new StringBuilder();

if (isFailedBuild()) {
if (isFailedRelease()) {
str.append("Failed release");
} else {
str.append("Release");
Expand All @@ -116,26 +98,41 @@ public String getTooltipText() {

/**
* Gets the version number that was released.
*
* @return Can be <code>null</code> if we are dealing with very legacy data
* that doesn't contain this information.
*/
public String getVersionNumber() {
return versionNumber;
M2ReleaseArgumentsAction args = run.getAction(M2ReleaseArgumentsAction.class);
return args.getReleaseVersion();
}

/**
* Returns if the release was a dryRun or not.
*/
public boolean isDryRun() {
return isDryRun;
M2ReleaseArgumentsAction args = run.getAction(M2ReleaseArgumentsAction.class);
return args.isDryRun();
}

/**
* Marks the build as failed.
* Returns <code>true</code> if building the release failed.
*/
public void setFailedBuild(boolean isFailedBuild) {
this.failedBuild = isFailedBuild;
public boolean isFailedRelease() {
return !isSuccessfulBuild(run);
}

public boolean isFailedBuild() {
return failedBuild;
}
private boolean isSuccessfulBuild(Run run) {
Result result = run.getResult();
if (result != null) {
return result.isBetterOrEqualTo(Result.SUCCESS);
} else { // build is still in progress
return true;
}
}

public void onAttached(Run<?, ?> run) {
this.run = run;
}

public void onLoad(Run<?, ?> run) {
this.run = run;
}
}
Expand Up @@ -168,7 +168,7 @@ public void buildEnvVars(Map<String, String> env) {
}

build.addAction(new M2ReleaseArgumentInterceptorAction(buildGoals.toString(), args.getScmPassword()));
build.addAction(new M2ReleaseBadgeAction(args.getReleaseVersion(), args.isDryRun()));
build.addAction(new M2ReleaseBadgeAction());

return new Environment() {

Expand Down Expand Up @@ -220,11 +220,6 @@ public boolean tearDown(@SuppressWarnings("rawtypes") AbstractBuild bld, BuildLi
}
}

if (bld.getResult() != null && !bld.getResult().isBetterOrEqualTo(Result.SUCCESS)) {
M2ReleaseBadgeAction badge = bld.getAction(M2ReleaseBadgeAction.class);
badge.setFailedBuild(true);
}

if (args.isDryRun()) {
lstnr.getLogger().println("[M2Release] its only a dryRun, no need to mark it for keep");
}
Expand Down
@@ -1,6 +1,6 @@
<j:jelly xmlns:j="jelly:core">
<j:choose>
<j:when test="${it.isFailedBuild()}">
<j:when test="${it.isFailedRelease()}">
<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
@@ -0,0 +1,78 @@
/*
* The MIT License
*
* Copyright (c) 2011, Dominik Bartholdi
*
* 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 org.jvnet.hudson.plugins.m2release;

import hudson.maven.MavenModuleSet;
import hudson.maven.MavenModuleSetBuild;
import hudson.model.Result;
import hudson.tasks.Maven.MavenInstallation;

import org.jvnet.hudson.plugins.m2release.M2ReleaseBuildWrapper.DescriptorImpl;
import org.jvnet.hudson.test.ExtractResourceSCM;
import org.jvnet.hudson.test.HudsonTestCase;

public class M2ReleaseBadgeActionTest extends HudsonTestCase {

public void testBadgeForSuccessfulDryRunRelease() throws Exception {
MavenInstallation mavenInstallation = configureDefaultMaven();
final MavenModuleSetBuild build =
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());
assertEquals("1.0", badge.getVersionNumber());
}

public void testBadgeForFailedDryRunRelease() throws Exception {
MavenInstallation mavenInstallation = configureMaven3();
final MavenModuleSetBuild build =
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());
assertEquals("1.0", badge.getVersionNumber());
}

private MavenModuleSetBuild runDryRunRelease(String projectZip, String unpackedPom,
MavenInstallation mavenInstallation, Result expectedResult)
throws Exception {
MavenModuleSet m = createMavenProject();
m.setRootPOM(unpackedPom);
m.setMaven(mavenInstallation.getName());
m.setScm(new ExtractResourceSCM(getClass().getResource(projectZip)));
m.setGoals("dummygoal"); // non-dryRun build would fail with this goal

final M2ReleaseBuildWrapper wrapper =
new M2ReleaseBuildWrapper(DescriptorImpl.DEFAULT_RELEASE_GOALS, DescriptorImpl.DEFAULT_DRYRUN_GOALS,
false, false, false, "ENV", "USERENV", "PWDENV",
DescriptorImpl.DEFAULT_NUMBER_OF_RELEASE_BUILDS_TO_KEEP);
M2ReleaseArgumentsAction args = new M2ReleaseArgumentsAction();
args.setReleaseVersion("1.0");
args.setDevelopmentVersion("1.1-SNAPSHOT");
args.setDryRun(true);
m.getBuildWrappersList().add(wrapper);

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

0 comments on commit 32529b1

Please sign in to comment.