-
Notifications
You must be signed in to change notification settings - Fork 168
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
dernDren161
wants to merge
1
commit into
jenkinsci:master
Choose a base branch
from
dernDren161:self-promotion
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
124 changes: 124 additions & 0 deletions
124
...n/plugins/promoted_builds/pipeline/PipelineConditions/PipelineSelfPromotionCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} | ||
} |
116 changes: 116 additions & 0 deletions
116
src/main/java/hudson/plugins/promoted_builds/pipeline/PipelinePromotedBuildAction.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; | ||
} | ||
|
||
|
||
|
||
|
||
|
||
} |
52 changes: 52 additions & 0 deletions
52
src/main/java/hudson/plugins/promoted_builds/pipeline/PipelinePromotionCondition.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> { | ||
|
||
//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; | ||
} | ||
|
||
} |
22 changes: 22 additions & 0 deletions
22
...in/java/hudson/plugins/promoted_builds/pipeline/PipelinePromotionConditionDescriptor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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