Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIXED JENKINS-16213] Add global and local settings to promote newest or oldest build #41

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.jvnet.localizer.Localizable;
import org.kohsuke.stapler.export.Exported;

/**
Expand All @@ -46,23 +47,99 @@
*/
public class DownstreamPassCondition extends PromotionCondition {
/**
* 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._DownstreamPassCondition_UpstreamFilterStrategy_UseGlobalSetting()),
/**
* Use the oldest build.
*
* The default value for the global configuration.
*/
UseOldest(true, Messages._DownstreamPassCondition_UpstreamFilterStrategy_UseOldest()),
/**
* Use the newest build.
*/
UseNewest(true, Messages._DownstreamPassCondition_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;
}
};/**
* List of downstream jobs that are used as the promotion criteria.
*
* Every job has to have at least one successful build for us to promote a build.
*/
private final String jobs;

private final boolean evenIfUnstable;

private final UpstreamFilterStrategy upstreamFilterStrategy;

public DownstreamPassCondition(String jobs) {
this(jobs, false);
this(jobs, false, UpstreamFilterStrategy.UseGlobalSetting);
}

public DownstreamPassCondition(String jobs, boolean evenIfUnstable) {
this(jobs, evenIfUnstable, UpstreamFilterStrategy.UseGlobalSetting);
}

public DownstreamPassCondition(String jobs, boolean evenIfUnstable, UpstreamFilterStrategy upstreamFilterStrategy ) {
this.jobs = jobs;
this.evenIfUnstable = evenIfUnstable;
this.upstreamFilterStrategy = upstreamFilterStrategy;
}


/**
* @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;
}
}

public String getJobs() {
return jobs;
}
Expand Down Expand Up @@ -156,6 +233,30 @@ void add(AbstractBuild<?,?> b) {

@Extension
public static final class DescriptorImpl extends PromotionConditionDescriptor {
private UpstreamFilterStrategy globalUpstreamFilterStrategy;

public DescriptorImpl() {
super(DownstreamPassCondition.class);
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);
}

public boolean isApplicable(AbstractProject<?,?> item) {
return true;
}
Expand All @@ -166,7 +267,7 @@ public String getDisplayName() {

public PromotionCondition newInstance(StaplerRequest req, JSONObject formData) throws FormException {
return new DownstreamPassCondition(
formData.getString("jobs"), formData.getBoolean("evenIfUnstable"));
formData.getString("jobs"), formData.getBoolean("evenIfUnstable"),UpstreamFilterStrategy.valueOf(formData.getString("upstreamFilterStrategy")));
}

public AutoCompletionCandidates doAutoCompleteJobs(@QueryParameter String value, @AncestorInPath AbstractProject project) {
Expand Down Expand Up @@ -224,11 +325,21 @@ public void onCompleted(AbstractBuild<?,?> build, TaskListener listener) {
if(jp!=null) {
for (PromotionProcess p : jp.getItems()) {
boolean considerPromotion = false;
boolean breakToFirstMatch = true; // take oldest build

for (PromotionCondition cond : p.conditions) {
if (cond instanceof DownstreamPassCondition) {
DownstreamPassCondition dpcond = (DownstreamPassCondition) cond;
if(dpcond.contains(j.getParent(), build.getParent())) {
considerPromotion = true;
UpstreamFilterStrategy upstreamFilterStrategy = dpcond.getUpstreamFilterStrategy();
if (upstreamFilterStrategy == UpstreamFilterStrategy.UseGlobalSetting) {
DownstreamPassCondition.DescriptorImpl d = (DownstreamPassCondition.DescriptorImpl)dpcond.getDescriptor();
if (d.getGlobalUpstreamFilterStrategy() == UpstreamFilterStrategy.UseNewest)
breakToFirstMatch = false;
} else if (upstreamFilterStrategy == UpstreamFilterStrategy.UseNewest)
breakToFirstMatch = false;

break;
}
}
Expand All @@ -248,7 +359,8 @@ public void onCompleted(AbstractBuild<?,?> build, TaskListener listener) {
u.addAction(pdb=new PseudoDownstreamBuilds());
pdb.add(build);
u.save();
break;
if ( breakToFirstMatch )
break;
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,10 @@ public boolean canApprove(PromotionProcess promotionProcess, AbstractBuild<?,?>
if (!getUsersAsSet().isEmpty() && !isInUsersList() && !isInGroupList()) {
return false;
}

//Check if user have promotion access
if (!promotionProcess.hasPermission(Promotion.PROMOTE)) {
return false;
}
List<ManualApproval> approvals = build.getActions(ManualApproval.class);

// For now, only allow approvals if this wasn't already approved
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,7 @@
<f:checkbox name="promotion.downstream.evenIfUnstable" checked="${instance.evenIfUnstable}"/>
<label class="attach-previous">${%Trigger even if the build is unstable}</label>
</f:entry>
<f:entry field="upstreamFilterStrategy" title="${%Which for multiple upstream}">
<f:enum field="upstreamFilterStrategy">${it.displayName}</f:enum>
</f:entry>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<!--
The MIT License

Copyright (c) 2014 Kari Sivonen

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="Promote build: ${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>
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,7 @@ ParameterizedSelfPromotionCondition.DisplayName=Promote immediately once the bui
DownstreamPassCondition.DisplayName=When the following downstream projects build successfully
ManualCondition.DisplayName=Only when manually approved
UpstreamPromotionCondition.DisplayName=When the following upstream promotions are promoted
DownstreamPassCondition.DisplayName=Upstream build that triggered this job
DownstreamPassCondition.UpstreamFilterStrategy.UseGlobalSetting=Use global setting
DownstreamPassCondition.UpstreamFilterStrategy.UseOldest=Use the oldest build
DownstreamPassCondition.UpstreamFilterStrategy.UseNewest=Use the newest build