diff --git a/src/main/java/com/adq/jenkins/xmljobtodsl/dsl/strategies/custom/DSLHudsonNotificationPropertyStrategy.java b/src/main/java/com/adq/jenkins/xmljobtodsl/dsl/strategies/custom/DSLHudsonNotificationPropertyStrategy.java new file mode 100644 index 0000000..ecda77a --- /dev/null +++ b/src/main/java/com/adq/jenkins/xmljobtodsl/dsl/strategies/custom/DSLHudsonNotificationPropertyStrategy.java @@ -0,0 +1,91 @@ +package com.adq.jenkins.xmljobtodsl.dsl.strategies.custom; + +import com.adq.jenkins.xmljobtodsl.dsl.strategies.DSLObjectStrategy; +import com.adq.jenkins.xmljobtodsl.parsers.PropertyDescriptor; + +import java.util.ArrayList; +import java.util.List; + +public class DSLHudsonNotificationPropertyStrategy extends DSLObjectStrategy { + private final String name; + private final String LATEST_UPDATED_PLUGIN_VERSION = "1.15"; + + public DSLHudsonNotificationPropertyStrategy(int tabs, PropertyDescriptor propertyDescriptor, String name) { + this(tabs, propertyDescriptor, name, true); + boolean doCastChildren = determineOutdatedOrNoPluginVersion(propertyDescriptor); + if(doCastChildren){ + castChildrenToLatestFormat(propertyDescriptor); + } + } + + public DSLHudsonNotificationPropertyStrategy(int tabs, PropertyDescriptor propertyDescriptor, String name, boolean shouldInitChildren) { + super(tabs, propertyDescriptor, name, shouldInitChildren); + this.name = name; + boolean doCastChildren = determineOutdatedOrNoPluginVersion(propertyDescriptor); + if(doCastChildren){ + castChildrenToLatestFormat(propertyDescriptor); + } + } + + private boolean determineOutdatedOrNoPluginVersion(PropertyDescriptor propertyDescriptor){ + if(propertyDescriptor.getAttributes() == null || propertyDescriptor.getAttributes().get("plugin") == null){ + return true; + } else { + // Assuming the plugin version looks like this: notification@1.15 + Float pluginVersion = Float.parseFloat(propertyDescriptor.getAttributes().get("plugin").split("@")[1]); + // Float.compare 0 means f1 is equal to f2 // negative value means f1 is less than f2 // positive value f1 is greater than f2 + int comparePluginToLatestVersion = Float.compare(pluginVersion, Float.parseFloat(this.LATEST_UPDATED_PLUGIN_VERSION)); + if(comparePluginToLatestVersion >= 0){ + return false; + } else { + return true; + } + } + } + + private void castChildrenToLatestFormat(PropertyDescriptor propertyDescriptor){ + // Just grab all the bottom level grandchildren at once + List children = getAllDescendants(propertyDescriptor); + for (PropertyDescriptor child : children) { + if(child.getAttributes() != null && child.getAttributes().get("ChangedName") == "true"){ + break; + } else if(child.getName().equals("url")){ + child.addAttribute("ChangedName", "true"); + child.changeName("urlInfo"); + List grandChildren = new ArrayList<>(); + PropertyDescriptor urlType = new PropertyDescriptor( + "urlType", + null, + "PUBLIC" + ); + + PropertyDescriptor urlOrId = new PropertyDescriptor( + "urlOrId", + null, + child.getValue() + ); + grandChildren.add(urlType); + grandChildren.add(urlOrId); + + child.replaceProperties(grandChildren); + initChildren(child.getParent()); + } + } + + } + + private ArrayList getAllDescendants(PropertyDescriptor propertyDescriptor){ + ArrayList viableChildren = new ArrayList<>(); + List children = propertyDescriptor.getProperties(); + + for(PropertyDescriptor child : children){ + if(child.getProperties() == null || child.getProperties().size() == 0){ + viableChildren.add(child); + } else { + viableChildren.addAll(getAllDescendants(child)); + } + } + + return viableChildren; + } +} diff --git a/src/main/resources/translator.properties b/src/main/resources/translator.properties index 6e656f6..4f0e6ce 100644 --- a/src/main/resources/translator.properties +++ b/src/main/resources/translator.properties @@ -175,7 +175,7 @@ com.chikli.hudson.plugin.naginator.NaginatorOptOutProperty.type = OBJECT optOut = optOut com.tikal.hudson.plugins.notification.HudsonNotificationProperty = hudsonNotificationProperty -com.tikal.hudson.plugins.notification.HudsonNotificationProperty.type = OBJECT +com.tikal.hudson.plugins.notification.HudsonNotificationProperty.type = com.adq.jenkins.xmljobtodsl.dsl.strategies.custom.DSLHudsonNotificationPropertyStrategy endpoints = endpoints endpoints.type = OBJECT @@ -189,7 +189,6 @@ urlType = urlType urlOrId = urlOrId urlOrId.type = com.adq.jenkins.xmljobtodsl.dsl.strategies.custom.DSLMandatoryStringStrategy - protocol = protocol format = format event = event