Expose bamboo notification plugin to yaml
Asked by Mathieu MARACHE:

I'm trying to fond information on how to expose/read my plugin configuration to/from yaml files (bamboo-spec).

It this at all possible from a custom plugin ?


Answers:
author: Alexey Chystoprudov
body: Hello [@Mathieu MARACHE](/t5/user/viewprofilepage/user-id/2958016) ,

There's no public documentation with specs API support for plugin.

Below are few snippets which can give you some direction. Also you can find more sample at Bamboo source code which you can find at my.atlassian.com if have any Bamboo license

Exporter class

```
import com.atlassian.bamboo.notification.NotificationRecipientExporter;
import com.atlassian.bamboo.slack.yaml.YamlParser;
import com.atlassian.bamboo.specs.api.builders.AtlassianModule;
import com.atlassian.bamboo.specs.api.builders.notification.AnyNotificationRecipient;
import com.atlassian.bamboo.specs.api.builders.notification.NotificationRecipient;
import com.atlassian.bamboo.specs.api.model.notification.AnyNotificationRecipientProperties;
import com.atlassian.bamboo.specs.api.model.notification.NotificationRecipientProperties;
import com.atlassian.bamboo.specs.yaml.Node;
import com.atlassian.bamboo.util.Narrow;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.util.Objects;

public class SlackNotificationRecipientExporter implements NotificationRecipientExporter {
    @NotNull
    @Override
    public NotificationRecipient toSpecsEntity(@NotNull String recipientKey, @NotNull String recipientData) {
        return new AnyNotificationRecipient(new AtlassianModule(SlackNotificationRecipient.PLUGIN_KEY))
                .recipientString(recipientData);
    }

    @NotNull
    @Override
    public String importNotificationRecipient(NotificationRecipientProperties recipient) {
        if (recipient instanceof AnyNotificationRecipientProperties
                && Objects.equals(SlackNotificationRecipient.PLUGIN_KEY, recipient.getAtlassianPlugin().getCompleteModuleKey())) {
            return ((AnyNotificationRecipientProperties) recipient).getRecipientString();
        }
        throw new IllegalStateException("Don't know how to import notification type class: " + recipient.getClass().getName());
    }

    @Override
    public com.atlassian.bamboo.specs.api.builders.notification.NotificationRecipient fromYaml(Node node) {
        return YamlParser.parse(node);
    }

    @Nullable
    @Override
    public Node toYaml(NotificationRecipientProperties recipient) {
        final AnyNotificationRecipientProperties properties = Narrow.downTo(recipient,
                AnyNotificationRecipientProperties.class);
        if (properties != null) {
            final SlackConfig config = SlackConfig.parse(properties.getRecipientString());
            if (config != null) {
                return YamlParser.generate(config);
            }
        }
        return null;
    }
}
```

atlassian-plugin.xml

```
<notificationRecipient key="recipient.slack" name="Slack Recipient" class="com.atlassian.bamboo.slack.SlackNotificationRecipient" weight="10">
    ...
    <exporter class="com.atlassian.bamboo.slack.SlackNotificationRecipientExporter" />
    ...
</notificationRecipient>
```

SlackConfig

```
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.Nullable;

public class SlackConfig {
    private final String webhookUrl;
    private final String channel;
    private final String iconUrl;
    private final String botName;

    private SlackConfig(String webhookUrl, String channel, String iconUrl, String botName) {
        this.webhookUrl = webhookUrl;
        this.channel = channel;
        this.iconUrl = iconUrl;
        this.botName = botName;
    }

    /**
     * Parse config line.
     * @param configurationData store line
     * @return parsed config
     */
    @Nullable
    public static SlackConfig parse(@Nullable String configurationData) {
        String webhookUrl = "";
        String channel = "";
        String iconUrl = "";
        String botName = "";
        if (StringUtils.isNotBlank(configurationData)) {
            String delimiter = "\\|";

            String[] configValues = configurationData.split(delimiter);

            if (configValues.length > 0) {
                webhookUrl = configValues[0];
            }
            if (configValues.length > 1) {
                channel = configValues[1];
            }
            if (configValues.length > 2) {
                iconUrl = configValues[2];
            }
            if (configValues.length > 3) {
                botName = configValues[3];
            }
            return new SlackConfig(webhookUrl, channel, iconUrl, botName);
        } else {
            return null;
        }
    }

    public String getWebhookUrl() {
        return webhookUrl;
    }

    public String getChannel() {
        return channel;
    }

    public String getIconUrl() {
        return iconUrl;
    }

    public String getBotName() {
        return botName;
    }
}
```

YamlParser

```
import com.atlassian.bamboo.slack.SlackConfig;
import com.atlassian.bamboo.slack.SlackNotificationRecipient;
import com.atlassian.bamboo.specs.api.builders.AtlassianModule;
import com.atlassian.bamboo.specs.api.builders.notification.AnyNotificationRecipient;
import com.atlassian.bamboo.specs.api.builders.notification.NotificationRecipient;
import com.atlassian.bamboo.specs.api.validators.common.ValidationContext;
import com.atlassian.bamboo.specs.yaml.BambooYamlParserUtils;
import com.atlassian.bamboo.specs.yaml.MapNode;
import com.atlassian.bamboo.specs.yaml.Node;
import com.atlassian.bamboo.specs.yaml.StringNode;
import com.atlassian.bamboo.util.Narrow;
import java.util.LinkedHashMap;
import java.util.Map;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

public class YamlParser {
    private static final String SLACK_ATLASSIAN = "slack-atlassian";
    private static final String WEBHOOK_URL = "webhook-url";
    private static final String CHANNEL = "channel";
    private static final String ICON_URL = "icon-url";
    private static final String BOT_NAME = "bot-name";

    public static NotificationRecipient<?, ?> parse(@NotNull final Node recipientsNode) {
        MapNode mapNode = Narrow.downTo(recipientsNode, MapNode.class);
        if (mapNode != null && mapNode.getOptionalNode(SLACK_ATLASSIAN).isPresent()) {
            final MapNode slackNode = mapNode.getMap(SLACK_ATLASSIAN);
            String webhookUrl = slackNode.getString(WEBHOOK_URL).get();
            String channel = slackNode.getString(CHANNEL).get();
            String iconUrl = slackNode.getOptionalString(ICON_URL).map(StringNode::get).orElse("");
            String botName = slackNode.getOptionalString(BOT_NAME).map(StringNode::get).orElse("");
            return new AnyNotificationRecipient(new AtlassianModule(SlackNotificationRecipient.PLUGIN_KEY))
                    .recipientString(SlackNotificationRecipient.toConfigString(webhookUrl, channel, iconUrl, botName));
        } else {
            return null;
        }
    }

    @NotNull
    public static Node generate(@NotNull SlackConfig slackConfig) {
        final Map<String, Object> result = new LinkedHashMap<>();
        final Map<String, String> config = new LinkedHashMap<>();
        putIfNotEmpty(config, WEBHOOK_URL, slackConfig.getWebhookUrl());
        putIfNotEmpty(config, CHANNEL, slackConfig.getChannel());
        putIfNotEmpty(config, ICON_URL, slackConfig.getIconUrl());
        putIfNotEmpty(config, BOT_NAME, slackConfig.getBotName());
        result.put(SLACK_ATLASSIAN, config);
        return BambooYamlParserUtils.asNode(result, ValidationContext.of(SLACK_ATLASSIAN));
    }

    private static void putIfNotEmpty(Map<String, String> container, String key, String value) {
        if (StringUtils.isNotBlank(value)) {
            container.put(key, value);
        }
    }
}
```

pom.xml

```
<bamboo.version>7.1.0</bamboo.version>
```

comments: author: Mathieu MARACHE
body: Hi Alexey, thanks for such a detailed answer !


Tags: spec