use of com.redhat.devtools.intellij.tektoncd.utils.model.actions.AddTriggerModel in project intellij-tekton by redhat-developer.
the class TriggerStep method setContent.
public void setContent(Map<String, String> triggerBindingTemplates) {
final int[] row = { 0 };
String infoText = "<html>The following list allows you to associate one or more bindings to the event-listener ";
infoText += "which will be created eventually. You are allowed to select none, one or many bindings. ";
infoText += "Only newly-created bindings that are selected will be actually pushed on cluster.</html>";
JLabel lblInfoText = new JLabel(infoText);
addComponent(lblInfoText, defaultLabelFont, new EmptyBorder(10, 0, 10, 0), new Dimension(594, 91), buildGridBagConstraints(0, row[0], 2, GridBagConstraints.NORTHWEST, null));
row[0] += 1;
JLabel lblExistingBindings = new JLabel("<html><span style=\\\"font-family:serif;font-size:10px;font-weight:bold;\\\">Select one or more TriggerBindings</span></html>");
addComponent(lblExistingBindings, null, MARGIN_BOTTOM_10, null, 0, row[0], GridBagConstraints.NORTHWEST);
row[0] += 1;
listBindingsAvailableOnCluster = new JBList();
listBindingsAvailableOnCluster.setLayoutOrientation(JList.VERTICAL);
listBindingsAvailableOnCluster.setBorder(new EmptyBorder(5, 2, 2, 2));
// show name as tooltip
listBindingsAvailableOnCluster.addMouseMotionListener(new MouseMotionAdapter() {
@Override
public void mouseMoved(MouseEvent e) {
JList sourceList = (JList) e.getSource();
int index = sourceList.locationToIndex(e.getPoint());
if (index > -1) {
sourceList.setToolTipText(sourceList.getModel().getElementAt(index).toString());
}
}
});
listBindingsAvailableOnCluster.setCellRenderer(new DefaultListCellRenderer() {
@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
JLabel label = (JLabel) super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
if (label.getText().endsWith(" NEW")) {
String beautifierText = label.getText().replace(" NEW", "");
label.setText(beautifierText);
label.setIcon(AllIcons.Actions.New);
}
return label;
}
});
fillAvailableBindingsList(null);
JScrollPane outerListScrollPane = new JBScrollPane();
outerListScrollPane.setViewportView(listBindingsAvailableOnCluster);
outerListScrollPane.setPreferredSize(new Dimension(200, 350));
// actions column
btnAdd = createActionButton(AllIcons.General.Add, AllIcons.General.InlineAdd, "Add new binding");
JButton btnRemove = createActionButton(AllIcons.General.Remove, IconLoader.getDisabledIcon(AllIcons.General.Remove), "Remove a newly-created selected binding");
btnRemove.setEnabled(false);
JButton btnFind = createActionButton(AllIcons.Actions.Find, IconLoader.getDisabledIcon(AllIcons.Actions.Find), "Show selected binding");
btnFind.setEnabled(false);
Box box = Box.createVerticalBox();
box.add(btnAdd);
box.add(btnRemove);
box.add(btnFind);
// right panel
JPanel rightPanel = new JPanel();
rightPanel.setBackground(backgroundTheme);
// description panel
JLabel lblDescriptionNewBinding = createDescriptionLabel("Add a new binding", AllIcons.General.Add, SwingConstants.LEFT, BORDER_LABEL_NAME);
lblDescriptionNewBinding.setCursor(new Cursor(Cursor.HAND_CURSOR));
JLabel lblDescriptionRemoveBinding = createDescriptionLabel("Remove a newly-created selected binding", AllIcons.General.Remove, SwingConstants.LEFT, BORDER_LABEL_NAME);
JLabel lblDescriptionFindBinding = createDescriptionLabel("Show the content of the selected binding", AllIcons.Actions.Find, SwingConstants.LEFT, BORDER_LABEL_NAME);
JLabel lblDescriptionGeneral1 = createDescriptionLabel("Select none or one or many bindings", null, -1, BORDER_LABEL_NAME);
JLabel lblDescriptionGeneral2 = createDescriptionLabel("Go Next", null, -1, BORDER_LABEL_NAME);
JPanel descriptionRightPanel = new JPanel(gridBagLayout);
descriptionRightPanel.setBorder(new EmptyBorder(60, 30, 0, 0));
descriptionRightPanel.setBackground(backgroundTheme);
descriptionRightPanel.add(lblDescriptionNewBinding, buildGridBagConstraints(0, 0, 1, GridBagConstraints.WEST, null));
descriptionRightPanel.add(lblDescriptionRemoveBinding, buildGridBagConstraints(0, 1, 1, GridBagConstraints.WEST, null));
descriptionRightPanel.add(lblDescriptionFindBinding, buildGridBagConstraints(0, 2, 1, GridBagConstraints.WEST, null));
descriptionRightPanel.add(lblDescriptionGeneral1, buildGridBagConstraints(0, 3, 1, GridBagConstraints.WEST, null));
descriptionRightPanel.add(lblDescriptionGeneral2, buildGridBagConstraints(0, 4, 1, GridBagConstraints.WEST, null));
// edit panel
editRightPanel = new JPanel(gridBagLayout);
editRightPanel.setBackground(backgroundTheme);
editRightPanel.setBorder(new EmptyBorder(0, 15, 0, 0));
editRightPanel.setVisible(false);
btnSave = new JLabel("Save");
Font font = btnSave.getFont();
btnSave.setFont(font.deriveFont(font.getStyle() | Font.BOLD));
btnSave.setForeground(BLUE);
btnSave.setCursor(new Cursor(Cursor.HAND_CURSOR));
JLabel btnClose = new JLabel("Close");
Font fontClose = btnClose.getFont();
btnClose.setFont(fontClose.deriveFont(fontClose.getStyle() | Font.BOLD));
btnClose.setForeground(BLUE);
btnClose.setCursor(new Cursor(Cursor.HAND_CURSOR));
// top right panel
JPanel topButtonsPanel = new JPanel(new FlowLayout());
topButtonsPanel.setBackground(backgroundTheme);
topButtonsPanel.add(btnSave);
topButtonsPanel.add(btnClose);
int internalRow = 0;
editRightPanel.add(topButtonsPanel, buildGridBagConstraints(1, internalRow, -1, GridBagConstraints.EAST, null));
internalRow++;
lblSelectTemplate = new JLabel("Select a template");
cmbPreMadeTriggerBindingTemplates = new ComboBox();
cmbPreMadeTriggerBindingTemplates.addItem("");
triggerBindingTemplates.keySet().stream().sorted().forEach(template -> cmbPreMadeTriggerBindingTemplates.addItem(template));
cmbPreMadeTriggerBindingTemplates.addItemListener(itemEvent -> {
if (itemEvent.getStateChange() == 1) {
// when cmbPreMadeTriggerBindingTemplates combo box value changes, the new value is saved and preview is updated
String templateSelected = (String) itemEvent.getItem();
if (!templateSelected.isEmpty()) {
String content = triggerBindingTemplates.get(templateSelected);
textAreaNewTriggerBinding.setText(content);
} else {
textAreaNewTriggerBinding.setText("");
}
fireStateChanged();
}
});
editRightPanel.add(lblSelectTemplate, buildGridBagConstraints(0, internalRow, 1, GridBagConstraints.WEST, null));
editRightPanel.add(cmbPreMadeTriggerBindingTemplates, buildGridBagConstraints(1, internalRow, 1, GridBagConstraints.EAST, null));
internalRow++;
lblErrorNewBinding = new JLabel();
lblErrorNewBinding.setVisible(false);
lblErrorNewBinding.setForeground(Color.RED);
editRightPanel.add(lblErrorNewBinding, buildGridBagConstraints(0, internalRow, 2, GridBagConstraints.WEST, null));
internalRow++;
textAreaNewTriggerBinding = new JTextArea(15, 35);
textAreaNewTriggerBinding.setEditable(true);
textAreaNewTriggerBinding.setText("");
textAreaNewTriggerBinding.setFont(defaultLabelFont);
textAreaNewTriggerBinding.setLineWrap(true);
scrollTriggerBindingAreaPane = new JBScrollPane(textAreaNewTriggerBinding);
Dimension sizeTextAreaNewTriggerBinding = textAreaNewTriggerBinding.getPreferredScrollableViewportSize();
scrollTriggerBindingAreaPane.setPreferredSize(new Dimension(sizeTextAreaNewTriggerBinding.width, sizeTextAreaNewTriggerBinding.height + 10));
editRightPanel.add(scrollTriggerBindingAreaPane, buildGridBagConstraints(0, internalRow, 2, GridBagConstraints.WEST, new Insets(10, 0, 0, 0)));
internalRow++;
rightPanel.add(descriptionRightPanel);
rightPanel.add(editRightPanel);
// listeners
editRightPanel.addComponentListener(new ComponentAdapter() {
public void componentShown(ComponentEvent e) {
descriptionRightPanel.setVisible(false);
}
public void componentHidden(ComponentEvent e) {
descriptionRightPanel.setVisible(true);
}
});
lblDescriptionNewBinding.addMouseListener(new MouseAdapter() {
Font original;
@Override
public void mouseClicked(MouseEvent e) {
openEditRightPanel();
}
@Override
public void mouseEntered(MouseEvent e) {
original = e.getComponent().getFont();
Map attributes = original.getAttributes();
attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
e.getComponent().setFont(original.deriveFont(attributes));
}
@Override
public void mouseExited(MouseEvent e) {
e.getComponent().setFont(original);
}
});
btnAdd.addActionListener(e -> {
openEditRightPanel();
});
btnClose.addMouseListener(new MouseAdapter() {
Font original;
@Override
public void mouseClicked(MouseEvent e) {
if (editRightPanel.isVisible()) {
btnAdd.setEnabled(true);
textAreaNewTriggerBinding.setText("");
lblErrorNewBinding.setVisible(false);
cmbPreMadeTriggerBindingTemplates.setSelectedIndex(0);
editRightPanel.setVisible(false);
}
}
@Override
public void mouseEntered(MouseEvent e) {
original = e.getComponent().getFont();
Map attributes = original.getAttributes();
attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
e.getComponent().setFont(original.deriveFont(attributes));
}
@Override
public void mouseExited(MouseEvent e) {
e.getComponent().setFont(original);
}
});
listBindingsAvailableOnCluster.addListSelectionListener(listSelectionEvent -> {
if (!listBindingsAvailableOnCluster.getSelectedValuesList().isEmpty()) {
if (listBindingsAvailableOnCluster.getSelectedValuesList().get(0).toString().endsWith(" NEW")) {
btnRemove.setEnabled(true);
} else {
btnRemove.setEnabled(false);
}
btnFind.setEnabled(true);
} else {
btnRemove.setEnabled(false);
btnFind.setEnabled(false);
}
});
btnFind.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String bindingSelected = listBindingsAvailableOnCluster.getSelectedValuesList().get(0).toString();
String bindingContent = ((AddTriggerModel) model).getBindingsAvailableOnCluster().get(bindingSelected);
btnSave.setVisible(false);
lblSelectTemplate.setVisible(false);
cmbPreMadeTriggerBindingTemplates.setVisible(false);
textAreaNewTriggerBinding.setText(bindingContent);
btnAdd.setEnabled(true);
if (!editRightPanel.isVisible()) {
editRightPanel.setVisible(true);
}
}
});
btnRemove.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {
String bindingSelected = listBindingsAvailableOnCluster.getSelectedValuesList().get(0).toString();
if (bindingSelected.endsWith(" NEW")) {
((AddTriggerModel) model).getBindingsAvailableOnCluster().remove(bindingSelected);
fillAvailableBindingsList(null);
}
}
});
btnSave.addMouseListener(new MouseAdapter() {
Font original;
@Override
public void mouseClicked(MouseEvent e) {
String configuration = textAreaNewTriggerBinding.getText();
String error = "";
if (!configuration.isEmpty()) {
TriggerBindingConfigurationModel bindingModel = new TriggerBindingConfigurationModel(configuration);
if (bindingModel.isValid()) {
if (((AddTriggerModel) model).getBindingsAvailableOnCluster().keySet().stream().map(binding -> binding.replace(" NEW", "")).anyMatch(binding -> binding.equalsIgnoreCase(bindingModel.getName()))) {
error = "<html>The name has already been used for another TriggerBinding. <br> Please change it and save again!!</html>";
} else {
String bindingNameWithNewSuffix = bindingModel.getName() + " NEW";
((AddTriggerModel) model).getBindingsAvailableOnCluster().put(bindingNameWithNewSuffix, textAreaNewTriggerBinding.getText());
fillAvailableBindingsList(bindingNameWithNewSuffix);
}
} else {
error = bindingModel.getErrorMessage();
}
} else {
error = "You cannot save an empty TriggerBinding.";
}
if (error.isEmpty()) {
editRightPanel.setVisible(false);
btnAdd.setEnabled(true);
return;
}
// if the new binding written by the user is not valid, we should show an error message with some info
lblErrorNewBinding.setText(error);
lblErrorNewBinding.setVisible(true);
}
@Override
public void mouseEntered(MouseEvent e) {
original = e.getComponent().getFont();
Map attributes = original.getAttributes();
attributes.put(TextAttribute.UNDERLINE, TextAttribute.UNDERLINE_ON);
e.getComponent().setFont(original.deriveFont(attributes));
}
@Override
public void mouseExited(MouseEvent e) {
e.getComponent().setFont(original);
}
});
JPanel wrapperContentPanel = new JPanel(new BorderLayout());
wrapperContentPanel.add(outerListScrollPane, BorderLayout.WEST);
wrapperContentPanel.add(box, BorderLayout.CENTER);
wrapperContentPanel.add(rightPanel, BorderLayout.EAST);
addComponent(wrapperContentPanel, editorFont, null, null, 0, row[0], GridBagConstraints.NORTHWEST);
row[0] += 1;
adjustContentPanel();
}
use of com.redhat.devtools.intellij.tektoncd.utils.model.actions.AddTriggerModel in project intellij-tekton by redhat-developer.
the class AddTriggerAction method actionPerformed.
@Override
public void actionPerformed(AnActionEvent anActionEvent, TreePath path, Object selected, Tkn tkncli) {
ActionMessage telemetry = TelemetryService.instance().action(NAME_PREFIX_CRUD + "add trigger");
ParentableNode element = getElement(selected);
String namespace = element.getNamespace();
ExecHelper.submit(() -> {
try {
String triggerVersion = tkncli.getTektonTriggersApiVersion();
Map<String, String> triggerBindingTemplates = SnippetHelper.getTriggerBindingTemplates(triggerVersion);
AddTriggerModel model = createModel(element, namespace, tkncli);
if (!model.isValid()) {
UIHelper.executeInUI(() -> Messages.showErrorDialog(model.getErrorMessage(), "Error"));
return;
}
String kind = (element instanceof PipelineNode) ? "Pipeline " : "Task ";
telemetry.property(PROP_RESOURCE_KIND, kind);
AddTriggerWizard addTriggerWizard = openTriggerBindingWizard(anActionEvent, element, triggerBindingTemplates, model, kind);
if (!addTriggerWizard.isOK()) {
telemetry.result(VALUE_ABORTED).send();
return;
}
createNewVolumes(model.getWorkspaces(), tkncli);
// take/create all triggerBindings
Map<String, String> triggerBindingsSelected = model.getBindingsSelectedByUser();
saveTriggerBindings(triggerBindingsSelected, namespace, tkncli);
// get all params from bindings
Set<String> paramsFromBindings = model.extractVariablesFromSelectedBindings();
// interpolate the variables correctly $variable to $(tt.params.variable)
normalizeVariablesInterpolation(model, paramsFromBindings);
// create the triggerTemplate
String randomString = Utils.getRandomString(6);
String triggerTemplateName = element.getName() + "-template-" + randomString;
ObjectNode run = createNode(element, model);
String triggerApiVersion = tkncli.getTektonTriggersApiVersion();
ObjectNode triggerTemplate = YAMLBuilder.createTriggerTemplate(triggerTemplateName, triggerApiVersion, new ArrayList<>(paramsFromBindings), Arrays.asList(run));
saveResource(YAMLHelper.JSONToYAML(triggerTemplate), namespace, KIND_TRIGGERTEMPLATES, tkncli);
notifySuccessOperation("TriggerTemplate " + triggerTemplateName);
// create the eventListener
String eventListenerName = element.getName() + "-listener-" + randomString;
// TODO we are using the default pipeline serviceAccount but we should allow users to select the one they prefer
ObjectNode eventListener = YAMLBuilder.createEventListener(eventListenerName, triggerApiVersion, "pipeline", triggerBindingsSelected.keySet().stream().map(binding -> binding.replace(" NEW", "")).collect(Collectors.toList()), triggerTemplateName);
saveResource(YAMLHelper.JSONToYAML(eventListener), namespace, KIND_EVENTLISTENERS, tkncli);
telemetry.result("bindings and resources created").send();
notifySuccessOperation("EventListener " + eventListenerName);
TreeHelper.refresh(getEventProject(anActionEvent), (ParentableNode) ((ParentableNode) element.getParent()).getParent());
} catch (IOException e) {
String errorMessage = "Failed to add a trigger to " + element.getName() + " in namespace " + namespace + "\n" + e.getLocalizedMessage();
telemetry.error(anonymizeResource(element.getName(), namespace, errorMessage)).send();
Notification notification = new Notification(NOTIFICATION_ID, "Error", errorMessage, NotificationType.ERROR);
Notifications.Bus.notify(notification);
logger.warn(errorMessage, e);
}
});
}
use of com.redhat.devtools.intellij.tektoncd.utils.model.actions.AddTriggerModel in project intellij-tekton by redhat-developer.
the class YAMLBuilderTest method checkTaskRunCreatedHasServiceAccount.
@Test
public void checkTaskRunCreatedHasServiceAccount() throws IOException {
String content = load("task1.yaml");
AddTriggerModel model = new AddTriggerModel(content, Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), Collections.emptyMap());
model.setServiceAccount("sa");
ObjectNode taskRunNode = YAMLBuilder.createTaskRun(model);
assertEquals(taskRunNode.get("apiVersion").asText(), "tekton.dev/v1beta1");
assertEquals(taskRunNode.get("kind").asText(), "TaskRun");
assertEquals(taskRunNode.get("metadata").get("generateName").asText(), "foo-");
assertEquals(taskRunNode.get("spec").get("taskRef").get("name").asText(), "foo");
assertEquals(taskRunNode.get("spec").get("serviceAccountName").asText(), "sa");
assertFalse(taskRunNode.get("spec").has("serviceAccountNames"));
assertFalse(taskRunNode.get("spec").has("params"));
assertFalse(taskRunNode.get("spec").has("resources"));
assertFalse(taskRunNode.get("spec").has("workspaces"));
}
use of com.redhat.devtools.intellij.tektoncd.utils.model.actions.AddTriggerModel in project intellij-tekton by redhat-developer.
the class YAMLBuilderTest method checkPipelineRunCreatedHasResource.
@Test
public void checkPipelineRunCreatedHasResource() throws IOException {
String content = load("pipeline4.yaml");
AddTriggerModel model = new AddTriggerModel(content, Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), Collections.emptyMap());
ObjectNode pipelineRunNode = YAMLBuilder.createPipelineRun(model);
assertEquals(pipelineRunNode.get("apiVersion").asText(), "tekton.dev/v1beta1");
assertEquals(pipelineRunNode.get("kind").asText(), "PipelineRun");
assertEquals(pipelineRunNode.get("metadata").get("generateName").asText(), "foo-");
assertEquals(pipelineRunNode.get("spec").get("pipelineRef").get("name").asText(), "foo");
assertFalse(pipelineRunNode.get("spec").has("serviceAccountName"));
assertFalse(pipelineRunNode.get("spec").has("serviceAccountNames"));
assertFalse(pipelineRunNode.get("spec").has("params"));
assertTrue(pipelineRunNode.get("spec").has("resources"));
assertEquals(pipelineRunNode.get("spec").get("resources").get(0).get("name").asText(), "resource1");
assertFalse(pipelineRunNode.get("spec").has("workspaces"));
}
use of com.redhat.devtools.intellij.tektoncd.utils.model.actions.AddTriggerModel in project intellij-tekton by redhat-developer.
the class YAMLBuilderTest method checkTriggerTemplateCreatedWithNoInputs.
// ///////////////////////////////////////////////////////
// / CREATE TRIGGERTEMPLATE
// ///////////////////////////////////////////////////////
@Test
public void checkTriggerTemplateCreatedWithNoInputs() throws IOException {
String content = load("task1.yaml");
AddTriggerModel model = new AddTriggerModel(content, Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), Collections.emptyMap());
ObjectNode taskRunNode = YAMLBuilder.createTaskRun(model);
ObjectNode triggerTemplateNode = YAMLBuilder.createTriggerTemplate("template", TRIGGER_BETA1_API_VERSION, Collections.emptyList(), Arrays.asList(taskRunNode));
assertEquals(triggerTemplateNode.get("apiVersion").asText(), TRIGGER_BETA1_API_VERSION);
assertEquals(triggerTemplateNode.get("kind").asText(), "TriggerTemplate");
assertEquals(triggerTemplateNode.get("metadata").get("name").asText(), "template");
assertFalse(triggerTemplateNode.get("spec").has("params"));
assertTrue(triggerTemplateNode.get("spec").has("resourcetemplates"));
}
Aggregations