diff --git a/bridge/slack/helpers.go b/bridge/slack/helpers.go index 4f58a46b..c8a06c5b 100644 --- a/bridge/slack/helpers.go +++ b/bridge/slack/helpers.go @@ -269,16 +269,18 @@ var ( topicOrPurposeRE = regexp.MustCompile(`(?s)(@.+) (cleared|set)(?: the)? channel (topic|purpose)(?:: (.*))?`) ) -func (b *Bslack) extractTopicOrPurpose(text string) (string, string) { +func extractTopicOrPurpose(text string) (string, string) { r := topicOrPurposeRE.FindStringSubmatch(text) - action, updateType, extracted := r[2], r[3], r[4] - switch action { - case "set": - return updateType, extracted - case "cleared": - return updateType, "" + if len(r) >= 5 { + action, updateType, extracted := r[2], r[3], r[4] + switch action { + case "set": + return updateType, extracted + case "cleared": + return updateType, "" + } } - return "", "" + return "unknown", "" } // @see https://api.slack.com/docs/message-formatting#linking_to_channels_and_users diff --git a/bridge/slack/helpers_test.go b/bridge/slack/helpers_test.go new file mode 100644 index 00000000..bca4860b --- /dev/null +++ b/bridge/slack/helpers_test.go @@ -0,0 +1,26 @@ +package bslack + +import ( + "testing" +) + +func TestExtractTopicOrPurpose(t *testing.T) { + tables := []struct{ + input string + changeType string + output string + }{ + {"@someone set channel topic: one liner", "topic", "one liner"}, + {"@someone set channel purpose: one liner", "purpose", "one liner"}, + {"@someone set channel topic: multi\nliner", "topic", "multi\nliner"}, + {"@someone cleared channel topic", "topic", ""}, + {"some unmatched message", "unknown", ""}, + } + + for _, table := range tables { + changeType, output := extractTopicOrPurpose(table.input) + if changeType != table.changeType || output != table.output { + t.Error() + } + } +}