Skip to content

Slack message empty text #31596

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 12 commits into from
Jul 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
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;
Expand All @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public void testToXContent() throws Exception {
}
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);
Expand Down Expand Up @@ -600,6 +600,22 @@ public void testUrlPathIsFiltered() throws Exception {
}
}

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);
Expand Down