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

[JENKINS-57816] Self-Promotion Condition feature with Pipeline Compatibility #136

Open
wants to merge 1 commit 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
@@ -0,0 +1,124 @@
package hudson.plugins.promoted_builds.pipeline.PipelineConditions;

import hudson.Extension;
import hudson.model.*;
import hudson.model.listeners.RunListener;
import hudson.plugins.promoted_builds.PromotionBadge;
import hudson.plugins.promoted_builds.conditions.SelfPromotionBadge;
import hudson.plugins.promoted_builds.pipeline.PipelinePromotionCondition;
import hudson.plugins.promoted_builds.pipeline.PipelinePromotionConditionDescriptor;
import hudson.plugins.promoted_builds.pipeline.PipelinePromotionProcess;

import javax.annotation.Nonnull;
import java.util.Collections;
import java.util.List;

public class PipelineSelfPromotionCondition extends PipelinePromotionCondition {

private final List<ParameterValue> parameters;
private final boolean evenIfUnstable;

public PipelineSelfPromotionCondition(@Nonnull List<ParameterValue> parameters, boolean evenIfUnstable){
this.parameters = parameters;
this.evenIfUnstable = evenIfUnstable;
}


private static String localJobName;



@Override
public PromotionBadge isMet(PipelinePromotionProcess promotionProcess, Run<?,?> run){

localJobName = run.getParent().getFullName();


if(!run.isBuilding()){
Result r = run.getResult();
if((r == Result.SUCCESS) || (evenIfUnstable && r == Result.UNSTABLE){
return new SelfPromotionBadge();
}

}
return null;
}

/** Singleton Design: private static List<ParameterValue> parameters; //[We pass the parameters from the DSL this way.]
* public static List<ParameterValue> getParameters(){
* return parameters;
* }
* Note: parameters contain (Job name, Build Number)
*/
@Override
public List<ParameterValue> getParameters(){

return Collections.unmodifiableList(parameters);
}




//Confusion as to how do i call ".considerPromotion2" without the "conditions" from the UI and by instead using the ParameterValue
@Extension
public static final class RunListenerImpl extends RunListener<Run<?,?>> {
public RunListenerImpl() {
super((Class)Run.class);
}


// My Expected approach but don't know how to call ".considerPromotion2" from here on!!
/**
@Override
public void onCompleted(Run<?,?> run, TaskListener listener) {

for(ParameterValue p : parameters){
if (p.equals(localJobName)) {
try {
p.considerPromotion2(build);
break; // move on to the next process
} catch (IOException e) {
e.printStackTrace(listener.error("Failed to promote a build"));
}
}

*/

//Original Implementation
/**
@Override
public void onCompleted(AbstractBuild<?,?> build, TaskListener listener) {
JobPropertyImpl jp = build.getProject().getProperty(JobPropertyImpl.class);
if(jp!=null) {
for (PromotionProcess p : jp.getItems()) {
for (PromotionCondition cond : p.conditions) {
if (cond instanceof SelfPromotionCondition) {
try {
p.considerPromotion2(build);
break; // move on to the next process
} catch (IOException e) {
e.printStackTrace(listener.error("Failed to promote a build"));
}
}
}
}
}
}
*/

}

@Extension
public static final class DescriptorImpl extends PipelinePromotionConditionDescriptor {
public boolean isApplicable(Job<?,?> item) {
return true;
}
//Cannot resolve the return method

public String getDisplayName() {

return null; //Messages.SelfPromotionCondition_DisplayName();
}

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
package hudson.plugins.promoted_builds.pipeline;

import edu.umd.cs.findbugs.annotations.CheckForNull;
import hudson.model.BuildBadgeAction;
import hudson.model.Run;
import hudson.plugins.promoted_builds.Promotion;
import hudson.plugins.promoted_builds.PromotionProcess;
import hudson.plugins.promoted_builds.Status;
import hudson.util.CopyOnWriteList;
import org.kohsuke.stapler.export.Exported;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

public final class PipelinePromotedBuildAction implements BuildBadgeAction {

public final Run<?,?> owner;

private final CopyOnWriteList<Status> statuses = new CopyOnWriteList<Status>();

public PipelinePromotedBuildAction(Run<?,?>owner){
assert owner != null;
this.owner = owner;
}

public PipelinePromotedBuildAction(Run<?,?> owner, Status firstStatus){
this(owner);
statuses.add(firstStatus);
}

public Run<?,?> getOwner(){

return owner;
}
// What do i do with this: getProject() ?? cannot convert it to getParent??
public Run<?,?> getProject(){

return owner.getProject();
}

// Gets resolved after Status is refactored
public boolean contains(PipelinePromotionProcess process){
for(Status s : statuses)
if(s.isFor(process))
return true;
return false;
}
public boolean contains(String name){
for(Status s : statuses)
if(s.name.equals(name));
return true;
return false;
}

public synchronized boolean add(Status status) throws IOException {
for(Status s: statuses)
if(s.name.equals(status.name))
return false;

this.statuses.add(status);
status.parent = this;
owner.save();
return true;
}

@Exported
public List<Status> getPromotions(){
return statuses.getView();
}

public List<Promotion> getPromotionBuilds(PromotionProcess promotionProcess) {
List<Promotion> filtered = new ArrayList<Promotion>();

for(Status s: getPromotions() ){
if( s.isFor(promotionProcess)){
filtered.addAll( s.getPromotionBuilds() );
}
}
return filtered;
}


/**
* Finds the {@link Status} that has matching {@link Status#name} value.
* Or {@code null} if not found.
*/
@CheckForNull
public Status getPromotion(String name) {
for (Status s : statuses)
if(s.name.equals(name))
return s;
return null;
}

public boolean hasPromotion() {
return !statuses.isEmpty();
}

public String getIconFileName() {
return "star.png";
}

public String getDisplayName() {
return "Promotion Status";
}

public String getUrlName() {
return "promotion";
}





}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package hudson.plugins.promoted_builds.pipeline;

import hudson.DescriptorExtensionList;
import hudson.ExtensionPoint;
import hudson.model.Describable;
import hudson.model.Job;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.plugins.promoted_builds.PromotionBadge;
import hudson.plugins.promoted_builds.util.JenkinsHelper;

import javax.annotation.CheckForNull;
import java.util.ArrayList;
import java.util.List;

public abstract class PipelinePromotionCondition implements ExtensionPoint, Describable<PipelinePromotionCondition> {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no need in separate extension point. The existing ones should be refactored as we agreed before


//for those promotions that don't satisfy the desired Promotion Conditions
@CheckForNull
public PromotionBadge isMet(Run<?,?> run, TaskListener listener){
return null;
}

//The method is used by the Conditions to label those who satisfy the presented Promotion Conditions.
public PromotionBadge isMet(PipelinePromotionProcess promotionProcess, Run<?,?> run, TaskListener listener){
return isMet(run,listener);
}

//Loads the descriptor with the URL
public PipelinePromotionConditionDescriptor getDescriptor() {
return (PipelinePromotionConditionDescriptor) JenkinsHelper.getInstance().getDescriptor(getClass());
}

//Holds a set of Descriptors
public static DescriptorExtensionList<PipelinePromotionCondition, PipelinePromotionConditionDescriptor> all() {
return JenkinsHelper.getInstance().<PipelinePromotionCondition, PipelinePromotionConditionDescriptor>getDescriptorList(PipelinePromotionCondition.class);
}

/**
* Returns a subset of {@link PipelinePromotionConditionDescriptor}s that applies to the given project.
*/
//Required Promotion Conditions to be satisfied for Promotion is being defined here.
public static List<PipelinePromotionConditionDescriptor> getApplicableTriggers(Job<?,?> p) {
List<PipelinePromotionConditionDescriptor> r = new ArrayList<PipelinePromotionConditionDescriptor>();
for (PipelinePromotionConditionDescriptor t : all()) {
if(t.isApplicable(p))
r.add(t);
}
return r;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package hudson.plugins.promoted_builds.pipeline;

import hudson.model.Descriptor;
import hudson.model.Job;

public abstract class PipelinePromotionConditionDescriptor extends Descriptor<PipelinePromotionCondition> {

protected PipelinePromotionConditionDescriptor(Class<? extends PipelinePromotionCondition> clazz) {
super(clazz);
}

protected PipelinePromotionConditionDescriptor() {
super();
}

/**
* Returns true if this condition is applicable to the given project.
*
* @return true to allow user to configure this promotion condition for the given project.
*/
public abstract boolean isApplicable(Job<?, ?> item);
}
Loading