Skip to content

Commit

Permalink
[FIXED JENKINS-10006] Parameterized publishing
Browse files Browse the repository at this point in the history
  • Loading branch information
bap2000 committed Jul 8, 2011
1 parent 8d8a8e2 commit e639bd8
Show file tree
Hide file tree
Showing 10 changed files with 480 additions and 21 deletions.
35 changes: 31 additions & 4 deletions src/main/java/jenkins/plugins/publish_over/BPInstanceConfig.java
Expand Up @@ -51,16 +51,18 @@ public class BPInstanceConfig<PUBLISHER extends BapPublisher> implements Seriali
private boolean alwaysPublishFromMaster;
private String masterNodeName;
private BPHostConfigurationAccess hostConfigurationAccess;
private ParamPublish paramPublish;

public BPInstanceConfig() { }

public BPInstanceConfig(final ArrayList<PUBLISHER> publishers, final boolean continueOnError, final boolean failOnError,
final boolean alwaysPublishFromMaster, final String masterNodeName) {
final boolean alwaysPublishFromMaster, final String masterNodeName, final ParamPublish paramPublish) {
setPublishers(publishers);
this.continueOnError = continueOnError;
this.failOnError = failOnError;
this.alwaysPublishFromMaster = alwaysPublishFromMaster;
this.masterNodeName = masterNodeName;
this.paramPublish = paramPublish;
}

public final ArrayList<PUBLISHER> getPublishers() {
Expand Down Expand Up @@ -92,6 +94,10 @@ public final void setHostConfigurationAccess(final BPHostConfigurationAccess hos
this.hostConfigurationAccess = hostConfigurationAccess;
}

public ParamPublish getParamPublish() {
return paramPublish;
}

public BPHostConfiguration getConfiguration(final String configName) {
final BPHostConfiguration config = hostConfigurationAccess.getConfiguration(configName);
if (config == null)
Expand All @@ -113,8 +119,17 @@ public final Result perform(final BPBuildInfo buildInfo) {
Result toReturn = Result.SUCCESS;
final Result onError = failOnError ? Result.FAILURE : Result.UNSTABLE;
if (masterNodeName != null) fixMasterNodeName(buildInfo);
PubSelector selector = null;
try {
selector = createSelector(buildInfo);
} catch (BapPublisherException bpe) {
LOGGER.log(Level.WARNING, bpe.getLocalizedMessage(), bpe);
buildInfo.getListener().error(bpe.getLocalizedMessage());
return onError;
}
for (PUBLISHER publisher : publishers) {
publisher.setEffectiveEnvironmentInBuildInfo(buildInfo);
if (!selector.selected(publisher)) continue;
try {
final BPHostConfiguration hostConfig = getConfiguration(publisher.getConfigName());
final BPCallablePublisher callablePublisher = new BPCallablePublisher(publisher, hostConfig, buildInfo);
Expand All @@ -134,25 +149,37 @@ public final Result perform(final BPBuildInfo buildInfo) {
return toReturn;
}

private PubSelector createSelector(final BPBuildInfo buildInfo) {
if (paramPublish != null)
return paramPublish.createSelector(buildInfo);
return new PubSelector() {
public boolean selected(final BapPublisher publisher) {
return true;
}
};
}

protected HashCodeBuilder addToHashCode(final HashCodeBuilder builder) {
return builder.append(publishers).append(continueOnError).append(failOnError)
.append(alwaysPublishFromMaster).append(masterNodeName);
.append(alwaysPublishFromMaster).append(masterNodeName).append(paramPublish);
}

protected EqualsBuilder addToEquals(final EqualsBuilder builder, final BPInstanceConfig that) {
return builder.append(publishers, that.publishers)
.append(continueOnError, that.continueOnError)
.append(failOnError, that.failOnError)
.append(masterNodeName, that.masterNodeName)
.append(alwaysPublishFromMaster, that.alwaysPublishFromMaster);
.append(alwaysPublishFromMaster, that.alwaysPublishFromMaster)
.append(paramPublish, that.paramPublish);
}

protected ToStringBuilder addToToString(final ToStringBuilder builder) {
return builder.append("publishers", publishers)
.append("continueOnError", continueOnError)
.append("failOnError", failOnError)
.append("masterNodeName", masterNodeName)
.append("alwaysPublishFromMaster", alwaysPublishFromMaster);
.append("alwaysPublishFromMaster", alwaysPublishFromMaster)
.append("paramPublish", paramPublish);
}

public boolean equals(final Object that) {
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/jenkins/plugins/publish_over/BPPlugin.java
Expand Up @@ -55,8 +55,10 @@ public abstract class BPPlugin<PUBLISHER extends BapPublisher, CLIENT extends BP
private BPInstanceConfig delegate;

public BPPlugin(final String consolePrefix, final ArrayList<PUBLISHER> publishers, final boolean continueOnError,
final boolean failOnError, final boolean alwaysPublishFromMaster, final String masterNodeName) {
this.delegate = new BPInstanceConfig<PUBLISHER>(publishers, continueOnError, failOnError, alwaysPublishFromMaster, masterNodeName);
final boolean failOnError, final boolean alwaysPublishFromMaster, final String masterNodeName,
final ParamPublish paramPublish) {
this.delegate = new BPInstanceConfig<PUBLISHER>(publishers, continueOnError, failOnError, alwaysPublishFromMaster, masterNodeName,
paramPublish);
delegate.setHostConfigurationAccess(this);
this.consolePrefix = consolePrefix;
}
Expand Down
17 changes: 13 additions & 4 deletions src/main/java/jenkins/plugins/publish_over/BapPublisher.java
Expand Up @@ -49,17 +49,20 @@ public class BapPublisher<TRANSFER extends BPTransfer> implements Serializable {
private boolean useWorkspaceInPromotion;
private boolean usePromotionTimestamp;
private Retry retry;
private PublisherLabel label;

public BapPublisher() { }

public BapPublisher(final String configName, final boolean verbose, final ArrayList<TRANSFER> transfers,
final boolean useWorkspaceInPromotion, final boolean usePromotionTimestamp, final Retry retry) {
final boolean useWorkspaceInPromotion, final boolean usePromotionTimestamp, final Retry retry,
final PublisherLabel label) {
this.configName = configName;
this.verbose = verbose;
setTransfers(transfers);
this.useWorkspaceInPromotion = useWorkspaceInPromotion;
this.usePromotionTimestamp = usePromotionTimestamp;
this.retry = retry;
this.label = label;
}

public String getConfigName() {
Expand Down Expand Up @@ -105,6 +108,10 @@ public Retry getRetry() {
return retry;
}

public PublisherLabel getLabel() {
return label;
}

private int sumTransfers(final List<Integer> transferred) {
int total = 0;
for (int tx : transferred) {
Expand Down Expand Up @@ -148,7 +155,7 @@ public void perform(final BPHostConfiguration hostConfig, final BPBuildInfo buil
protected HashCodeBuilder addToHashCode(final HashCodeBuilder builder) {
return builder.append(configName).append(verbose).append(transfers)
.append(useWorkspaceInPromotion).append(usePromotionTimestamp)
.append(retry);
.append(retry).append(label);
}

protected EqualsBuilder addToEquals(final EqualsBuilder builder, final BapPublisher that) {
Expand All @@ -157,7 +164,8 @@ protected EqualsBuilder addToEquals(final EqualsBuilder builder, final BapPublis
.append(transfers, that.transfers)
.append(useWorkspaceInPromotion, that.useWorkspaceInPromotion)
.append(usePromotionTimestamp, that.usePromotionTimestamp)
.append(retry, that.retry);
.append(retry, that.retry)
.append(label, that.label);
}

protected ToStringBuilder addToToString(final ToStringBuilder builder) {
Expand All @@ -166,7 +174,8 @@ protected ToStringBuilder addToToString(final ToStringBuilder builder) {
.append("transfers", transfers)
.append("useWorkspaceInPromotion", useWorkspaceInPromotion)
.append("usePromotionTimestamp", usePromotionTimestamp)
.append("retry", retry);
.append("retry", retry)
.append("label", label);
}

public boolean equals(final Object that) {
Expand Down
126 changes: 126 additions & 0 deletions src/main/java/jenkins/plugins/publish_over/ParamPublish.java
@@ -0,0 +1,126 @@
/*
* The MIT License
*
* Copyright (C) 2010-2011 by Anthony Robinson
*
* 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 jenkins.plugins.publish_over;

import hudson.Util;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

import java.io.Serializable;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;

public class ParamPublish implements Serializable {

private static final long serialVersionUID = 1L;

private final String parameterName;

public ParamPublish(final String parameterName) {
this.parameterName = parameterName;
}

public String getParameterName() {
return parameterName;
}

public PubSelector createSelector(final BPBuildInfo buildInfo) {
if (Util.fixEmptyAndTrim(parameterName) == null) {
return new PubSelector() {
public boolean selected(final BapPublisher publisher) {
return true;
}
};
}
final String regexp = buildInfo.getCurrentBuildEnv().getEnvVars().get(parameterName);
if (regexp == null)
throw new BapPublisherException(Messages.exception_paramPublish_noParameter(parameterName));
try {
final Pattern pattern = Pattern.compile(regexp);
return new Selector(buildInfo, pattern);
} catch (PatternSyntaxException pse) {
throw new BapPublisherException(Messages.exception_paramPublish_badPattern(parameterName, regexp, pse.getMessage()), pse);
}
}

protected HashCodeBuilder addToHashCode(final HashCodeBuilder builder) {
return builder.append(parameterName);
}

protected EqualsBuilder addToEquals(final EqualsBuilder builder, final ParamPublish that) {
return builder.append(parameterName, that.parameterName);
}

protected ToStringBuilder addToToString(final ToStringBuilder builder) {
return builder.append("parameterName", parameterName);
}

public boolean equals(final Object that) {
if (this == that) return true;
if (that == null || getClass() != that.getClass()) return false;

return addToEquals(new EqualsBuilder(), (ParamPublish) that).isEquals();
}

public int hashCode() {
return addToHashCode(new HashCodeBuilder()).toHashCode();
}

public String toString() {
return addToToString(new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE)).toString();
}

public static final class Selector implements PubSelector {

private final BPBuildInfo buildInfo;
private final Pattern pattern;

public Selector(final BPBuildInfo buildInfo, final Pattern pattern) {
this.buildInfo = buildInfo;
this.pattern = pattern;
}

public boolean selected(final BapPublisher publisher) {
String label = null;
if ((publisher.getLabel() == null) || (Util.fixEmptyAndTrim(publisher.getLabel().getLabel()) == null)) {
label = "";
} else {
final String rawLabel = publisher.getLabel().getLabel().trim();
label = Util.replaceMacro(rawLabel, buildInfo.getEnvVars());
}
if (pattern.matcher(label).matches()) {
buildInfo.println(Messages.console_paramPublish_match(label, pattern.pattern(), publisher.getConfigName()));
return true;
} else {
buildInfo.println(Messages.console_paramPublish_skip(label, pattern.pattern(), publisher.getConfigName()));
return false;
}
}

}

}
33 changes: 33 additions & 0 deletions src/main/java/jenkins/plugins/publish_over/PubSelector.java
@@ -0,0 +1,33 @@
/*
* The MIT License
*
* Copyright (C) 2010-2011 by Anthony Robinson
*
* 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 jenkins.plugins.publish_over;

import java.io.Serializable;

public interface PubSelector extends Serializable {

boolean selected(BapPublisher publisher);

}

0 comments on commit e639bd8

Please sign in to comment.