Search in sources :

Example 1 with AddTriggerWizard

use of com.redhat.devtools.intellij.tektoncd.ui.wizard.addtrigger.AddTriggerWizard in project intellij-tekton by redhat-developer.

the class AddTriggerAction method openTriggerBindingWizard.

private AddTriggerWizard openTriggerBindingWizard(AnActionEvent anActionEvent, ParentableNode element, Map<String, String> triggerBindingTemplates, AddTriggerModel model, String kind) {
    return UIHelper.executeInUI(() -> {
        String titleDialog = "Add Trigger to " + kind + " " + element.getName();
        AddTriggerWizard wizard = new AddTriggerWizard(titleDialog, getEventProject(anActionEvent), model, triggerBindingTemplates);
        wizard.show();
        return wizard;
    });
}
Also used : AddTriggerWizard(com.redhat.devtools.intellij.tektoncd.ui.wizard.addtrigger.AddTriggerWizard)

Example 2 with AddTriggerWizard

use of com.redhat.devtools.intellij.tektoncd.ui.wizard.addtrigger.AddTriggerWizard 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);
        }
    });
}
Also used : AddTriggerWizard(com.redhat.devtools.intellij.tektoncd.ui.wizard.addtrigger.AddTriggerWizard) ObjectNode(com.fasterxml.jackson.databind.node.ObjectNode) ParentableNode(com.redhat.devtools.intellij.tektoncd.tree.ParentableNode) ActionMessage(com.redhat.devtools.intellij.telemetry.core.service.TelemetryMessageBuilder.ActionMessage) AddTriggerModel(com.redhat.devtools.intellij.tektoncd.utils.model.actions.AddTriggerModel) IOException(java.io.IOException) PipelineNode(com.redhat.devtools.intellij.tektoncd.tree.PipelineNode) Notification(com.intellij.notification.Notification)

Aggregations

AddTriggerWizard (com.redhat.devtools.intellij.tektoncd.ui.wizard.addtrigger.AddTriggerWizard)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)1 Notification (com.intellij.notification.Notification)1 ParentableNode (com.redhat.devtools.intellij.tektoncd.tree.ParentableNode)1 PipelineNode (com.redhat.devtools.intellij.tektoncd.tree.PipelineNode)1 AddTriggerModel (com.redhat.devtools.intellij.tektoncd.utils.model.actions.AddTriggerModel)1 ActionMessage (com.redhat.devtools.intellij.telemetry.core.service.TelemetryMessageBuilder.ActionMessage)1 IOException (java.io.IOException)1