From c3965f04f1b282376f2199906a0b783bc8743098 Mon Sep 17 00:00:00 2001 From: Oleg Nenashev Date: Tue, 4 Jun 2019 23:09:37 +0200 Subject: [PATCH 1/3] [JENKINS-57839] - Initial PromotionBadge compatibility code for Jenkins Pipeline --- .../AddPromotionBadgeStep.java | 34 +++++++++++++++++++ .../plugins/promoted_builds/Promotion.java | 22 ++++++++++-- .../promoted_builds/PromotionBadge.java | 19 +++++++++-- .../plugins/promoted_builds/PromotionRun.java | 27 +++++++++++++++ .../plugins/promoted_builds/Status.java | 20 +++++++++-- .../conditions/GroovyCondition.java | 5 +-- .../conditions/ManualCondition.java | 23 ++++++++----- 7 files changed, 131 insertions(+), 19 deletions(-) create mode 100644 src/main/java/hudson/plugins/promoted_builds/AddPromotionBadgeStep.java create mode 100644 src/main/java/hudson/plugins/promoted_builds/PromotionRun.java diff --git a/src/main/java/hudson/plugins/promoted_builds/AddPromotionBadgeStep.java b/src/main/java/hudson/plugins/promoted_builds/AddPromotionBadgeStep.java new file mode 100644 index 00000000..4daee6f4 --- /dev/null +++ b/src/main/java/hudson/plugins/promoted_builds/AddPromotionBadgeStep.java @@ -0,0 +1,34 @@ +package hudson.plugins.promoted_builds; + +import hudson.Extension; +import hudson.FilePath; +import hudson.Launcher; +import hudson.model.Descriptor; +import hudson.model.Run; +import hudson.model.TaskListener; +import hudson.tasks.BuildStepMonitor; +import jenkins.tasks.SimpleBuildStep; +import org.jenkinsci.Symbol; + +import javax.annotation.Nonnull; +import java.io.IOException; +import java.util.Collection; + +//TODO: Add a Pipeline step so that we can spot-check the logic +// SimpleBuildStep for quick win, but it will require workspace +// Use Pipeline: Step API for advanced thing +public class AddPromotionBadgeStep extends SimpleBuildStep { + + @Override + public void perform(@Nonnull Run run, @Nonnull FilePath workspace, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException { + // Somehow define badge + // Take env vars + // Append it to a the run action + } + + @Extension + @Symbol("addPromotionBadge") + public static class DescriptorImpl extends ..Descriptor { + + } +} diff --git a/src/main/java/hudson/plugins/promoted_builds/Promotion.java b/src/main/java/hudson/plugins/promoted_builds/Promotion.java index 394e1adc..aaf016fe 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 PromotionRun { public Promotion(PromotionProcess job) throws IOException { super(job); @@ -113,6 +113,7 @@ public EnvVars getEnvironment(TaskListener listener) throws IOException, Interru // Augment environment with target build's information String rootUrl = JenkinsHelper.getInstance().getRootUrl(); + //TODO: Refactor to Run AbstractBuild target = getTarget(); if(rootUrl!=null) e.put("PROMOTED_URL",rootUrl+target.getUrl()); @@ -162,7 +163,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; } @@ -239,6 +240,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 95ffb357..0f752265 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,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 6bdeab90..5db5fa80 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); } } } From 6808702e4b6008383a232b9904fc2c9fb05042e6 Mon Sep 17 00:00:00 2001 From: Oleg Nenashev Date: Sun, 21 Jul 2019 23:35:00 +0200 Subject: [PATCH 2/3] Delete AddPromotionBadgeStep.java --- .../AddPromotionBadgeStep.java | 34 ------------------- 1 file changed, 34 deletions(-) delete mode 100644 src/main/java/hudson/plugins/promoted_builds/AddPromotionBadgeStep.java diff --git a/src/main/java/hudson/plugins/promoted_builds/AddPromotionBadgeStep.java b/src/main/java/hudson/plugins/promoted_builds/AddPromotionBadgeStep.java deleted file mode 100644 index 4daee6f4..00000000 --- a/src/main/java/hudson/plugins/promoted_builds/AddPromotionBadgeStep.java +++ /dev/null @@ -1,34 +0,0 @@ -package hudson.plugins.promoted_builds; - -import hudson.Extension; -import hudson.FilePath; -import hudson.Launcher; -import hudson.model.Descriptor; -import hudson.model.Run; -import hudson.model.TaskListener; -import hudson.tasks.BuildStepMonitor; -import jenkins.tasks.SimpleBuildStep; -import org.jenkinsci.Symbol; - -import javax.annotation.Nonnull; -import java.io.IOException; -import java.util.Collection; - -//TODO: Add a Pipeline step so that we can spot-check the logic -// SimpleBuildStep for quick win, but it will require workspace -// Use Pipeline: Step API for advanced thing -public class AddPromotionBadgeStep extends SimpleBuildStep { - - @Override - public void perform(@Nonnull Run run, @Nonnull FilePath workspace, @Nonnull Launcher launcher, @Nonnull TaskListener listener) throws InterruptedException, IOException { - // Somehow define badge - // Take env vars - // Append it to a the run action - } - - @Extension - @Symbol("addPromotionBadge") - public static class DescriptorImpl extends ..Descriptor { - - } -} From 72be6a6de2eaa621831ff26589838f21844a9f17 Mon Sep 17 00:00:00 2001 From: Oleg Nenashev Date: Mon, 29 Jul 2019 16:05:25 +0200 Subject: [PATCH 3/3] Use Jenkinsclass instead of JenkinsHelper --- src/main/java/hudson/plugins/promoted_builds/Promotion.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/hudson/plugins/promoted_builds/Promotion.java b/src/main/java/hudson/plugins/promoted_builds/Promotion.java index c151b3c3..5bd12fb2 100644 --- a/src/main/java/hudson/plugins/promoted_builds/Promotion.java +++ b/src/main/java/hudson/plugins/promoted_builds/Promotion.java @@ -111,7 +111,7 @@ public EnvVars getEnvironment(TaskListener listener) throws IOException, Interru EnvVars e = super.getEnvironment(listener); // Augment environment with target build's information - String rootUrl = JenkinsHelper.get().getRootUrl(); + String rootUrl = Jenkins.get().getRootUrl(); //TODO: Refactor to Run AbstractBuild target = getTarget(); if(rootUrl!=null)