Skip to content

Commit 8ec33b7

Browse files
albendzhub-cap
authored andcommitted
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
1 parent 1f0421a commit 8ec33b7

File tree

2 files changed

+23
-2
lines changed
  • x-pack/plugin/watcher/src

2 files changed

+23
-2
lines changed

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/slack/message/SlackMessage.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import org.elasticsearch.common.xcontent.XContentParser;
1313
import org.elasticsearch.xpack.watcher.common.text.TextTemplate;
1414
import org.elasticsearch.xpack.watcher.common.text.TextTemplateEngine;
15+
import org.elasticsearch.common.Nullable;
1516

1617
import java.io.IOException;
1718
import java.util.ArrayList;
@@ -29,7 +30,11 @@ public class SlackMessage implements MessageElement {
2930
final String text;
3031
final Attachment[] attachments;
3132

32-
public SlackMessage(String from, String[] to, String icon, String text, Attachment[] attachments) {
33+
public SlackMessage(String from, String[] to, String icon, @Nullable String text, @Nullable Attachment[] attachments) {
34+
if(text == null && attachments == null) {
35+
throw new IllegalArgumentException("Both text and attachments cannot be null.");
36+
}
37+
3338
this.from = from;
3439
this.to = to;
3540
this.icon = icon;

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/slack/message/SlackMessageTests.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public void testToXContent() throws Exception {
4949
}
5050
String icon = randomBoolean() ? null : randomAlphaOfLength(10);
5151
String text = randomBoolean() ? null : randomAlphaOfLength(50);
52-
Attachment[] attachments = randomBoolean() ? null : new Attachment[randomIntBetween(0, 2)];
52+
Attachment[] attachments = (text != null && randomBoolean()) ? null : new Attachment[randomIntBetween(0, 2)];
5353
if (attachments != null) {
5454
for (int i = 0; i < attachments.length; i++) {
5555
String fallback = randomBoolean() ? null : randomAlphaOfLength(10);
@@ -600,6 +600,22 @@ public void testUrlPathIsFiltered() throws Exception {
600600
}
601601
}
602602

603+
public void testCanHaveNullText() throws Exception {
604+
SlackMessage slackMessage = new SlackMessage("from", new String[] {"to"}, "icon", null, new Attachment[1]);
605+
assertNull(slackMessage.getText());
606+
assertNotNull(slackMessage.getAttachments());
607+
}
608+
609+
public void testCanHaveNullAttachments() throws Exception {
610+
SlackMessage slackMessage = new SlackMessage("from", new String[] {"to"}, "icon", "text", null);
611+
assertNotNull(slackMessage.getText());
612+
assertNull(slackMessage.getAttachments());
613+
}
614+
615+
public void testCannotHaveNullAttachmentsAndNullText() throws Exception {
616+
expectThrows(IllegalArgumentException.class, () -> new SlackMessage("from", new String[]{"to"}, "icon", null, null));
617+
}
618+
603619
private static void writeFieldIfNotNull(XContentBuilder builder, String field, Object value) throws IOException {
604620
if (value != null) {
605621
builder.field(field, value);

0 commit comments

Comments
 (0)