Watcher: Slack message empty text (#31596)
Slack accepts an empty text or attachments, but not both. This commit ensures that both are not empty when creating a watch. Closes #30071 Replacing old pull request: #31288
This commit is contained in:
parent
1f0421aa69
commit
8ec33b742d
|
@ -12,6 +12,7 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
|
|||
import org.elasticsearch.common.xcontent.XContentParser;
|
||||
import org.elasticsearch.xpack.watcher.common.text.TextTemplate;
|
||||
import org.elasticsearch.xpack.watcher.common.text.TextTemplateEngine;
|
||||
import org.elasticsearch.common.Nullable;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
|
@ -29,7 +30,11 @@ public class SlackMessage implements MessageElement {
|
|||
final String text;
|
||||
final Attachment[] attachments;
|
||||
|
||||
public SlackMessage(String from, String[] to, String icon, String text, Attachment[] attachments) {
|
||||
public SlackMessage(String from, String[] to, String icon, @Nullable String text, @Nullable Attachment[] attachments) {
|
||||
if(text == null && attachments == null) {
|
||||
throw new IllegalArgumentException("Both text and attachments cannot be null.");
|
||||
}
|
||||
|
||||
this.from = from;
|
||||
this.to = to;
|
||||
this.icon = icon;
|
||||
|
|
|
@ -49,7 +49,7 @@ public class SlackMessageTests extends ESTestCase {
|
|||
}
|
||||
String icon = randomBoolean() ? null : randomAlphaOfLength(10);
|
||||
String text = randomBoolean() ? null : randomAlphaOfLength(50);
|
||||
Attachment[] attachments = randomBoolean() ? null : new Attachment[randomIntBetween(0, 2)];
|
||||
Attachment[] attachments = (text != null && randomBoolean()) ? null : new Attachment[randomIntBetween(0, 2)];
|
||||
if (attachments != null) {
|
||||
for (int i = 0; i < attachments.length; i++) {
|
||||
String fallback = randomBoolean() ? null : randomAlphaOfLength(10);
|
||||
|
@ -600,6 +600,22 @@ public class SlackMessageTests extends ESTestCase {
|
|||
}
|
||||
}
|
||||
|
||||
public void testCanHaveNullText() throws Exception {
|
||||
SlackMessage slackMessage = new SlackMessage("from", new String[] {"to"}, "icon", null, new Attachment[1]);
|
||||
assertNull(slackMessage.getText());
|
||||
assertNotNull(slackMessage.getAttachments());
|
||||
}
|
||||
|
||||
public void testCanHaveNullAttachments() throws Exception {
|
||||
SlackMessage slackMessage = new SlackMessage("from", new String[] {"to"}, "icon", "text", null);
|
||||
assertNotNull(slackMessage.getText());
|
||||
assertNull(slackMessage.getAttachments());
|
||||
}
|
||||
|
||||
public void testCannotHaveNullAttachmentsAndNullText() throws Exception {
|
||||
expectThrows(IllegalArgumentException.class, () -> new SlackMessage("from", new String[]{"to"}, "icon", null, null));
|
||||
}
|
||||
|
||||
private static void writeFieldIfNotNull(XContentBuilder builder, String field, Object value) throws IOException {
|
||||
if (value != null) {
|
||||
builder.field(field, value);
|
||||
|
|
Loading…
Reference in New Issue