From 237b29dc5a15ae853b939bfe989f67c4131361ef Mon Sep 17 00:00:00 2001 From: dernDren161 Date: Thu, 6 Jun 2019 16:35:42 +0545 Subject: [PATCH] Promotion Badge refactoring --- .../plugins/promoted_builds/Promotion.java | 21 ++++++++--- .../promoted_builds/PromotionBadge.java | 22 ++++++++++-- .../promoted_builds/PromotionPipeline.java | 19 ++++++++++ .../plugins/promoted_builds/Status.java | 23 +++++++----- .../conditions/GroovyCondition.java | 6 ++-- .../conditions/ManualCondition.java | 35 ++++++------------- 6 files changed, 85 insertions(+), 41 deletions(-) create mode 100644 src/main/java/hudson/plugins/promoted_builds/PromotionPipeline.java diff --git a/src/main/java/hudson/plugins/promoted_builds/Promotion.java b/src/main/java/hudson/plugins/promoted_builds/Promotion.java index 394e1adc..755e9513 100644 --- a/src/main/java/hudson/plugins/promoted_builds/Promotion.java +++ b/src/main/java/hudson/plugins/promoted_builds/Promotion.java @@ -60,11 +60,11 @@ import org.kohsuke.stapler.StaplerResponse; /** - * Records a promotion process. + * Records a promotion process for {@link AbstractProject}s * * @author Kohsuke Kawaguchi */ -public class Promotion extends AbstractBuild { +public class Promotion extends AbstractBuild implements PromotionPipeline{ public Promotion(PromotionProcess job) throws IOException { super(job); @@ -113,7 +113,7 @@ public EnvVars getEnvironment(TaskListener listener) throws IOException, Interru // Augment environment with target build's information String rootUrl = JenkinsHelper.getInstance().getRootUrl(); - AbstractBuild target = getTarget(); + Run target = getTarget(); if(rootUrl!=null) e.put("PROMOTED_URL",rootUrl+target.getUrl()); e.put("PROMOTED_JOB_NAME", target.getParent().getName()); @@ -155,14 +155,14 @@ public EnvVars getEnvironment(TaskListener listener) throws IOException, Interru e.put("PROMOTED_USER_NAME", getUserName()); e.put("PROMOTED_USER_ID", getUserId()); EnvVars envScm = new EnvVars(); - target.getProject().getScm().buildEnvVars( target, envScm ); + target.getProject().getScm().buildEnvVars( target, envScm); for ( Entry entry : envScm.entrySet() ) { e.put( "PROMOTED_" + entry.getKey(), entry.getValue() ); } // Allow the promotion status to contribute to build environment - getStatus().buildEnvVars(this, e); + getStatus().buildEnvVars(this,e,listener); return e; } @@ -238,6 +238,17 @@ public String getUserId() { } return User.getUnknown().getId(); } + @Nonnull + @Override + public Run getPromotedRun(){ + return getTarget(); + } + + @Nonnull + @Override + public Run getPromotionRun(){ + return this; + } public List getParameterValues(){ List values=new ArrayList(); diff --git a/src/main/java/hudson/plugins/promoted_builds/PromotionBadge.java b/src/main/java/hudson/plugins/promoted_builds/PromotionBadge.java index 2a527cb6..6a925a76 100644 --- a/src/main/java/hudson/plugins/promoted_builds/PromotionBadge.java +++ b/src/main/java/hudson/plugins/promoted_builds/PromotionBadge.java @@ -2,8 +2,12 @@ import hudson.EnvVars; import hudson.model.AbstractBuild; +import hudson.model.Run; +import hudson.model.TaskListener; import org.kohsuke.stapler.export.ExportedBean; +import javax.annotation.Nonnull; + /** * Captures the information about how/when the promotion criteria is satisfied. * @@ -18,11 +22,25 @@ public abstract class PromotionBadge { /** * Called by {@link Status} to allow promotion badges to contribute environment variables. * - * @param build - * The calling build. Never null. + * @param run + * The calling run. Never null * @param env * Environment variables should be added to this map. */ + public void buildEnvVars(@Nonnull Run run, EnvVars env,TaskListener listener) { + // Default implementation when method is not overridden i.e a classical build + if(run instanceof AbstractBuild){ + buildEnvVars((AbstractBuild)run,env); + } + } + + /** + + @deprecated Use {@link #buildEnvVars(Run, EnvVars, TaskListener)} + + */ + + @Deprecated public void buildEnvVars(AbstractBuild build, EnvVars env) { // by default don't contribute any variables } diff --git a/src/main/java/hudson/plugins/promoted_builds/PromotionPipeline.java b/src/main/java/hudson/plugins/promoted_builds/PromotionPipeline.java new file mode 100644 index 00000000..702bf829 --- /dev/null +++ b/src/main/java/hudson/plugins/promoted_builds/PromotionPipeline.java @@ -0,0 +1,19 @@ +package hudson.plugins.promoted_builds; + +import hudson.model.ParameterValue; +import hudson.model.Run; + +import javax.annotation.Nonnull; +import java.util.List; + +public interface PromotionPipeline { + //Run which we try to promote + @Nonnull + Run getPromotedRun(); + + //Execution which does the promotion + @Nonnull + Run getPromotionRun(); + + List getParameterValues(); +} diff --git a/src/main/java/hudson/plugins/promoted_builds/Status.java b/src/main/java/hudson/plugins/promoted_builds/Status.java index e4493ff6..7eefe178 100644 --- a/src/main/java/hudson/plugins/promoted_builds/Status.java +++ b/src/main/java/hudson/plugins/promoted_builds/Status.java @@ -3,12 +3,8 @@ import hudson.AbortException; import hudson.EnvVars; import hudson.Util; -import hudson.model.AbstractBuild; -import hudson.model.AbstractProject; +import hudson.model.*; import hudson.model.Cause.UserCause; -import hudson.model.ParameterDefinition; -import hudson.model.ParameterValue; -import hudson.model.Result; import hudson.plugins.promoted_builds.conditions.ManualCondition; import hudson.util.Iterators; import net.sf.json.JSONArray; @@ -150,14 +146,25 @@ public AbstractBuild getTarget() { /** * Called by {@link Promotion} to allow status to contribute environment variables. * - * @param build - * The calling build. Never null. + * @param run + * The calling run. Never null. * @param env * Environment variables should be added to this map. */ + public void buildEnvVars(@Nonnull Run run, EnvVars env, TaskListener listener) { + for (PromotionBadge badge : badges) { + badge.buildEnvVars(run, env, listener); + } + } + + /** + * @deprecated use{@link #buildEnvVars(Run, EnvVars, TaskListener)} + * + */ + @Deprecated public void buildEnvVars(AbstractBuild build, EnvVars env) { for (PromotionBadge badge : badges) { - badge.buildEnvVars(build, env); + badge.buildEnvVars(build, env, TaskListener.NULL); } } diff --git a/src/main/java/hudson/plugins/promoted_builds/conditions/GroovyCondition.java b/src/main/java/hudson/plugins/promoted_builds/conditions/GroovyCondition.java index 95ffb357..9b8433b0 100644 --- a/src/main/java/hudson/plugins/promoted_builds/conditions/GroovyCondition.java +++ b/src/main/java/hudson/plugins/promoted_builds/conditions/GroovyCondition.java @@ -5,6 +5,8 @@ import hudson.PluginManager; import hudson.Util; import hudson.model.AbstractBuild; +import hudson.model.Run; +import hudson.model.TaskListener; import hudson.plugins.promoted_builds.PromotionBadge; import hudson.plugins.promoted_builds.PromotionCondition; import hudson.plugins.promoted_builds.PromotionProcess; @@ -130,8 +132,8 @@ public String getDisplayLabel() { } @Override - public void buildEnvVars(final AbstractBuild build, final EnvVars env) { - super.buildEnvVars(build, env); + public void buildEnvVars(final Run run, final EnvVars env, TaskListener listener) { + for (final Map.Entry entry : variables.entrySet()) { env.put(entry.getKey(), entry.getValue()); diff --git a/src/main/java/hudson/plugins/promoted_builds/conditions/ManualCondition.java b/src/main/java/hudson/plugins/promoted_builds/conditions/ManualCondition.java index 6bdeab90..3e4d9e6b 100644 --- a/src/main/java/hudson/plugins/promoted_builds/conditions/ManualCondition.java +++ b/src/main/java/hudson/plugins/promoted_builds/conditions/ManualCondition.java @@ -2,21 +2,8 @@ import hudson.EnvVars; import hudson.Extension; -import hudson.model.AbstractBuild; -import hudson.model.AbstractProject; -import hudson.model.Descriptor; -import hudson.model.Hudson; -import hudson.model.InvisibleAction; -import hudson.model.SimpleParameterDefinition; -import hudson.model.ParameterDefinition; -import hudson.model.ParameterValue; -import hudson.model.User; -import hudson.plugins.promoted_builds.PromotionPermissionHelper; -import hudson.plugins.promoted_builds.PromotionBadge; -import hudson.plugins.promoted_builds.PromotionCondition; -import hudson.plugins.promoted_builds.PromotionConditionDescriptor; -import hudson.plugins.promoted_builds.Promotion; -import hudson.plugins.promoted_builds.PromotionProcess; +import hudson.model.*; +import hudson.plugins.promoted_builds.*; import java.io.IOException; import java.util.ArrayList; @@ -35,10 +22,10 @@ import javax.annotation.Nonnull; import javax.servlet.ServletException; - +import hudson.model.ParameterValue; import net.sf.json.JSONArray; import net.sf.json.JSONObject; - +import hudson.plugins.promoted_builds.PromotionPermissionHelper; import org.acegisecurity.GrantedAuthority; import org.kohsuke.stapler.AncestorInPath; import org.kohsuke.stapler.StaplerRequest; @@ -51,7 +38,7 @@ * @author Kohsuke Kawaguchi * @author Peter Hayes */ -public class ManualCondition extends PromotionCondition { +public class ManualCondition extends PromotionCondition{ private String users; private List parameterDefinitions = new ArrayList(); @@ -284,15 +271,15 @@ public List getParameterValues() { } @Override - public void buildEnvVars(AbstractBuild build, EnvVars env) { - if (!(build instanceof Promotion)) { - throw new IllegalStateException ("Wrong build type. Expected a Promotion, but got "+build.getClass()); + public void buildEnvVars(@Nonnull Run run, EnvVars env, TaskListener listener) { + if (!(run instanceof PromotionPipeline)) { + throw new IllegalStateException ("Wrong build type. Expected a Promotion, but got "+run.getClass()); } - - List params = ((Promotion) build).getParameterValues(); + PromotionPipeline promotion = (PromotionPipeline) run; + List params = ((PromotionPipeline) run).getParameterValues(); if (params != null) { for (ParameterValue value : params) { - value.buildEnvVars(build, env); + value.buildEnvironment(promotion.getPromotionRun(),env); } } }