Skip to content

Commit

Permalink
[FIXED JENKINS-34988] Remove duplicate code from DynamicReferencePara…
Browse files Browse the repository at this point in the history
…meter, use code from Abstract parent class (avoids using Descriptor, scans Jenkins objects instead)
  • Loading branch information
kinow committed Oct 23, 2016
1 parent d796e81 commit 1ade075
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 34 deletions.
Expand Up @@ -39,6 +39,7 @@
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;

import hudson.model.AbstractBuild;
import hudson.model.AbstractItem;
import hudson.model.ParameterValue;
import hudson.model.Project;
Expand All @@ -65,6 +66,10 @@ public abstract class AbstractScriptableParameter extends AbstractUnoChoiceParam
* Constant used to add the project in the environment variables map.
*/
protected static final String JENKINS_PROJECT_VARIABLE_NAME = "jenkinsProject";
/**
* Constant used to add the build in the environment variables map.
*/
protected static final String JENKINS_BUILD_VARIABLE_NAME = "jenkinsBuild";
/**
* Number of visible items on the screen.
*/
Expand Down Expand Up @@ -158,8 +163,13 @@ private Map<Object, Object> getHelperParameters() {
// otherwise, in case we don't have the item name, we iterate looking for a job that uses this UUID
project = Utils.findProjectByParameterUUID(this.getRandomName());
}
if (project != null)
if (project != null) {
helperParameters.put(JENKINS_PROJECT_VARIABLE_NAME, project);
AbstractBuild<?, ?> build = project.getLastBuild();
if (build != null && build.getHasArtifacts()) {
helperParameters.put(JENKINS_BUILD_VARIABLE_NAME, build);
}
}
return helperParameters;
}

Expand Down
40 changes: 7 additions & 33 deletions src/main/java/org/biouno/unochoice/DynamicReferenceParameter.java
Expand Up @@ -24,16 +24,7 @@

package org.biouno.unochoice;

import hudson.Extension;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.ParameterDefinition;
import hudson.util.FormValidation;

import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.BooleanUtils;
import org.apache.commons.lang.StringUtils;
Expand All @@ -44,6 +35,12 @@
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.bind.JavaScriptMethod;

import hudson.Extension;
import hudson.model.AbstractProject;
import hudson.model.ParameterDefinition;
import hudson.util.FormValidation;
import net.sf.json.JSONObject;

/**
* <p>Provides a <b>dynamic reference parameter</b> for users. This is a not so elegant
* solution, since we are using a ParameterDefinition extension point, but it
Expand All @@ -64,10 +61,7 @@ public class DynamicReferenceParameter extends AbstractCascadableParameter {
/*
* Serial UID.
*/
private static final long serialVersionUID = 3583160434198488019L;

private static final String JENKINS_PROJECT_VARIABLE_NAME = "jenkinsProject";
private static final String JENKINS_BUILD_VARIABLE_NAME = "jenkinsBuild";
private static final long serialVersionUID = 8261526672604361397L;

/**
* Choice type.
Expand Down Expand Up @@ -128,26 +122,6 @@ public Boolean getOmitValueField() {
return omitValueField;
}

/**
* {@inheritDoc}
*
* This parameter also includes the Jenkins project and build objects in the Groovy variables map. It
* means that you can use these two in your code for rendering the parameter.
*/
@Override
public Map<Object, Object> getParameters() {
Map<Object, Object> parameters = super.getParameters();
final AbstractProject<?, ?> project = ((DescriptorImpl) getDescriptor()).getProject();
if (project != null) {
parameters.put(JENKINS_PROJECT_VARIABLE_NAME, project);
AbstractBuild<?, ?> build = project.getLastBuild();
if (build != null && build.getHasArtifacts()) {
parameters.put(JENKINS_BUILD_VARIABLE_NAME, build);
}
}
return parameters;
}

@JavaScriptMethod
public String getChoicesAsStringForUI() {
String result = getChoicesAsString(getParameters());
Expand Down

0 comments on commit 1ade075

Please sign in to comment.