Skip to content
Permalink
Browse files

Merge branch 'JENKINS-21063' of https://github.com/andham/m2release-p…

…lugin into andham-JENKINS-21063
  • Loading branch information
imod committed Mar 30, 2014
2 parents 7ee2284 + 35d201a commit aaba60e7307eea50ebc377403562364896f3d968
@@ -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>
@@ -25,43 +25,45 @@
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 builds in the build history.
*
* 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 had happened.
* other plugins can introspect that the release has happened.
*
* @author domi
* @author teilo
*/
public class M2ReleaseBadgeAction implements BuildBadgeAction {
public class M2ReleaseBadgeAction implements BuildBadgeAction, RunAction2 {

private transient Run<?, ?> run;

/** The tooltip text displayed to the user with the badge. */
private transient String tooltipText;

private boolean isDryRun;
@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.
*/
private String versionNumber;
@Deprecated
private transient String versionNumber; // kept for backwards compatibility

/**
* Construct a new BadgeIcon to a Maven release build.
*
* @param tooltipText
* the tool tip text that should be displayed with the badge.
*/
public M2ReleaseBadgeAction(String versionNumber, boolean isDryRun) {
this.versionNumber = versionNumber;
this.isDryRun = isDryRun;
public M2ReleaseBadgeAction() {
}

public Object readResolve() {
// try to recover versionNumber from tooltipText
if (versionNumber == null && tooltipText.startsWith("Release - ")) {
// 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;
@@ -95,23 +97,76 @@ public String getUrlName() {
}

/**
* Gets the tool tip text that should be displayed to the user.
* Gets the tooltip text that should be displayed to the user.
*/
public String getTooltipText() {
return isDryRun ? "Release (dryRun) - " + versionNumber : "Release - " + versionNumber;
StringBuilder str = new StringBuilder();

if (isFailedBuild()) {
str.append("Failed release");
} else {
str.append("Release");
}
if (isDryRun()) {
str.append(" (dryRun)");
}
str.append(" - ");
str.append(getVersionNumber());

return str.toString();
}

/**
* 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;
if (versionNumber != null) {
return versionNumber;
}
M2ReleaseArgumentsAction args = run.getAction(M2ReleaseArgumentsAction.class);
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() {
return isDryRun;
if (isDryRun != null) {
return isDryRun;
}
M2ReleaseArgumentsAction args = run.getAction(M2ReleaseArgumentsAction.class);
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 the release build job failed.
*/
public boolean isFailedBuild() {
return !isSuccessfulBuild(run);
}

private boolean isSuccessfulBuild(Run<?, ?> run) {
Result result = run.getResult();
if (result != null) {
return result.isBetterOrEqualTo(Result.SUCCESS);
} else { // build is not yet initiated
return true;
}
}

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

public void onLoad(Run<?, ?> run) {
this.run = run;
}
}
@@ -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() {

@@ -184,13 +184,9 @@ public void buildEnvVars(Map<String, String> env) {
public boolean tearDown(@SuppressWarnings("rawtypes") AbstractBuild bld, BuildListener lstnr)
throws IOException, InterruptedException {
boolean retVal = true;

final MavenModuleSet mmSet = getModuleSet(bld);
M2ReleaseArgumentsAction args = bld.getAction(M2ReleaseArgumentsAction.class);

if (args.isDryRun()) {
lstnr.getLogger().println("[M2Release] its only a dryRun, no need to mark it for keep");
}

if (args.isCloseNexusStage() && !args.isDryRun()) {
StageClient client = new StageClient(new URL(getDescriptor().getNexusURL()), getDescriptor()
.getNexusUser(), getDescriptor().getNexusPassword());
@@ -223,6 +219,10 @@ public boolean tearDown(@SuppressWarnings("rawtypes") AbstractBuild bld, BuildLi
retVal = false;
}
}

if (args.isDryRun()) {
lstnr.getLogger().println("[M2Release] its only a dryRun, no need to mark it for keep");
}
int buildsKept = 0;
if (bld.getResult() != null && bld.getResult().isBetterOrEqualTo(Result.SUCCESS) && !args.isDryRun()) {
if (numberOfReleaseBuildsToKeep > 0 || numberOfReleaseBuildsToKeep == -1) {
@@ -266,7 +266,7 @@ public boolean tearDown(@SuppressWarnings("rawtypes") AbstractBuild bld, BuildLi
}

/**
* evaluate if the specified build is a sucessful release build (not including dry runs)
* evaluate if the specified build is a successful release build (not including dry runs)
* @param run the run to check
* @return <code>true</code> if this is a successful release build that is not a dry run.
*/
@@ -1,8 +1,13 @@
<j:jelly xmlns:j="jelly:core">
<j:if test="${it.isDryRun()}">
<img width="16" height="16" title="${it.tooltipText}" src="${rootURL}/plugin/m2release/img/new-package.png"/>
</j:if>
<j:if test="${!it.isDryRun()}">
<img width="16" height="16" title="${it.tooltipText}" src="${imagesURL}/16x16/package.png"/>
</j:if>
</j:jelly>
<j:choose>
<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()}">
<img width="16" height="16" title="${it.tooltipText}" src="${rootURL}/plugin/m2release/img/releasebadge-dryrun.png"/>
</j:when>
<j:otherwise>
<img width="16" height="16" title="${it.tooltipText}" src="${rootURL}/plugin/m2release/img/releasebadge.png"/>
</j:otherwise>
</j:choose>
</j:jelly>
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@@ -0,0 +1,118 @@
/*
* 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 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;
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.isFailedBuild());
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.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());
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);

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
}
}
}
Binary file not shown.

0 comments on commit aaba60e

Please sign in to comment.
You can’t perform that action at this time.