diff --git a/src/main/java/hudson/plugins/promoted_builds/Promotion.java b/src/main/java/hudson/plugins/promoted_builds/Promotion.java index bb3a04f5..f5e86003 100644 --- a/src/main/java/hudson/plugins/promoted_builds/Promotion.java +++ b/src/main/java/hudson/plugins/promoted_builds/Promotion.java @@ -59,11 +59,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 PromotionRun { public Promotion(PromotionProcess job) throws IOException { super(job); @@ -142,6 +142,7 @@ public EnvVars getEnvironment(TaskListener listener) throws IOException, Interru // Augment environment with target build's information String rootUrl = Jenkins.get().getRootUrl(); + //TODO: Refactor to Run AbstractBuild target = getTargetBuildOrFail(); if(rootUrl!=null) e.put("PROMOTED_URL",rootUrl+target.getUrl()); @@ -191,7 +192,7 @@ public EnvVars getEnvironment(TaskListener listener) throws IOException, Interru } // Allow the promotion status to contribute to build environment - getStatus().buildEnvVars(this, e); + getStatus().buildEnvVars(this, e, listener); return e; } @@ -268,6 +269,21 @@ public String getUserId() { return User.getUnknown().getId(); } + @Nonnull + @Override + public Run getPromotedRun() { + return getTarget(); + } + + + @Nonnull + @Override + public Run getPromotionRun() { + return this; + } + + //TODO: move to a default method + @Override public List getParameterValues(){ List values=new ArrayList(); ParametersAction parametersAction=getParametersActions(this); diff --git a/src/main/java/hudson/plugins/promoted_builds/PromotionBadge.java b/src/main/java/hudson/plugins/promoted_builds/PromotionBadge.java index 2a527cb6..79fee47c 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,22 @@ 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. * @param env * Environment variables should be added to this map. */ + public void buildEnvVars(@Nonnull Run run, EnvVars env, TaskListener listener) { + // Default implementation when the method is not overridden + 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/PromotionRun.java b/src/main/java/hudson/plugins/promoted_builds/PromotionRun.java new file mode 100644 index 00000000..12273455 --- /dev/null +++ b/src/main/java/hudson/plugins/promoted_builds/PromotionRun.java @@ -0,0 +1,27 @@ +package hudson.plugins.promoted_builds; + +// TODO: implementation for Pipeline +// TODO: add generics? + +import hudson.model.ParameterValue; +import hudson.model.Run; + +import javax.annotation.Nonnull; +import java.util.List; + +/** + * @see Promotion + */ +public interface PromotionRun { // always a run? + + // Run which we try to promote + @Nonnull + Run getPromotedRun(); + + // Execution which does the promotion + @Nonnull + Run getPromotionRun(); + + //TODO: Move implementation to a default method? + 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..f7ae257b 100644 --- a/src/main/java/hudson/plugins/promoted_builds/Status.java +++ b/src/main/java/hudson/plugins/promoted_builds/Status.java @@ -9,6 +9,8 @@ import hudson.model.ParameterDefinition; import hudson.model.ParameterValue; import hudson.model.Result; +import hudson.model.Run; +import hudson.model.TaskListener; import hudson.plugins.promoted_builds.conditions.ManualCondition; import hudson.util.Iterators; import net.sf.json.JSONArray; @@ -147,17 +149,29 @@ public AbstractBuild getTarget() { return _parent != null ? _parent.owner : null; } + + /** * Called by {@link Promotion} to allow status to contribute environment variables. * - * @param build - * The calling build. Never null. + * @param run + * The calling run * @param env * Environment variables should be added to this map. */ + public void buildEnvVars(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 edfb4ae1..c8ea9fb5 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; @@ -120,8 +122,7 @@ public String getDisplayLabel() { } @Override - public void buildEnvVars(final AbstractBuild build, final EnvVars env) { - super.buildEnvVars(build, env); + public void buildEnvVars(final Run build, 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 fd2c0929..40a069c6 100644 --- a/src/main/java/hudson/plugins/promoted_builds/conditions/ManualCondition.java +++ b/src/main/java/hudson/plugins/promoted_builds/conditions/ManualCondition.java @@ -7,16 +7,18 @@ 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.Run; +import hudson.model.TaskListener; import hudson.model.User; -import hudson.plugins.promoted_builds.PromotionPermissionHelper; +import hudson.plugins.promoted_builds.Promotion; 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.PromotionPermissionHelper; import hudson.plugins.promoted_builds.PromotionProcess; +import hudson.plugins.promoted_builds.PromotionRun; import java.io.IOException; import java.util.ArrayList; @@ -283,16 +285,19 @@ public List getParameterValues() { return values != null ? values : Collections.emptyList(); } + //TODO, TBD: Refactor API to PromotionRun ? @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(Run run, EnvVars env, TaskListener listener) { + // TODO: Refactor to support Pipeline Promotion types + if (!(run instanceof PromotionRun)) { + throw new IllegalStateException ("Wrong build type. Expected a PromotionRun, but got "+run.getClass()); } - - List params = ((Promotion) build).getParameterValues(); + + PromotionRun promotion = (PromotionRun)run; + List params = ((PromotionRun) run).getParameterValues(); if (params != null) { for (ParameterValue value : params) { - value.buildEnvVars(build, env); + value.buildEnvironment(promotion.getPromotionRun(), env); } } }