Skip to content
This repository was archived by the owner on Apr 23, 2025. It is now read-only.

Commit 10c509b

Browse files
authored
feat: list helm repositories (#672) (#736)
Signed-off-by: Andre Dietisheim <[email protected]>
1 parent 360fb52 commit 10c509b

File tree

13 files changed

+253
-28
lines changed

13 files changed

+253
-28
lines changed

src/it/java/org/jboss/tools/intellij/openshift/utils/helm/HelmCliListTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ public void testList_should_list_kuberos() throws Exception {
2020
// given openshift repo was added to helm
2121
String releaseName = Charts.CHART_KUBEROS + new Random().nextInt();
2222
try {
23-
Chart jenkinsChart = Charts.get(Charts.CHART_KUBEROS, helm);
24-
helm.install(releaseName, jenkinsChart.getName(), jenkinsChart.getVersion(), null);
23+
Chart kuberosChart = Charts.get(Charts.CHART_KUBEROS, helm);
24+
helm.install(releaseName, kuberosChart.getName(), kuberosChart.getVersion(), null);
2525
// when
2626
List<ChartRelease> releases = helm.list();
2727
// then
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 Red Hat, Inc.
3+
* Distributed under license by Red Hat, Inc. All rights reserved.
4+
* This program is made available under the terms of the
5+
* Eclipse Public License v2.0 which accompanies this distribution,
6+
* and is available at http://www.eclipse.org/legal/epl-v20.html
7+
*
8+
* Contributors:
9+
* Red Hat, Inc. - initial API and implementation
10+
******************************************************************************/
11+
package org.jboss.tools.intellij.openshift.utils.helm;
12+
13+
14+
import java.io.IOException;
15+
import java.util.List;
16+
17+
public class HelmCliRepoTest extends HelmCliTest {
18+
19+
public void testListRepos_should_list_repo_that_was_added() throws IOException {
20+
// given openshift repo was added to helm repos
21+
String name = "openshift";
22+
String url = "https://charts.openshift.io/";
23+
helm.addRepo(name, url);
24+
// when
25+
List<HelmRepository> repositories = helm.listRepos();
26+
// then
27+
boolean found = repositories.stream().anyMatch((HelmRepository repository) ->
28+
repository.getName().equals(name)
29+
&& repository.getUrl().equals(url)
30+
);
31+
assertTrue(found);
32+
}
33+
34+
}

src/it/java/org/jboss/tools/intellij/openshift/utils/helm/HelmCliSearchTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public void testSearch_should_list_all_charts() throws IOException {
2121
// when
2222
List<Chart> charts = helm.search();
2323
// then
24-
assertTrue(charts.size() > 0);
24+
assertFalse(charts.isEmpty());
2525
}
2626

2727
public void testSearch_should_list_kuberos() throws IOException {

src/main/java/org/jboss/tools/intellij/openshift/tree/application/ApplicationsTreeStructure.java

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,19 @@ public Object getApplicationsRoot() {
6868

6969
@NotNull
7070
@Override
71-
public Object @NotNull [] getChildElements(@NotNull Object element) {
71+
public Object @NotNull [] getChildElements(@NotNull Object element) {
7272
try {
7373
if (element == this) {
7474
return new Object[]{root, registries};
7575
} else if (element instanceof ApplicationsRootNode) {
76-
return getCurrentNamespace((ApplicationsRootNode) element);
76+
return new Object[] {
77+
getCurrentNamespace((ApplicationsRootNode) element),
78+
new HelmRepositoriesNode((ApplicationsRootNode) element)
79+
};
7780
} else if (element instanceof NamespaceNode) {
7881
return createNamespaceChildren((NamespaceNode) element);
82+
} else if (element instanceof HelmRepositoriesNode) {
83+
return createHelmRepositoriesChildren((HelmRepositoriesNode) element);
7984
} else if (element instanceof ComponentNode) {
8085
return createComponentChildren((ComponentNode) element);
8186
} else if (element instanceof DevfileRegistriesNode) {
@@ -134,8 +139,9 @@ private Object[] createNamespaceChildren(@NotNull NamespaceNode namespaceNode) {
134139
return nodes.toArray();
135140
}
136141

137-
private Object[] getCurrentNamespace(ApplicationsRootNode element) {
138-
List<Object> namespaces = new ArrayList<>();
142+
@NotNull
143+
private Object getCurrentNamespace(ApplicationsRootNode element) {
144+
Object node;
139145
try {
140146
Odo odo = root.getOdo().getNow(null);
141147
if (odo == null) {
@@ -144,20 +150,38 @@ private Object[] getCurrentNamespace(ApplicationsRootNode element) {
144150
boolean isAuthorized = odo.isAuthorized();
145151
element.setLogged(isAuthorized);
146152
if (!isAuthorized) {
147-
namespaces.add(new MessageNode<>(root, root, LOGIN));
153+
node = new MessageNode<>(root, root, LOGIN);
148154
} else {
149155
String namespace = odo.getCurrentNamespace();
150156
if (namespace != null) {
151-
namespaces.add(new NamespaceNode(element, namespace));
157+
node = new NamespaceNode(element, namespace);
152158
} else {
153-
namespaces.add(new CreateNamespaceLinkNode(element));
159+
node = new CreateNamespaceLinkNode(element);
154160
}
155161
}
156162
} catch (Exception e) {
157-
namespaces.add(createErrorNode(element, e));
163+
node = createErrorNode(element, e);
158164
element.setLogged(false);
159165
}
160-
return namespaces.toArray();
166+
return node;
167+
}
168+
169+
private Object[] createHelmRepositoriesChildren(HelmRepositoriesNode parent) {
170+
Helm helm = root.getHelm(true).getNow(null);
171+
if (helm == null) {
172+
return new Object[] { new MessageNode<>(root, parent, "Could not list repositories: Helm binary missing.") };
173+
}
174+
try {
175+
var repositories = helm.listRepos();
176+
if (repositories == null) {
177+
return new Object[] { new MessageNode<>(root, parent, "Could not list repositories: no repositories defined.") };
178+
}
179+
return repositories.stream()
180+
.map(repository -> new HelmRepositoryNode(root, parent, repository))
181+
.toArray();
182+
} catch (IOException e) {
183+
throw new RuntimeException(e);
184+
}
161185
}
162186

163187
private MessageNode<?> createErrorNode(ParentableNode<?> parent, Exception e) {

src/main/java/org/jboss/tools/intellij/openshift/tree/application/DescriptorFactory.java

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.intellij.openapi.project.Project;
1515
import com.intellij.openapi.util.IconLoader;
1616
import com.redhat.devtools.intellij.common.tree.LabelAndIconDescriptor;
17+
import org.jboss.tools.intellij.openshift.ui.SwingUtils;
1718
import org.jboss.tools.intellij.openshift.ui.helm.ChartIcons;
1819
import org.jboss.tools.intellij.openshift.utils.odo.Binding;
1920
import org.jboss.tools.intellij.openshift.utils.odo.Component;
@@ -35,8 +36,11 @@ public class DescriptorFactory {
3536
private static final Icon COMPONENT_TYPE_ICON = IconLoader.findIcon("/images/component-type-light.png", ApplicationsTreeStructure.class);
3637
private static final Icon STARTER_ICON = IconLoader.findIcon("/images/start-project-light.png", ApplicationsTreeStructure.class);
3738
private static final Icon REGISTRY_ICON = IconLoader.findIcon("/images/registry.svg", ApplicationsTreeStructure.class);
39+
private static final Icon HELM_REPOSITORY_ICON = IconLoader.findIcon("/images/helm/repo.svg", ApplicationsTreeStructure.class);
3840

39-
public static @NotNull NodeDescriptor<?> create(@NotNull Object element, @Nullable NodeDescriptor parentDescriptor, @NotNull ApplicationsTreeStructure structure, @NotNull Project project) {
41+
private static final int ICON_WIDTH = 15;
42+
43+
public static @NotNull NodeDescriptor<?> create(@NotNull Object element, @Nullable NodeDescriptor<?> parentDescriptor, @NotNull ApplicationsTreeStructure structure, @NotNull Project project) {
4044
if (element == structure) {
4145
return new LabelAndIconDescriptor<>(
4246
project,
@@ -157,7 +161,29 @@ public class DescriptorFactory {
157161
releaseNode,
158162
releaseNode::getName,
159163
() -> "Helm Release",
160-
() -> ChartIcons.getIcon15x15(releaseNode.getRelease()),
164+
() -> SwingUtils.scaleIcon(ICON_WIDTH, ChartIcons.getIcon(releaseNode.getRelease())),
165+
parentDescriptor);
166+
} else if (element instanceof HelmRepositoriesNode) {
167+
HelmRepositoriesNode helmRepositoriesNode = (HelmRepositoriesNode) element;
168+
return new ApplicationsTreeStructure.ProcessableDescriptor<>(
169+
project,
170+
helmRepositoriesNode,
171+
helmRepositoriesNode::getName,
172+
() -> "Repositories",
173+
() -> SwingUtils.scaleIcon(ICON_WIDTH, ChartIcons.getHelmIcon()),
174+
parentDescriptor);
175+
} else if (element instanceof HelmRepositoryNode) {
176+
HelmRepositoryNode helmRepositoryNode = (HelmRepositoryNode) element;
177+
return new ApplicationsTreeStructure.ProcessableDescriptor<>(
178+
project,
179+
helmRepositoryNode,
180+
helmRepositoryNode::getName,
181+
() -> {
182+
var repository = helmRepositoryNode.getRepository();
183+
return repository != null ?
184+
repository.getUrl() : "";
185+
},
186+
() -> HELM_REPOSITORY_ICON,
161187
parentDescriptor);
162188
}
163189

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 Red Hat, Inc.
3+
* Distributed under license by Red Hat, Inc. All rights reserved.
4+
* This program is made available under the terms of the
5+
* Eclipse Public License v2.0 which accompanies this distribution,
6+
* and is available at http://www.eclipse.org/legal/epl-v20.html
7+
*
8+
* Contributors:
9+
* Red Hat, Inc. - initial API and implementation
10+
******************************************************************************/
11+
package org.jboss.tools.intellij.openshift.tree.application;
12+
13+
public class HelmRepositoriesNode extends BaseNode<ApplicationsRootNode> {
14+
public HelmRepositoriesNode(ApplicationsRootNode parent) {
15+
super(parent, parent, "Helm");
16+
}
17+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2024 Red Hat, Inc.
3+
* Distributed under license by Red Hat, Inc. All rights reserved.
4+
* This program is made available under the terms of the
5+
* Eclipse Public License v2.0 which accompanies this distribution,
6+
* and is available at http://www.eclipse.org/legal/epl-v20.html
7+
*
8+
* Contributors:
9+
* Red Hat, Inc. - initial API and implementation
10+
******************************************************************************/
11+
package org.jboss.tools.intellij.openshift.tree.application;
12+
13+
import org.jboss.tools.intellij.openshift.utils.helm.HelmRepository;
14+
15+
public class HelmRepositoryNode extends BaseNode<HelmRepositoriesNode> {
16+
17+
private final HelmRepository repository;
18+
19+
public HelmRepositoryNode(ApplicationsRootNode root, HelmRepositoriesNode parent, HelmRepository repository) {
20+
super(root, parent, repository.getName());
21+
this.repository = repository;
22+
}
23+
24+
public HelmRepository getRepository() {
25+
return repository;
26+
}
27+
}

src/main/java/org/jboss/tools/intellij/openshift/ui/SwingUtils.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,11 @@
1414
import com.intellij.openapi.application.ApplicationManager;
1515
import com.intellij.openapi.wm.impl.IdeGlassPaneEx;
1616
import com.intellij.ui.JBColor;
17+
import com.intellij.ui.SizedIcon;
1718
import com.intellij.ui.WindowMoveListener;
1819
import com.intellij.ui.WindowResizeListener;
1920
import com.intellij.ui.components.JBScrollPane;
21+
import com.intellij.ui.scale.JBUIScale;
2022
import com.intellij.ui.table.JBTable;
2123
import com.intellij.util.containers.JBIterable;
2224
import com.intellij.util.ui.JBFont;
@@ -26,6 +28,7 @@
2628

2729
import javax.swing.AbstractButton;
2830
import javax.swing.DefaultCellEditor;
31+
import javax.swing.Icon;
2932
import javax.swing.JComboBox;
3033
import javax.swing.JComponent;
3134
import javax.swing.JLabel;
@@ -153,4 +156,10 @@ public static Point locationOrMouseLocation(Point location) {
153156
return location;
154157
}
155158

159+
public static Icon scaleIcon(int width, Icon icon) {
160+
float scale = (float) width / icon.getIconWidth();
161+
SizedIcon scaled = JBUIScale.scaleIcon(new SizedIcon(icon, icon.getIconWidth(), icon.getIconHeight()));
162+
return scaled.scale(scale);
163+
}
164+
156165
}

src/main/java/org/jboss/tools/intellij/openshift/ui/helm/ChartIcons.java

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111
package org.jboss.tools.intellij.openshift.ui.helm;
1212

1313
import com.intellij.ui.IconManager;
14-
import com.intellij.ui.SizedIcon;
15-
import com.intellij.ui.scale.JBUIScale;
1614
import org.jboss.tools.intellij.openshift.utils.helm.ChartRelease;
1715

1816
import javax.swing.Icon;
@@ -26,28 +24,25 @@ public class ChartIcons {
2624
private static final Path BASE_PATH = Paths.get("images", "helm");
2725
private static final String HELM_ICON = "helm.png";
2826

29-
public static javax.swing.Icon getIcon(ChartVersions chart) {
30-
return getIcon(chart.getName() + chart.getDescription());
27+
public static Icon getHelmIcon() {
28+
return IconManager.getInstance().getIcon(BASE_PATH.resolve(HELM_ICON).toString(), ChartIcons.class);
3129
}
3230

33-
public static javax.swing.Icon getIcon(ChartRelease chart) {
34-
return getIcon(chart.getChart());
31+
public static Icon getIcon(ChartVersions chart) {
32+
return getIcon(chart.getName() + chart.getDescription());
3533
}
3634

37-
public static javax.swing.Icon getIcon15x15(ChartRelease chart) {
38-
Icon icon = getIcon(chart);
39-
float scale = 15f / icon.getIconWidth();
40-
SizedIcon sized = JBUIScale.scaleIcon(new SizedIcon(icon, icon.getIconHeight(), icon.getIconHeight()));
41-
return sized.scale(scale);
35+
public static Icon getIcon(ChartRelease chart) {
36+
return getIcon(chart.getChart());
4237
}
4338

44-
private static javax.swing.Icon getIcon(String name) {
39+
private static Icon getIcon(String name) {
4540
Optional<IconExpression> found = Stream.of(IconExpression.values())
4641
.filter((IconExpression available) -> available.isMatching(name))
4742
.findFirst();
4843
return found
4944
.map(iconExpression -> IconManager.getInstance().getIcon(iconExpression.filename, ChartIcons.class))
50-
.orElseGet(() -> IconManager.getInstance().getIcon(BASE_PATH.resolve(HELM_ICON).toString(), ChartIcons.class));
45+
.orElseGet(ChartIcons::getHelmIcon);
5146
}
5247

5348
private enum IconExpression {
@@ -88,5 +83,4 @@ public boolean isMatching(String chartText) {
8883
return chartText.toLowerCase().contains(substring.toLowerCase());
8984
}
9085
}
91-
9286
}

src/main/java/org/jboss/tools/intellij/openshift/utils/helm/Helm.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ public interface Helm {
1717

1818
String addRepo(String name, String url) throws IOException;
1919

20+
List<HelmRepository> listRepos() throws IOException;
21+
2022
List<Chart> search() throws IOException;
2123

2224
List<Chart> search(String regex) throws IOException;

0 commit comments

Comments
 (0)