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-57839] - Initial PromotionBadge compatibility code for Jenkins Pipeline #128

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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,34 @@
package hudson.plugins.promoted_builds;
Copy link
Member Author

Choose a reason for hiding this comment

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

If it helps, I can remove this non-compilable stub and get the rest to the mergeable state. Or one can just build a new pull request on the top of it


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 {

}
}
22 changes: 19 additions & 3 deletions src/main/java/hudson/plugins/promoted_builds/Promotion.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<PromotionProcess,Promotion> {
public class Promotion extends AbstractBuild<PromotionProcess,Promotion> implements PromotionRun {

public Promotion(PromotionProcess job) throws IOException {
super(job);
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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<ParameterValue> getParameterValues(){
List<ParameterValue> values=new ArrayList<ParameterValue>();
ParametersAction parametersAction=getParametersActions(this);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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
}
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/hudson/plugins/promoted_builds/PromotionRun.java
Original file line number Diff line number Diff line change
@@ -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<ParameterValue> getParameterValues();
}
20 changes: 17 additions & 3 deletions src/main/java/hudson/plugins/promoted_builds/Status.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String, String> entry :
variables.entrySet()) {
env.put(entry.getKey(), entry.getValue());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -283,16 +285,19 @@ public List<ParameterValue> getParameterValues() {
return values != null ? values : Collections.<ParameterValue>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<ParameterValue> params = ((Promotion) build).getParameterValues();

PromotionRun promotion = (PromotionRun)run;
List<ParameterValue> params = ((PromotionRun) run).getParameterValues();
if (params != null) {
for (ParameterValue value : params) {
value.buildEnvVars(build, env);
value.buildEnvironment(promotion.getPromotionRun(), env);
}
}
}
Expand Down