summaryrefslogtreecommitdiffstats
path: root/routers/utils
diff options
context:
space:
mode:
authorLanre Adelowo <adelowomailbox@gmail.com>2018-09-10 15:31:08 +0100
committertechknowlogick <techknowlogick@users.noreply.github.com>2018-09-10 10:31:08 -0400
commitbe48397945c77748d412baad3493bb5bd1c95e2a (patch)
treed53dc2e78052588b6985400b762e994227183e76 /routers/utils
parent6e03390aa8fa096206457962db1955d642860b57 (diff)
downloadgitea-be48397945c77748d412baad3493bb5bd1c95e2a.tar.gz
gitea-be48397945c77748d412baad3493bb5bd1c95e2a.zip
Slack webhook channel name cannot be empty or just contain an hashtag (#4786)
Diffstat (limited to 'routers/utils')
-rw-r--r--routers/utils/utils.go19
-rw-r--r--routers/utils/utils_test.go17
2 files changed, 36 insertions, 0 deletions
diff --git a/routers/utils/utils.go b/routers/utils/utils.go
index 6ead7d60e2..7c90fd7048 100644
--- a/routers/utils/utils.go
+++ b/routers/utils/utils.go
@@ -15,3 +15,22 @@ func RemoveUsernameParameterSuffix(name string) string {
}
return name
}
+
+// IsValidSlackChannel validates a channel name conforms to what slack expects.
+// It makes sure a channel name cannot be empty and invalid ( only an # )
+func IsValidSlackChannel(channelName string) bool {
+ switch len(strings.TrimSpace(channelName)) {
+ case 0:
+ return false
+ case 1:
+ // Keep default behaviour where a channel name is still
+ // valid without an #
+ // But if it contains only an #, it should be regarded as
+ // invalid
+ if channelName[0] == '#' {
+ return false
+ }
+ }
+
+ return true
+}
diff --git a/routers/utils/utils_test.go b/routers/utils/utils_test.go
index fb56ac85c2..d96e1d7d26 100644
--- a/routers/utils/utils_test.go
+++ b/routers/utils/utils_test.go
@@ -15,3 +15,20 @@ func TestRemoveUsernameParameterSuffix(t *testing.T) {
assert.Equal(t, "foobar", RemoveUsernameParameterSuffix("foobar"))
assert.Equal(t, "", RemoveUsernameParameterSuffix(""))
}
+
+func TestIsValidSlackChannel(t *testing.T) {
+ tt := []struct {
+ channelName string
+ expected bool
+ }{
+ {"gitea", true},
+ {" ", false},
+ {"#", false},
+ {"gitea ", true},
+ {" gitea", true},
+ }
+
+ for _, v := range tt {
+ assert.Equal(t, v.expected, IsValidSlackChannel(v.channelName))
+ }
+}