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

Promotion Badge refactoring #129

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 16 additions & 5 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 PromotionPipeline{

public Promotion(PromotionProcess job) throws IOException {
super(job);
Expand Down Expand Up @@ -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());
Expand Down Expand Up @@ -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<String, String> 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;
}
Expand Down Expand Up @@ -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<ParameterValue> getParameterValues(){
List<ParameterValue> values=new ArrayList<ParameterValue>();
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,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.
*/

Choose a reason for hiding this comment

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

Maybe a @param for listener?

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
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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<ParameterValue> getParameterValues();
}
23 changes: 15 additions & 8 deletions src/main/java/hudson/plugins/promoted_builds/Status.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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.
*/

Choose a reason for hiding this comment

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

Maybe a @param for listener?

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);
}
}

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,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<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 @@ -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;
Expand All @@ -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;
Expand All @@ -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<ParameterDefinition> parameterDefinitions = new ArrayList<ParameterDefinition>();

Expand Down Expand Up @@ -284,15 +271,15 @@ public List<ParameterValue> 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<ParameterValue> params = ((Promotion) build).getParameterValues();
PromotionPipeline promotion = (PromotionPipeline) run;
List<ParameterValue> params = ((PromotionPipeline) run).getParameterValues();
if (params != null) {
for (ParameterValue value : params) {
value.buildEnvVars(build, env);
value.buildEnvironment(promotion.getPromotionRun(),env);
}
}
}
Expand Down