Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Merge pull request #39 from ikedam/feature/JENKINS-11655_NewestUpstream
[JENKINS-11655] Use the newest upstream
  • Loading branch information
ikedam committed Jul 20, 2014
2 parents b33ceb5 + 6f80d6c commit e894206
Show file tree
Hide file tree
Showing 8 changed files with 897 additions and 10 deletions.
139 changes: 129 additions & 10 deletions src/main/java/hudson/plugins/copyartifact/TriggeredBuildSelector.java
Expand Up @@ -29,31 +29,113 @@
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 UpstreamFilterStrategy upstreamFilterStrategy;

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

@Deprecated
public TriggeredBuildSelector(boolean fallback) {
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() {
UpstreamFilterStrategy strategy = getUpstreamFilterStrategy();
if(strategy == null || strategy == UpstreamFilterStrategy.UseGlobalSetting) {
strategy = ((DescriptorImpl)getDescriptor()).getGlobalUpstreamFilterStrategy();
}
if(strategy == null){
return false;
}
switch(strategy) {
case UseOldest:
return false;
case UseNewest:
return true;
default:
// default behavior
return false;
}
}

@Override
public Run<?,?> getBuild(Job<?,?> job, EnvVars env, BuildFilter filter, Run<?,?> parent) {
Run<?,?> result = null;
// Upstream job for matrix will be parent project, not individual configuration:
String jobName = job instanceof MatrixConfiguration
? job.getParent().getFullName() : job.getFullName();
Expand All @@ -66,7 +148,15 @@ public Run<?,?> getBuild(Job<?,?> job, EnvVars env, BuildFilter filter, Run<?,?>
int upstreamBuild = upstream.getUpstreamBuild();
if (jobName.equals(upstreamProject)) {
Run<?,?> run = job.getBuildByNumber(upstreamBuild);
return (run != null && filter.isSelectable(run, env)) ? run : null;
if (run != null && filter.isSelectable(run, env)){
if (
(result == null)
|| (isUseNewest() && result.getNumber() < run.getNumber())
|| (!isUseNewest() && result.getNumber() > run.getNumber())
) {
result = run;
}
}
} else {
// Figure out the parent job and do a recursive call to getBuild
Job<?,?> parentJob = Jenkins.getInstance().getItemByFullName(upstreamProject, Job.class);
Expand All @@ -76,16 +166,23 @@ public Run<?,?> getBuild(Job<?,?> job, EnvVars env, BuildFilter filter, Run<?,?>
filter,
parentJob.getBuildByNumber(upstreamBuild));
if (run != null && filter.isSelectable(run, env)) {
return run;
if (
(result == null)
|| (isUseNewest() && result.getNumber() < run.getNumber())
|| (!isUseNewest() && result.getNumber() > run.getNumber())
) {
result = run;
}
}
}
}
}
if (isFallbackToLastSuccessful()) {

if (result == null && isFallbackToLastSuccessful()) {
//TODO: Write to console, that fallback is used.
return super.getBuild(job, env, filter, parent);
result = super.getBuild(job, env, filter, parent);
}
return null;
return result;
}

@Override
Expand All @@ -94,7 +191,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,4 +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: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>

0 comments on commit e894206

Please sign in to comment.