Skip to content

Commit

Permalink
impl'd 'remove repo' action (#673)
Browse files Browse the repository at this point in the history
Signed-off-by: Andre Dietisheim <[email protected]>
  • Loading branch information
adietish committed Apr 22, 2024
1 parent 68272b4 commit 5c1b9ff
Show file tree
Hide file tree
Showing 7 changed files with 161 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public static Chart get(String name, Helm helm) throws Exception {
}

public static void addRepository(Pair<String, String> pair, Helm helm) throws IOException {
helm.addRepo(pair.first, pair.second);
helm.addRepo(pair.first, pair.second, null);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public void testListRepos_should_list_repo_that_was_added() throws IOException {
// given openshift repo was added to helm repos
String name = "openshift";
String url = "https://charts.openshift.io/";
helm.addRepo(name, url);
helm.addRepo(name, url, null);
// when
List<HelmRepository> repositories = helm.listRepos();
// then
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
/*******************************************************************************
* Copyright (c) 2024 Red Hat, Inc.
* Distributed under license by Red Hat, Inc. All rights reserved.
* This program is made available under the terms of the
* Eclipse Public License v2.0 which accompanies this distribution,
* and is available at http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* Red Hat, Inc. - initial API and implementation
******************************************************************************/
package org.jboss.tools.intellij.openshift.actions.helm;

import com.intellij.openapi.actionSystem.AnActionEvent;
import com.intellij.openapi.progress.ProgressIndicator;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.Messages;
import com.redhat.devtools.intellij.common.actions.StructureTreeAction;
import com.redhat.devtools.intellij.common.utils.UIHelper;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
import javax.swing.tree.TreePath;
import org.jboss.tools.intellij.openshift.actions.HelmAction;
import org.jboss.tools.intellij.openshift.telemetry.TelemetryService;
import org.jboss.tools.intellij.openshift.tree.application.ApplicationsRootNode;
import org.jboss.tools.intellij.openshift.tree.application.HelmRepositoryNode;
import org.jboss.tools.intellij.openshift.tree.application.ProcessingNode;
import org.jboss.tools.intellij.openshift.utils.helm.Helm;
import org.jetbrains.annotations.Nullable;

import static org.jboss.tools.intellij.openshift.actions.ActionUtils.runWithProgress;
import static org.jboss.tools.intellij.openshift.actions.NodeUtils.clearProcessing;
import static org.jboss.tools.intellij.openshift.actions.NodeUtils.setProcessing;

public class RemoveRepositoriesAction extends HelmAction {

@Override
public void actionPerformed(AnActionEvent anActionEvent, TreePath[] path, Object[] selected) {
Helm helm = getHelm(anActionEvent);
if (helm == null) {
return;
}
Project project = getEventProject(anActionEvent);
List<HelmRepositoryNode> repositories = toHelmRepositoryNodes(selected);
if (repositories == null
|| repositories.isEmpty()) {
return;
}

if (cancelRemoval(repositories)) {
sendTelemetryResults(TelemetryService.TelemetryResult.ABORTED);
return;
}

runWithProgress((ProgressIndicator progress) -> {
removeRepositories(repositories, helm);
},
"Remove Helm Repositories...",
project);
}

private void removeRepositories(List<HelmRepositoryNode> repositories, Helm helm) {
ProcessingNode[] processingNodes = repositories.toArray(new ProcessingNode[0]);
ApplicationsRootNode rootNode = repositories.get(0).getRoot();
try {
setProcessing("removing...", rootNode, processingNodes);
helm.removeRepos(
repositories.stream()
.map(HelmRepositoryNode::getName)
.toArray(String[]::new));
clearProcessing(rootNode, processingNodes);
sendTelemetryResults(TelemetryService.TelemetryResult.SUCCESS);
} catch (Exception e) {
clearProcessing(rootNode, processingNodes);
UIHelper.executeInUI(() -> Messages.showErrorDialog("Error: " + e.getLocalizedMessage(), "Remove Helm Repositories"));
sendTelemetryResults(TelemetryService.TelemetryResult.ERROR);
}
}

@Nullable
private static List<HelmRepositoryNode> toHelmRepositoryNodes(Object[] selected) {
if (selected == null
|| selected.length == 0) {
return null;
}

return Arrays.stream(selected)
.map(StructureTreeAction::getElement)
.filter(HelmRepositoryNode.class::isInstance)
.map(HelmRepositoryNode.class::cast)
.collect(Collectors.toList());
}

private boolean cancelRemoval(List<HelmRepositoryNode> releases) {
String repositoriesNames = releases.stream()
.map(HelmRepositoryNode::getName)
.collect(Collectors.joining(", "));
return Messages.NO == Messages.showYesNoDialog(
"Remove Repositories "
+ repositoriesNames
+ ".\n\nAre you sure?",
"Remove Repositories",
Messages.getQuestionIcon());
}

@Override
public String getTelemetryActionName() {
return "helm-remove repositories";
}

@Override
public boolean isVisible(Object[] selected) {
return Arrays.stream(selected).anyMatch(item -> {
Object node = getElement(item);
if (!(node instanceof HelmRepositoryNode)) {
return false;
}
return !((HelmRepositoryNode) node).isProcessing();
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ private CompletableFuture<Void> addDefaultRepo(Helm helm) {
return CompletableFuture
.runAsync(() -> {
try {
helm.addRepo(OPENSHIFT_REPO_NAME, OPENSHIFT_REPO_URL);
helm.addRepo(OPENSHIFT_REPO_NAME, OPENSHIFT_REPO_URL, null);
} catch (IOException e) {
throw new RuntimeException(e.getMessage(), e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@

public interface Helm {

String addRepo(String name, String url) throws IOException;
String addRepo(String name, String url, String flags) throws IOException;

void removeRepos(String... name) throws IOException;

List<HelmRepository> listRepos() throws IOException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,20 @@
import com.fasterxml.jackson.core.type.TypeReference;
import com.intellij.openapi.util.text.StringUtil;
import com.redhat.devtools.intellij.common.utils.ExecHelper;
import org.jboss.tools.intellij.openshift.telemetry.TelemetryService;
import org.jboss.tools.intellij.openshift.utils.Serialization;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.jboss.tools.intellij.openshift.telemetry.TelemetryService;
import org.jboss.tools.intellij.openshift.utils.Serialization;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static com.redhat.devtools.intellij.telemetry.core.service.TelemetryMessageBuilder.ActionMessage;
import static org.jboss.tools.intellij.openshift.Constants.HOME_FOLDER;
Expand All @@ -43,12 +43,12 @@ public HelmCli(String command) {
}

@Override
public String addRepo(String name, String url) throws IOException {
public String addRepo(String name, String url, String flags) throws IOException {
ActionMessage telemetry = TelemetryService.instance().getBuilder().action(
TelemetryService.NAME_PREFIX_MISC + "helm-add repo");
try {
LOGGER.info("Adding repo {} at {}.", name, url);
String result = execute(command, Collections.emptyMap(), "repo", "add", name, url);
String result = execute(command, Collections.emptyMap(), "repo", "add", name, url, flags);
asyncSend(telemetry.success());
return result;
} catch (IOException e) {
Expand All @@ -58,6 +58,32 @@ public String addRepo(String name, String url) throws IOException {
}

@Override
public void removeRepos(String... names) throws IOException {
LOGGER.info("Removing repositories {}.", String.join(", ", names));
List<String> notRemoved = Arrays.stream(names)
.map(this::removeRepo)
.filter(Objects::nonNull)
.toList();
if (!notRemoved.isEmpty()) {
throw new IOException("Could not remove repositories " + String.join(", ", notRemoved));
}
}

private String removeRepo(String name) {
ActionMessage telemetry = TelemetryService.instance().getBuilder().action(
TelemetryService.NAME_PREFIX_MISC + "helm-remove repository");
try {
execute(command, Collections.emptyMap(), "repo", "remove", name);
asyncSend(telemetry.success());
return null;
} catch (IOException e) {
LOGGER.info("Could not remove repository " + name, e);
asyncSend(telemetry.error(e.getMessage()));
return name;
}
}

@Override
public List<HelmRepository> listRepos() throws IOException {
ActionMessage telemetry = TelemetryService.instance().getBuilder().action(
TelemetryService.NAME_PREFIX_MISC + "helm-list repo");
Expand Down
1 change: 1 addition & 0 deletions src/main/resources/META-INF/plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,7 @@
<!-- helm -->
<action class="org.jboss.tools.intellij.openshift.actions.helm.OpenHelmChartsAction" id="org.jboss.tools.intellij.openshift.actions.helm.OpenHelmChartsAction" text="Open Helm Charts"/>
<action class="org.jboss.tools.intellij.openshift.actions.helm.AddHelmRepoAction" id="org.jboss.tools.intellij.openshift.actions.helm.AddHelmRepoAction" text="Add Repository"/>
<action class="org.jboss.tools.intellij.openshift.actions.helm.RemoveRepositoriesAction" id="org.jboss.tools.intellij.openshift.actions.helm.RemoveRepositoriesAction" text="Remove Repositories"/>
<separator/>
<!-- registry management -->
<action class="org.jboss.tools.intellij.openshift.actions.registry.CreateRegistryAction" id="org.jboss.tools.intellij.openshift.actions.registry.CreateRegistryAction" text="New registry"/>
Expand Down

0 comments on commit 5c1b9ff

Please sign in to comment.