Skip to content
Permalink
Browse files

[JENKINS-22075] - Advanced null checks for missing tools (not finished)

Added checks of non-existent tools to the most of handlers

Signed-off-by: Oleg Nenashev <o.v.nenashev@gmail.com>
  • Loading branch information...
oleg-nenashev committed Mar 7, 2014
1 parent 5754c18 commit 3d1f96edfb3b488738b2409c99395cf15a3dbf18
@@ -44,6 +44,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.annotation.CheckForNull;

import org.kohsuke.stapler.DataBoundConstructor;

@@ -183,6 +184,12 @@ public void setInstallations(CustomTool... installations) {
save();
}

/**
* Gets a {@link CustomTool} by its name.
* @param name A name of the tool to be retrieved.
* @return A {@link CustomTool} or null if it has no found
*/
@CheckForNull
public CustomTool byName(String name) {
for (CustomTool tool : getInstallations()) {
if (tool.getName().equals(name)) {
@@ -45,6 +45,7 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.annotation.CheckForNull;

import net.sf.json.JSONObject;

@@ -73,10 +74,12 @@
public SelectedTool(String name) {
this.name = name;
}

public String getName() {
return name;
}

@CheckForNull
public CustomTool toCustomTool() {
return ((CustomTool.DescriptorImpl)Hudson.getInstance().getDescriptor(CustomTool.class)).byName(name);
}
@@ -106,9 +109,10 @@ public Environment setUp(AbstractBuild build, Launcher launcher,

return new Environment(){
@Override
public void buildEnvVars(Map<String, String> env) {
public void buildEnvVars(Map<String, String> env) {

// TODO: Inject Home dirs as well
for (CustomTool tool : customTools()) {
for (CustomTool tool : getCustomToolsList()) {
if (tool.hasVersions()) {
ToolVersion version = ToolVersion.getEffectiveToolVersion(tool, buildEnv, node);
if (version != null && !env.containsKey(version.getVariableName())) {
@@ -124,7 +128,8 @@ public void buildEnvVars(Map<String, String> env) {
return selectedTools.clone();
}

private List<CustomTool> customTools() {
// TODO: Check for null
private List<CustomTool> getCustomToolsList() {
List<CustomTool> tools = new ArrayList<CustomTool>();
for (SelectedTool selected : selectedTools) {
tools.add(selected.toCustomTool());
@@ -158,7 +163,7 @@ public Launcher decorateLauncher(AbstractBuild build, final Launcher launcher,

// Each tool can export zero or many directories to the PATH
Node node = Computer.currentComputer().getNode();
for (CustomTool tool : customTools()) {
for (CustomTool tool : getCustomToolsList()) {
CustomToolsLogger.logMessage(listener, tool.getName(), "Starting installation");

// Check versioning
@@ -32,7 +32,7 @@
implements Serializable
{
public static final ToolVersionConfig DEFAULT = null;
ExtendedChoiceParameterDefinition versionsListSource;
private final ExtendedChoiceParameterDefinition versionsListSource;

@DataBoundConstructor
public ToolVersionConfig(ExtendedChoiceParameterDefinition versionsListSource) {
@@ -43,8 +43,11 @@ public static String getDefaultVersionVariableName(String toolName) {

public static ExtendedChoiceParameterDefinition getVersionDescr(String toolName) {
CustomTool.DescriptorImpl tools = ToolInstallation.all().get(CustomTool.DescriptorImpl.class);
CustomTool tool = tools.byName(toolName);
return tool.hasVersions() ? tool.getToolVersion().getVersionsListSource() : getDefaultVersionDescr(toolName);
CustomTool tool = tools.byName(toolName);

return tool != null && tool.hasVersions()
? tool.getToolVersion().getVersionsListSource()
: getDefaultVersionDescr(toolName);
}

/**
@@ -16,13 +16,15 @@
package com.synopsys.arc.jenkinsci.plugins.customtools.versions;

import com.cloudbees.jenkins.plugins.customtools.CustomTool;
import com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterDefinition;
import com.cwctravel.hudson.plugins.extended_choice_parameter.ExtendedChoiceParameterValue;
import com.synopsys.arc.jenkinsci.plugins.customtools.Messages;
import hudson.Extension;
import hudson.model.ParameterDefinition;
import hudson.model.ParameterDefinition.ParameterDescriptor;
import hudson.model.StringParameterValue;
import hudson.tools.ToolInstallation;
import javax.annotation.CheckForNull;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.StaplerRequest;
@@ -33,7 +35,8 @@
* @since 0.4
*/
public class ToolVersionParameterDefinition extends ParameterDefinition {
private String toolName;

private final String toolName;

@DataBoundConstructor
public ToolVersionParameterDefinition(String toolName) {
@@ -46,24 +49,50 @@ public String getToolName() {
return toolName;
}

/**
* Gets a {@link CustomTool} linked with this Parameter definition.
* @return A custom tool or null if it has not been found
*/
@CheckForNull
public final CustomTool getTool() {
CustomTool.DescriptorImpl tool = ToolInstallation.all().get(CustomTool.DescriptorImpl.class);
return tool.byName(toolName);
}

/**
* Gets a Tool Version configuration for the parameter definition.
* @return A tool version configuration or null if the tool cannot be found.
*/
@CheckForNull
public final ToolVersionConfig getVersionConfig() {
return getTool().getToolVersion();
CustomTool tool = getTool();
return tool != null ? tool.getToolVersion() : null;
}

private ExtendedChoiceParameterDefinition getVersionsListSource() {
ToolVersionConfig versionConfig = getVersionConfig();
if (versionConfig == null) {
throw new IllegalStateException(
Messages.Versions_ToolVersionParameterDefinition_GetVersionConfigError(toolName));
}

return versionConfig.getVersionsListSource();
}

@Override
public StringParameterValue createValue(StaplerRequest req, JSONObject jo) {
ExtendedChoiceParameterValue paramVal = (ExtendedChoiceParameterValue)getVersionConfig().getVersionsListSource().createValue(req, jo);
public StringParameterValue createValue(StaplerRequest req, JSONObject jo)
throws IllegalStateException
{
ExtendedChoiceParameterValue paramVal = (ExtendedChoiceParameterValue)
getVersionsListSource().createValue(req, jo);
return new StringParameterValue(paramVal.getName(), paramVal.value);
}

@Override
public StringParameterValue createValue(StaplerRequest req) {
ExtendedChoiceParameterValue paramVal = (ExtendedChoiceParameterValue)getVersionConfig().getVersionsListSource().createValue(req);

ExtendedChoiceParameterValue paramVal = (ExtendedChoiceParameterValue)
getVersionsListSource().createValue(req);
return new StringParameterValue(paramVal.getName(), paramVal.value);
}

@@ -1,3 +1,7 @@
LabelSpecifics.DisplayName=Label specifics
Versions.ToolVersionConfig.DisplayName=Tool version
Versions.ToolVersionParameterDefinition.DisplayName=Tool version
Versions.ToolVersionParameterDefinition.DisplayName=Tool version
Versions.ToolVersionParameterDefinition.GetVersionConfigError= \
Cannot retrieve the version configuration for the tool {0}. Probably, it has been deleted
Versions.ToolVersionDescription.NoToolError=ERROR: Cannot find the custom tool. \
Probably, it has been deleted

0 comments on commit 3d1f96e

Please sign in to comment.
You can’t perform that action at this time.