Skip to content

Commit

Permalink
Fix the editing of the server url in the UI and additionally added a …
Browse files Browse the repository at this point in the history
…validation for the server url (#32)
  • Loading branch information
maximiliansoelch authored Sep 22, 2023
1 parent cc0e5b7 commit ec403a5
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 6 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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;

Expand All @@ -29,6 +35,8 @@ public class ServerNotificationRecipient extends AbstractNotificationRecipient

private String webhookUrl = null;

private I18nResolver i18n;

private TemplateRenderer templateRenderer;

private ImmutablePlan plan;
Expand Down Expand Up @@ -58,15 +66,34 @@ public class ServerNotificationRecipient extends AbstractNotificationRecipient

@Override
public void populate(@NotNull Map<String, String[]> 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<String, String[]> 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) {

Expand Down Expand Up @@ -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;
}
}
2 changes: 2 additions & 0 deletions src/main/resources/atlassian-plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
<interface>com.atlassian.bamboo.variable.VariableDefinitionManager</interface>
</component-import>

<component-import key="i18nResolver" interface="com.atlassian.sal.api.message.I18nResolver"/>

<component-import key="artifactLinkManager" interface="com.atlassian.bamboo.build.artifact.ArtifactLinkManager"/>

<component-import key="buildLoggerManager" interface="com.atlassian.bamboo.build.BuildLoggerManager"/>
Expand Down
3 changes: 2 additions & 1 deletion src/main/resources/english.properties
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
server.webhookUrl = Server URL
server.webhookUrl = Server URL
server.webhookUrl.invalid = Please enter a valid URL

0 comments on commit ec403a5

Please sign in to comment.