Skip to content

Commit

Permalink
[FIX JENKINS-40199] Tool listing API (#14)
Browse files Browse the repository at this point in the history
* JENKINS-40199 - API to list available tools
  • Loading branch information
kzantow committed Jan 5, 2017
1 parent a22e1c1 commit 8107a4c
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 1 deletion.
@@ -0,0 +1,93 @@
/*
* The MIT License
*
* Copyright (c) 2016, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

package io.blueocean.rest.pipeline.editor;

import java.util.ArrayList;
import java.util.List;

import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;

/**
* Provides tool information
*/
@ExportedBean
public class ExportedToolDescriptor {
private final String toolName;
private final String symbol;
private final Class<?> type;
private final List<ExportedToolInstallation> installations = new ArrayList<ExportedToolInstallation>();

public ExportedToolDescriptor(String toolName, String symbol, Class<?> type) {
this.toolName = toolName;
this.symbol = symbol;
this.type = type;
}

@Exported
public String getToolName() {
return toolName;
}

@Exported
public String getSymbol() {
return symbol;
}

@Exported
public String getType() {
return type.getName();
}

@Exported
public ExportedToolInstallation[] getInstallations() {
return installations.toArray(new ExportedToolInstallation[installations.size()]);
}

public void addInstallation(ExportedToolInstallation installation) {
this.installations.add(installation);
}

@ExportedBean
public static class ExportedToolInstallation {
private final String name;
private final Class<?> type;

public ExportedToolInstallation(String name, Class<?> type) {
this.name = name;
this.type = type;
}

@Exported
public String getName() {
return name;
}

@Exported
public String getType() {
return type.getName();
}
}
}
Expand Up @@ -12,6 +12,8 @@
import hudson.model.Describable;
import hudson.tasks.Builder;
import hudson.tasks.Publisher;
import hudson.tools.ToolDescriptor;
import hudson.tools.ToolInstallation;
import jenkins.tasks.SimpleBuildStep;
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgent;
import org.jenkinsci.plugins.pipeline.modeldefinition.agent.DeclarativeAgentDescriptor;
Expand All @@ -22,7 +24,6 @@
import org.jenkinsci.plugins.workflow.steps.Step;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
import org.kohsuke.stapler.NoStaplerConstructorException;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.verb.GET;

import hudson.Extension;
Expand Down Expand Up @@ -75,6 +76,24 @@ public ExportedDescribableModel[] doAgentMetadata() {
return models.toArray(new ExportedDescribableModel[models.size()]);
}

/**
* Function to return all {@link ExportedToolDescriptor}s present in the system when accessed through the REST API,
* pipeline scripts need: symbol and name to specify tools
*/
@GET
@TreeResponse
public ExportedToolDescriptor[] doToolMetadata() {
List<ExportedToolDescriptor> models = new ArrayList<>();
for (ToolDescriptor<? extends ToolInstallation> d : ToolInstallation.all()) {
ExportedToolDescriptor descriptor = new ExportedToolDescriptor(d.getDisplayName(), symbolForObject(d), d.getClass());
models.add(descriptor);
for (ToolInstallation installation : d.getInstallations()) {
descriptor.addInstallation(new ExportedToolDescriptor.ExportedToolInstallation(installation.getName(), installation.getClass()));
}
}
return models.toArray(new ExportedToolDescriptor[models.size()]);
}

/**
* Function to return the names of all build conditions present in the system when accessed through the REST API
*/
Expand Down
Expand Up @@ -12,6 +12,7 @@
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.JenkinsRule.JSONWebResponse;

import hudson.model.JDK;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

Expand Down Expand Up @@ -73,6 +74,28 @@ public void declarativeAgents() throws Exception {
assertEquals(3, m.getParameters().size());
}

@Test
public void toolMetadata() throws Exception {
PipelineMetadataService svc = new PipelineMetadataService();

List<ExportedToolDescriptor> tools = new ArrayList<>();
tools.addAll(Arrays.asList(svc.doToolMetadata()));

assertFalse(tools.isEmpty());

ExportedToolDescriptor t = null;

for (ExportedToolDescriptor a : tools) {
if (a.getType().equals(JDK.DescriptorImpl.class.getName())) {
t = a;
}
}

assertNotNull(t);

assertEquals("jdk", t.getSymbol());
}

@Test
public void wrappers() throws Exception {
PipelineMetadataService svc = new PipelineMetadataService();
Expand Down

0 comments on commit 8107a4c

Please sign in to comment.