Skip to content

Commit

Permalink
fixing JENKINS-33921
Browse files Browse the repository at this point in the history
  • Loading branch information
vimil committed Apr 5, 2016
1 parent b02f1d7 commit 7fc25af
Showing 1 changed file with 44 additions and 22 deletions.
Expand Up @@ -16,7 +16,6 @@
import hudson.model.ParameterValue;
import hudson.model.User;
import hudson.model.AbstractProject;
import hudson.model.Hudson;
import hudson.model.ParameterDefinition;
import hudson.util.FormValidation;
import hudson.util.LogTaskListener;
Expand Down Expand Up @@ -192,10 +191,15 @@ public ExtendedChoiceParameterDefinition newInstance(StaplerRequest req, JSONObj
String descriptionGroovyScriptFile = null;
String descriptionBindings = null;
String descriptionGroovyClasspath = null;

String projectName = null;
name = formData.getString("name");
description = formData.getString("description");


AbstractProject<?,?> project = Stapler.getCurrentRequest().findAncestorObject(AbstractProject.class);
if(project !=null) {
projectName = project.getName();
}

JSONObject parameterGroup = formData.getJSONObject("parameterGroup");
if(parameterGroup != null) {
int value = parameterGroup.getInt("value");
Expand Down Expand Up @@ -321,8 +325,9 @@ else if(jsonParameterConfigJavascriptSourceJSON.getInt("value") == 1) {

//@formatter:off
return new ExtendedChoiceParameterDefinition(name,
type,
type,
propertyValue,
projectName,
propertyFile,
groovyScript,
groovyScriptFile,
Expand Down Expand Up @@ -410,10 +415,13 @@ else if(jsonParameterConfigJavascriptSourceJSON.getInt("value") == 1) {

private String javascript;

private String projectName;

//@formatter:off
public ExtendedChoiceParameterDefinition(String name,
String type,
String value,
String projectName,
String propertyFile,
String groovyScript,
String groovyScriptFile,
Expand Down Expand Up @@ -446,8 +454,8 @@ public ExtendedChoiceParameterDefinition(String name,
super(name, description);

this.type = type;

this.value = value;
this.projectName = projectName;
this.propertyFile = propertyFile;
this.propertyKey = propertyKey;
this.groovyScript = groovyScript;
Expand Down Expand Up @@ -641,7 +649,6 @@ private String computeValue(String value, String propertyFilePath, String proper
property.execute();
return project.getProperty(propertyKey);
}

}
catch(Exception e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
Expand Down Expand Up @@ -818,9 +825,12 @@ private void setBindings(GroovyShell shell, String bindings) throws IOException
}

Jenkins jenkins = Jenkins.getInstance();
AbstractProject<?, ?> project = Stapler.getCurrentRequest().findAncestorObject(AbstractProject.class);
shell.setProperty("jenkins", jenkins);
shell.setProperty("currentProject", project);

if(projectName != null) {
AbstractProject<?, ?> project = (AbstractProject<?, ?>) Jenkins.getInstance().getItem(projectName);
shell.setProperty("currentProject", project);
}
}

private String computeEffectiveValue() {
Expand Down Expand Up @@ -1211,6 +1221,16 @@ public void setMultiSelectDelimiter(final String multiSelectDelimiter) {
public void setDefaultPropertyFile(String defaultPropertyFile) {
this.defaultPropertyFile = defaultPropertyFile;
}



public String getProjectName() {
return projectName;
}

public void setProjectName(String projectName) {
this.projectName = projectName;
}

public ParameterDefinitionInfo getParameterDefinitionInfo() {
ParameterDefinitionInfo result = new ParameterDefinitionInfo();
Expand Down Expand Up @@ -1269,21 +1289,23 @@ public Object getJSONEditorOptions() {

private String expandVariables(String input) {
String result = input;
AbstractProject<?, ?> project = Stapler.getCurrentRequest().findAncestorObject(AbstractProject.class);
if(input != null) {
EnvVars envVars;
try {
envVars = project.getEnvironment(null, new LogTaskListener(LOGGER, Level.SEVERE));
User user = User.current();
if(user != null) {
String userId = user.getId();
envVars.put("USER_ID", userId);
}
result = Util.replaceMacro(input, envVars);
} catch (IOException e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
} catch (InterruptedException e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
AbstractProject<?, ?> project = (AbstractProject<?, ?>) (projectName !=null ? Jenkins.getInstance().getItem(projectName) : null);
if(project != null) {
EnvVars envVars;
try {
envVars = project.getEnvironment(null, new LogTaskListener(LOGGER, Level.SEVERE));
User user = User.current();
if(user != null) {
String userId = user.getId();
envVars.put("USER_ID", userId);
}
result = Util.replaceMacro(input, envVars);
} catch (IOException e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
} catch (InterruptedException e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
}
}
}
return result;
Expand Down

0 comments on commit 7fc25af

Please sign in to comment.