Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add support for optional workspace #458

Merged
merged 3 commits into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public class Constants {
public static final String FLAG_OUTPUTRESOURCE = "-o";
public static final String FLAG_WORKSPACE = "-w";
public static final String FLAG_PREFIXNAME = "--prefix-name";
public static final String FLAG_SKIP_OPTIONAL_WORKSPACES = "--skip-optional-workspace";

public static final String TERMINAL_TITLE = "Tekton";

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.redhat.devtools.intellij.common.utils.YAMLHelper;
import com.redhat.devtools.intellij.tektoncd.tkn.component.field.Input;
import com.redhat.devtools.intellij.tektoncd.tkn.component.field.Output;
import com.redhat.devtools.intellij.tektoncd.tkn.component.field.Workspace;
import com.redhat.devtools.intellij.tektoncd.utils.TektonVirtualFileManager;
import com.redhat.devtools.intellij.tektoncd.utils.model.ConfigurationModel;
import com.redhat.devtools.intellij.tektoncd.utils.model.ConfigurationModelFactory;
Expand All @@ -30,6 +31,10 @@
import com.redhat.devtools.intellij.tektoncd.utils.model.resources.TaskConfigurationModel;
import io.fabric8.kubernetes.client.utils.Serialization;
import io.fabric8.tekton.pipeline.v1beta1.Task;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -39,10 +44,6 @@
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.jetbrains.annotations.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


import static com.redhat.devtools.intellij.tektoncd.Constants.KIND_TASK;
import static com.redhat.devtools.intellij.tektoncd.Constants.NAMESPACE;
Expand Down Expand Up @@ -335,7 +336,7 @@ private List<LookupElementBuilder> getTasksInPipelineLookups(CompletionParameter
* @param insertOffset the position where the lookup has to be copied on
* @return
*/
private List<LookupElementBuilder> getWorkspaceLookups(List<String> workspaces, String prefix, String completionPrefix, int insertOffset) {
private List<LookupElementBuilder> getWorkspaceLookups(List<Workspace> workspaces, String prefix, String completionPrefix, int insertOffset) {
List<LookupElementBuilder> lookups = new ArrayList<>();
String headPrefix_13 = prefix.length() > 13 ? prefix.substring(0, 13) : prefix;
if ("$(workspaces.".contains(headPrefix_13)) {
Expand All @@ -344,8 +345,8 @@ private List<LookupElementBuilder> getWorkspaceLookups(List<String> workspaces,
if (workspace != null) {
lookups.addAll(getLookupsByWorkspace("workspaces." + workspace, completionPrefix, insertOffset));
} else {
workspaces.stream().forEach(workspaceName -> {
lookups.add(createInnerLookup("workspaces." + workspaceName, completionPrefix, insertOffset));
workspaces.forEach(wk -> {
lookups.add(createInnerLookup("workspaces." + wk.getName(), completionPrefix, insertOffset));
});
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.redhat.devtools.intellij.common.utils.StringHelper;
import com.redhat.devtools.intellij.tektoncd.tkn.component.field.Input;
import com.redhat.devtools.intellij.tektoncd.tkn.component.field.Output;
import com.redhat.devtools.intellij.tektoncd.tkn.component.field.Workspace;
import com.redhat.devtools.intellij.tektoncd.utils.model.ConfigurationModel;
import com.redhat.devtools.intellij.tektoncd.utils.model.resources.PipelineConfigurationModel;
import com.redhat.devtools.intellij.tektoncd.utils.model.resources.TaskConfigurationModel;
Expand Down Expand Up @@ -57,7 +58,7 @@ public ProblemDescriptor[] checkFile(@NotNull PsiFile file, @NotNull InspectionM
private List<PsiElement> highlightInPipeline(PsiFile file, PipelineConfigurationModel model) {
List<Input> params = model.getParams();
List<Input> inputResources = model.getInputResources();
List<String> workspaces = model.getWorkspaces();
List<Workspace> workspaces = model.getWorkspaces();

if (params.isEmpty() && inputResources.isEmpty() && workspaces.isEmpty()) {
return Collections.emptyList();
Expand All @@ -70,7 +71,7 @@ private List<PsiElement> highlightInTask(PsiFile file, TaskConfigurationModel mo
List<Input> params = model.getParams();
List<Input> inputResources = model.getInputResources();
List<Output> outputResources = model.getOutputResources();
List<String> workspaces = model.getWorkspaces();
List<Workspace> workspaces = model.getWorkspaces();

if (params.isEmpty() && inputResources.isEmpty() && outputResources.isEmpty() && workspaces.isEmpty()) {
return Collections.emptyList();
Expand All @@ -79,7 +80,7 @@ private List<PsiElement> highlightInTask(PsiFile file, TaskConfigurationModel mo
return findUnusedVariables(file, params, inputResources, outputResources, workspaces);
}

private List<PsiElement> findUnusedVariables(PsiFile file, List<Input> params, List<Input> inputResource, List<Output> outputResources, List<String> workspaces) {
private List<PsiElement> findUnusedVariables(PsiFile file, List<Input> params, List<Input> inputResource, List<Output> outputResources, List<Workspace> workspaces) {
if (!file.getText().contains("\nspec:")) {
return Collections.emptyList();
}
Expand Down Expand Up @@ -124,10 +125,10 @@ private List<PsiElement> findUnusedVariables(PsiFile file, List<Input> params, L
});

workspaces.forEach(workspace -> {
Pattern pattern = isPipeline(file) ? Pattern.compile("workspace:\\s*[\"\']?" + workspace + "(?=\\s|\"|')") : Pattern.compile("\\$\\(workspaces\\." + workspace);
Pattern pattern = isPipeline(file) ? Pattern.compile("workspace:\\s*[\"\']?" + workspace.getName() + "(?=\\s|\"|')") : Pattern.compile("\\$\\(workspaces\\." + workspace.getName());
List<Integer> variableUsageIndexes = indexesOfByPattern(pattern, spec);
if (variableUsageIndexes.isEmpty()) {
PsiElement psiNode = getVariablePsiElement(file, "workspaces:", workspace);
PsiElement psiNode = getVariablePsiElement(file, "workspaces:", workspace.getName());
if (psiNode != null) {
unusedPsiElements.add(psiNode);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@
import static com.redhat.devtools.intellij.tektoncd.Constants.FLAG_PARAMETER;
import static com.redhat.devtools.intellij.tektoncd.Constants.FLAG_PREFIXNAME;
import static com.redhat.devtools.intellij.tektoncd.Constants.FLAG_SERVICEACCOUNT;
import static com.redhat.devtools.intellij.tektoncd.Constants.FLAG_SKIP_OPTIONAL_WORKSPACES;
import static com.redhat.devtools.intellij.tektoncd.Constants.FLAG_TASKSERVICEACCOUNT;
import static com.redhat.devtools.intellij.tektoncd.Constants.FLAG_WORKSPACE;
import static com.redhat.devtools.intellij.tektoncd.Constants.KIND_CONFIGMAP;
Expand Down Expand Up @@ -645,13 +646,14 @@ public String startPipeline(String namespace, String pipeline, Map<String, Input
if (!runPrefixName.isEmpty()) {
args.add(FLAG_PREFIXNAME + "=" + runPrefixName);
}
args.add(FLAG_SKIP_OPTIONAL_WORKSPACES);
String output = ExecHelper.execute(command, envVars, args.toArray(new String[0]));
return this.getTektonRunName(output);
}

@Override
public String startLastPipeline(String namespace, String pipeline) throws IOException {
String output = ExecHelper.execute(command, envVars, "pipeline", "start", pipeline, "--last", "-n", namespace);
String output = ExecHelper.execute(command, envVars, "pipeline", "start", pipeline, "--last", "-n", namespace, FLAG_SKIP_OPTIONAL_WORKSPACES);
return this.getTektonRunName(output);
}

Expand Down Expand Up @@ -688,6 +690,7 @@ private String startTask(List<String> args, Map<String, Input> parameters, Map<S
if (!runPrefixName.isEmpty()) {
args.add(FLAG_PREFIXNAME + "=" + runPrefixName);
}
args.add(FLAG_SKIP_OPTIONAL_WORKSPACES);
return ExecHelper.execute(command, envVars, args.toArray(new String[0]));
}

Expand All @@ -698,7 +701,7 @@ private String startTaskAndGetRunName(List<String> args, Map<String, Input> para

@Override
public String startLastTask(String namespace, String task) throws IOException {
String output = ExecHelper.execute(command, envVars, "task", "start", task, "--last", "-n", namespace);
String output = ExecHelper.execute(command, envVars, "task", "start", task, "--last", "-n", namespace, FLAG_SKIP_OPTIONAL_WORKSPACES);
return getTektonRunName(output);
}

Expand Down Expand Up @@ -734,8 +737,11 @@ private List<String> workspaceArgsToList(Map<String, Workspace> argMap) {
argMap.entrySet().forEach(item -> {
String name = item.getKey();
Workspace workspace = item.getValue();
if (workspace.isOptional() && workspace.getKind() == null) {
return;
}
args.add(FLAG_WORKSPACE);
if (workspace == null || workspace.getKind() == Workspace.Kind.EMPTYDIR) {
if (workspace.getKind() == Workspace.Kind.EMPTYDIR) {
args.add("name=" + name + ",emptyDir=");
} else if (workspace.getKind() == Workspace.Kind.PVC) {
if (workspace.getItems() != null && workspace.getItems().containsKey("file")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,41 @@ public String toString() {
private String name;
private Kind kind;
private String resource;
private String subPath;
private boolean optional;
private Map<String, String> items;

public Workspace(String name, Kind kind, String resource) {
this(name, kind, resource, Collections.emptyMap());
this(name, kind, resource, Collections.emptyMap(), false);
}

public Workspace(String name, Kind kind, String resource, Map<String, String> items) {
public Workspace(String name, Kind kind, String resource, boolean optional) {
this(name, kind, resource, Collections.emptyMap(), optional);
}

public Workspace(String name, Kind kind, String resource, Map<String, String> items, boolean optional) {
this.name = name;
this.kind = kind;
this.resource = resource;
this.items = items;
this.optional = optional;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public Kind getKind() {
return kind;
}

public void setKind(Kind kind) {
this.kind = kind;
}

public String getResource() {
return resource;
}
Expand All @@ -68,8 +81,8 @@ public void setResource(String resource) {
this.resource = resource;
}

public String getSubPath() {
return subPath;
public boolean isOptional() {
return optional;
}

public Map<String, String> getItems() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,16 +79,19 @@ public WorkspacesStep(ActionToRunModel model) {

@Override
public boolean isComplete() {
boolean isComplete = model.getWorkspaces().values().stream().allMatch(workspace -> workspace != null);
boolean isComplete = model.getWorkspaces().values().stream().allMatch(workspace -> {
return workspace.getKind() != null
|| workspace.isOptional();
});
changeErrorTextVisibility(!isComplete);
return isComplete;
}

private Workspace saveNewVolume(String workspaceName, String name, Workspace.Kind kind, JPanel panel) {
return saveNewVolume(workspaceName, name, kind, panel, false);
private Workspace saveNewVolume(String workspaceName, String name, Workspace.Kind kind, JPanel panel, boolean isOptional) {
return saveNewVolume(workspaceName, name, kind, panel, false, isOptional);
}

private Workspace saveNewVolume(String workspaceName, String name, Workspace.Kind kind, JPanel panel, boolean isVCT) {
private Workspace saveNewVolume(String workspaceName, String name, Workspace.Kind kind, JPanel panel, boolean isVCT, boolean isOptional) {
JPanel accessModePanel = (JPanel) Arrays.stream(panel.getComponents())
.filter(component -> "accessModePanel".equals(component.getName())).findFirst().get();
JComboBox accessModeComboBox = (JComboBox) Arrays.stream(accessModePanel.getComponents())
Expand Down Expand Up @@ -118,7 +121,7 @@ private Workspace saveNewVolume(String workspaceName, String name, Workspace.Kin
values.put("accessMode", ((Pair)accessModeComboBox.getSelectedItem()).getSecond().toString());
values.put("size", size);
values.put("unit", ((Pair)sizeUnitComboBox.getSelectedItem()).getSecond().toString());
return new Workspace(workspaceName, kind, "", values);
return new Workspace(workspaceName, kind, "", values, isOptional);
}

@Override
Expand All @@ -143,7 +146,9 @@ public void setContent() {
JPanel panelWrapperWorkspace = new JPanel(new GridBagLayout());
panelWrapperWorkspace.setBackground(backgroundTheme);

JLabel lblWorkspaceName = createLabel(workspaceName, 11, "", null);
boolean optional = workspace != null && workspace.isOptional();
String lblOptional = optional ? "<span style=\"font-family:serif;font-size:10;font-weight:normal;font-style:italic;\">(Optional)</span>" : "";
JLabel lblWorkspaceName = createLabel(workspaceName, lblOptional, 11, "", null);
addComponent(lblWorkspaceName, panelWrapperWorkspace, workspacePanelConstraint, null, getBottomBorder(), ROW_DIMENSION, 0, innerPanelRow, GridBagConstraints.NORTH);
innerPanelRow += 1;

Expand All @@ -166,7 +171,7 @@ public void setContent() {
addListeners(workspaceName, panelWrapperWorkspace, innerPanelRow - 1);
innerPanelRow += 1;

JLabel lblNewPVCName = createLabel("Name:", 9, "lblNameNewPVC", null);
JLabel lblNewPVCName = createLabel("Name:", "", 9, "lblNameNewPVC", null);

JTextField txtNewPVCName = new JTextField("");
txtNewPVCName.setName("txtNameNewPVC");
Expand All @@ -182,7 +187,7 @@ protected void textChanged(@NotNull DocumentEvent e) {
newPVCNamePanel.setVisible(false);
innerPanelRow += 1;

JLabel lblNewVolumeAccessMode = createLabel("Mode:", 9, "lblAccessMode", null);
JLabel lblNewVolumeAccessMode = createLabel("Mode:", "", 9, "lblAccessMode", null);

JComboBox cmbNewVolumeAccessMode = createCustomCombo("cmbAccessMode",
getBasicComboBoxRenderer(),
Expand All @@ -194,7 +199,7 @@ protected void textChanged(@NotNull DocumentEvent e) {
addComponent(panelNewVolumeAccessMode, panelWrapperWorkspace, workspacePanelConstraint,null, NO_BORDER, ROW_DIMENSION, 0, innerPanelRow, GridBagConstraints.NORTH);
innerPanelRow += 1;

JLabel lblNewVolumeSize = createLabel("Size:", 9, "lblSize", lblNewPVCName.getPreferredSize());
JLabel lblNewVolumeSize = createLabel("Size:", "", 9, "lblSize", lblNewPVCName.getPreferredSize());

String sizeDefaultValue = getVCTItemValue(workspace, "size");
int sizeDefaultValueAsInteger = sizeDefaultValue.isEmpty() ? 1 : Integer.parseInt(sizeDefaultValue);
Expand Down Expand Up @@ -228,9 +233,8 @@ protected void textChanged(@NotNull DocumentEvent e) {
});

}

private JLabel createLabel(String text, int pixel, String name, Dimension size) {
JLabel label = new JLabel("<html><span style=\\\"font-family:serif;font-size:" + pixel + "px;font-weight:bold;\\\">" + text + "</span> </html");
private JLabel createLabel(String text, String optional, int pixel, String name, Dimension size) {
JLabel label = new JLabel("<html><span style=\\\"font-family:serif;font-size:" + pixel + "px;font-weight:bold;\\\">" + text + "</span>" + optional + "</html");
if (!name.isEmpty()) {
label.setName(name);
}
Expand Down Expand Up @@ -351,7 +355,7 @@ private void addListeners(String workspace, JPanel parent, int row) {
String resource = getSelectedWorkspaceType(cmbWorkspaceTypeValues);
updateWorkspaceModel(parent, workspace, kindSelected, resource);
// reset error graphics if error occurred earlier
if (isValid(cmbWorkspaceTypes)) {
if (isValid(workspace, cmbWorkspaceTypes)) {
changeErrorTextVisibility(false);
cmbWorkspaceTypes.setBorder(new JComboBox().getBorder());
}
Expand Down Expand Up @@ -469,7 +473,8 @@ private void setCmbWorkspaceTypeValues(String workspaceName, Workspace.Kind kind
}

private void updateWorkspaceModel(JPanel panel, String workspaceName, Workspace.Kind kind, String resource) {
Workspace workspace = null;
Workspace workspace = model.getWorkspaces().get(workspaceName);
boolean isOptional = workspace.isOptional();
if (kind == PVC) {
JComboBox cmbWorkspaceTypeValues = (JComboBox) Arrays.stream(panel.getComponents())
.filter(component -> "cmbWorkspaceTypeValues".equals(component.getName())).findFirst().get();
Expand All @@ -482,24 +487,28 @@ private void updateWorkspaceModel(JPanel panel, String workspaceName, Workspace.
String nameNewPVC = newPVCNameTextField.getText();
newPVCNameTextField.setBorder(nameNewPVC.isEmpty() ? RED_BORDER_SHOW_ERROR : NO_BORDER);

workspace = saveNewVolume(workspaceName, nameNewPVC, PVC, panel);
workspace = saveNewVolume(workspaceName, nameNewPVC, PVC, panel, isOptional);
} else if (valueSelected.equals(NEW_VCT_TEXT)) {
workspace = saveNewVolume(workspaceName, workspaceName + "-vct", PVC, panel, true);
workspace = saveNewVolume(workspaceName, workspaceName + "-vct", PVC, panel, true, isOptional);
} else {
workspace = new Workspace(workspaceName, kind, resource);
}
} else if (resource.isEmpty() && kind != EMPTYDIR) {
workspace = null;
} else if (resource.isEmpty() && !isOptional && kind != EMPTYDIR) {
workspace = new Workspace(workspaceName, null, null, isOptional);
} else {
workspace = new Workspace(workspaceName, kind, resource);
workspace = new Workspace(workspaceName, kind, resource, isOptional);
}

model.getWorkspaces().put(workspaceName, workspace);
fireStateChanged();
}

private boolean isValid(JComboBox component) {
if (!component.isVisible() || component.getSelectedItem().toString().isEmpty()) return false;
private boolean isValid(String workspaceName, JComboBox component) {
boolean isOptional = model.getWorkspaces().get(workspaceName).isOptional();
if (!component.isVisible()
|| (component.getSelectedItem().toString().isEmpty() && !isOptional))
return false;
if (isOptional) return true;
Workspace.Kind kind = (Workspace.Kind) component.getSelectedItem();
if (kind == PVC || kind == EMPTYDIR) return true;
List<String> resourcesByKind = getResources(kind);
Expand Down
Loading