Skip to content

Commit

Permalink
[JENKINS-11655] Added global configuration to specify the default beh…
Browse files Browse the repository at this point in the history
…avior.
  • Loading branch information
ikedam committed Jul 12, 2014
1 parent 1addda5 commit a99e78e
Show file tree
Hide file tree
Showing 7 changed files with 157 additions and 17 deletions.
104 changes: 95 additions & 9 deletions src/main/java/hudson/plugins/copyartifact/TriggeredBuildSelector.java
Expand Up @@ -29,41 +29,105 @@
import hudson.matrix.MatrixConfiguration;
import hudson.matrix.MatrixRun;
import hudson.model.Result;
import hudson.model.Descriptor;
import hudson.model.Cause;
import hudson.model.Cause.UpstreamCause;
import hudson.model.Job;
import hudson.model.Run;
import net.sf.json.JSONObject;

import org.jvnet.localizer.Localizable;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;

/**
* Copy artifacts from the build that triggered this build.
* @author Alan Harder
*/
public class TriggeredBuildSelector extends BuildSelector {
/**
* Which build should be used if triggered by multiple upstream builds.
*
* Specified in buildstep configurations and the global configuration.
*/
public enum UpstreamFilterStrategy {
/**
* Use global configuration.
*
* The default value for buildstep configurations.
* Should not be specified in the global configuration.
*
*/
UseGlobalSetting(false, Messages._TriggeredBuildSelector_UpstreamFilterStrategy_UseGlobalSetting()),
/**
* Use the oldest build.
*
* The default value for the global configuration.
*/
UseOldest(true, Messages._TriggeredBuildSelector_UpstreamFilterStrategy_UseOldest()),
/**
* Use the newest build.
*/
UseNewest(true, Messages._TriggeredBuildSelector_UpstreamFilterStrategy_UseNewest()),
;

private final boolean forGlobalSetting;
private final Localizable displayName;

UpstreamFilterStrategy(boolean forGlobalSetting, Localizable displayName) {
this.forGlobalSetting = forGlobalSetting;
this.displayName = displayName;
}

public String getDisplayName() {
return displayName.toString();
}

public boolean isForGlobalSetting() {
return forGlobalSetting;
}
};
private Boolean fallbackToLastSuccessful;
private final boolean useNewest;
private final UpstreamFilterStrategy upstreamFilterStrategy;

@DataBoundConstructor
public TriggeredBuildSelector(boolean fallback, boolean useNewest) {
public TriggeredBuildSelector(boolean fallback, UpstreamFilterStrategy upstreamFilterStrategy) {
this.fallbackToLastSuccessful = fallback ? Boolean.TRUE : null;
this.useNewest = useNewest;
this.upstreamFilterStrategy = upstreamFilterStrategy;
}

@Deprecated
public TriggeredBuildSelector(boolean fallback) {
this(fallback, false);
this(fallback, UpstreamFilterStrategy.UseGlobalSetting);
}

public boolean isFallbackToLastSuccessful() {
return fallbackToLastSuccessful != null && fallbackToLastSuccessful.booleanValue();
}

/**
* @return Which build should be used if triggered by multiple upstream builds.
*/
public UpstreamFilterStrategy getUpstreamFilterStrategy() {
return upstreamFilterStrategy;
}

/**
* @return whether to use the newest upstream or not (use the oldest) when there are multiple upstreams.
*/
public boolean isUseNewest() {
return useNewest;
UpstreamFilterStrategy strategy = getUpstreamFilterStrategy();
if(strategy == null || strategy == UpstreamFilterStrategy.UseGlobalSetting) {
strategy = ((DescriptorImpl)getDescriptor()).getGlobalUpstreamFilterStrategy();
}
switch(strategy) {
case UseOldest:
return false;
case UseNewest:
return true;
default:
// default behavior
return false;
}
}

@Override
Expand Down Expand Up @@ -124,7 +188,29 @@ protected boolean isSelectable(Run<?,?> run, EnvVars env) {
}

@Extension(ordinal=25)
public static final Descriptor<BuildSelector> DESCRIPTOR =
new SimpleBuildSelectorDescriptor(
TriggeredBuildSelector.class, Messages._TriggeredBuildSelector_DisplayName());
public static class DescriptorImpl extends SimpleBuildSelectorDescriptor {
private UpstreamFilterStrategy globalUpstreamFilterStrategy;

public DescriptorImpl() {
super(TriggeredBuildSelector.class, Messages._TriggeredBuildSelector_DisplayName());
globalUpstreamFilterStrategy = UpstreamFilterStrategy.UseOldest;
load();
}

public void setGlobalUpstreamFilterStrategy(UpstreamFilterStrategy globalUpstreamFilterStrategy) {
this.globalUpstreamFilterStrategy = globalUpstreamFilterStrategy;
}

public UpstreamFilterStrategy getGlobalUpstreamFilterStrategy() {
return globalUpstreamFilterStrategy;
}

@Override
public boolean configure(StaplerRequest req, JSONObject json)
throws hudson.model.Descriptor.FormException {
setGlobalUpstreamFilterStrategy(UpstreamFilterStrategy.valueOf(json.getString("globalUpstreamFilterStrategy")));
save();
return super.configure(req, json);
}
}
}
Expand Up @@ -16,6 +16,9 @@ StatusBuildSelector.DisplayName=Latest successful build
SavedBuildSelector.DisplayName=Latest saved build (marked "keep forever")
SpecificBuildSelector.DisplayName=Specific build
TriggeredBuildSelector.DisplayName=Upstream build that triggered this job
TriggeredBuildSelector.UpstreamFilterStrategy.UseGlobalSetting=Use global setting
TriggeredBuildSelector.UpstreamFilterStrategy.UseOldest=Use the oldest build
TriggeredBuildSelector.UpstreamFilterStrategy.UseNewest=Use the newest build
ParameterizedBuildSelector.DisplayName=Specified by a build parameter
BuildSelectorParameter.DisplayName=Build selector for Copy Artifact
WorkspaceSelector.DisplayName=Copy from WORKSPACE of latest completed build
Expand Down
Expand Up @@ -27,7 +27,9 @@ THE SOFTWARE.
<f:checkbox name="fallback" checked="${selector.isFallbackToLastSuccessful()}"/>
<label class="attach-previous">${%Use "Last successful build" as fallback}</label>
</f:entry>
<f:entry field="useNewest">
<f:checkbox title="${%Use the newest build if there are multiple upstream builds}" />
<f:advanced>
<f:entry field="upstreamFilterStrategy" title="${%Which for multiple upstream}">
<f:enum field="upstreamFilterStrategy">${it.displayName}</f:enum>
</f:entry>
</f:advanced>
</j:jelly>
@@ -0,0 +1,42 @@
<!--
The MIT License
Copyright (c) 2014 IKEDA Yasuyuki
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.
-->
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define" xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form">
<f:section title="Copyartifact: ${descriptor.displayName}">
<f:entry field="globalUpstreamFilterStrategy" title="${%Which for multiple upstream}">
<j:scope>
<j:set var="field" value="globalUpstreamFilterStrategy" />
<select class="setting-input" name="${field}">
<j:forEach var="it" items="${descriptor.getPropertyType(instance,field).enumConstants}">
<j:if test="${it.forGlobalSetting}">
<f:option value="${it.name()}" selected="${it==instance[field]}">
${it.getDisplayName()}
</f:option>
</j:if>
</j:forEach>
</select>
</j:scope>
</f:entry>
</f:section>
</j:jelly>
@@ -0,0 +1,6 @@
<div>
"Upstream build that triggered this job", a build selector in Copyartifact can specify
which upstream build to use when a build is triggered by multiple upstream builds.
This field specifies the default value for that configuration,
which is used when "Use global setting" is specified in project configurations.
</div>
@@ -0,0 +1,7 @@
<div>
Jenkins launches only one build when multiple upstreams triggered the same project at the same time.
This field specifies from which upstream build to copy artifacts in those cases.
"Use the oldest" copies artifacts from the upstream build with the smallest build number (that is, oldest).
"Use the newest" copies artifacts from the upstream build with the largest build number (that is, newest).
The default value is "Use global setting", which behaves as configured in "Manage Jenkins" > "Configure System".
</div>

This file was deleted.

0 comments on commit a99e78e

Please sign in to comment.