From ec403a52591104197b8bd390365c9aac968a5f23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maximilian=20S=C3=B6lch?= Date: Fri, 22 Sep 2023 23:27:01 +0200 Subject: [PATCH] Fix the editing of the server url in the UI and additionally added a validation for the server url (#32) --- .../server/ServerNotificationRecipient.java | 41 ++++++++++++++++--- src/main/resources/atlassian-plugin.xml | 2 + src/main/resources/english.properties | 3 +- 3 files changed, 40 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/tum/in/www1/bamboo/server/ServerNotificationRecipient.java b/src/main/java/de/tum/in/www1/bamboo/server/ServerNotificationRecipient.java index 35341ff..1882077 100644 --- a/src/main/java/de/tum/in/www1/bamboo/server/ServerNotificationRecipient.java +++ b/src/main/java/de/tum/in/www1/bamboo/server/ServerNotificationRecipient.java @@ -1,5 +1,8 @@ package de.tum.in.www1.bamboo.server; +import java.net.MalformedURLException; +import java.net.URISyntaxException; +import java.net.URL; import java.util.List; import java.util.Map; import java.util.concurrent.ConcurrentHashMap; @@ -18,7 +21,10 @@ import com.atlassian.bamboo.plugin.descriptor.NotificationRecipientModuleDescriptor; import com.atlassian.bamboo.resultsummary.ResultsSummary; import com.atlassian.bamboo.template.TemplateRenderer; +import com.atlassian.bamboo.utils.error.ErrorCollection; +import com.atlassian.bamboo.utils.error.SimpleErrorCollection; import com.atlassian.bamboo.variable.CustomVariableContext; +import com.atlassian.sal.api.message.I18nResolver; import com.google.common.collect.Lists; import com.google.common.collect.Maps; @@ -29,6 +35,8 @@ public class ServerNotificationRecipient extends AbstractNotificationRecipient private String webhookUrl = null; + private I18nResolver i18n; + private TemplateRenderer templateRenderer; private ImmutablePlan plan; @@ -58,15 +66,34 @@ public class ServerNotificationRecipient extends AbstractNotificationRecipient @Override public void populate(@NotNull Map params) { - for (String next : params.keySet()) { - System.out.println("next = " + next); - } if (params.containsKey(WEBHOOK_URL)) { - int i = params.get(WEBHOOK_URL).length - 1; - this.webhookUrl = params.get(WEBHOOK_URL)[i]; + this.webhookUrl = this.getParam(WEBHOOK_URL, params); + } + } + + private boolean isValidURL(String url) { + try { + new URL(url).toURI(); + return true; + } + catch (MalformedURLException | URISyntaxException e) { + return false; } } + @NotNull + @Override + public ErrorCollection validate(@NotNull Map params) { + ErrorCollection errorCollection = new SimpleErrorCollection(); + + webhookUrl = this.getParam(WEBHOOK_URL, params); + if (webhookUrl != null && (webhookUrl.isEmpty() || !isValidURL(webhookUrl))) { + errorCollection.addError(WEBHOOK_URL, i18n.getText("server.webhookUrl.invalid")); + } + + return errorCollection; + } + @Override public void init(@Nullable String configurationData) { @@ -168,4 +195,8 @@ public void setTemplateRenderer(TemplateRenderer templateRenderer) { public void setCustomVariableContext(CustomVariableContext customVariableContext) { this.customVariableContext = customVariableContext; } + + public void setI18nResolver(I18nResolver i18n) { + this.i18n = i18n; + } } diff --git a/src/main/resources/atlassian-plugin.xml b/src/main/resources/atlassian-plugin.xml index 524b509..f811d60 100644 --- a/src/main/resources/atlassian-plugin.xml +++ b/src/main/resources/atlassian-plugin.xml @@ -20,6 +20,8 @@ com.atlassian.bamboo.variable.VariableDefinitionManager + + diff --git a/src/main/resources/english.properties b/src/main/resources/english.properties index b7b5c5d..880d8f4 100644 --- a/src/main/resources/english.properties +++ b/src/main/resources/english.properties @@ -1 +1,2 @@ -server.webhookUrl = Server URL \ No newline at end of file +server.webhookUrl = Server URL +server.webhookUrl.invalid = Please enter a valid URL \ No newline at end of file