diff options
author | oliverpool <3864879+oliverpool@users.noreply.github.com> | 2022-08-11 17:48:23 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-08-11 17:48:23 +0200 |
commit | c81b26b0e52f55fcdf94b50a14bca3dfc375e2a9 (patch) | |
tree | acccd4400733b5505a86ca668ceb4167119789fe /services/webhook | |
parent | 2b4d43dd4d7388ff73e99c143008ad4663142e61 (diff) | |
download | gitea-c81b26b0e52f55fcdf94b50a14bca3dfc375e2a9.tar.gz gitea-c81b26b0e52f55fcdf94b50a14bca3dfc375e2a9.zip |
refactor webhook *NewPost (#20729)
* refactor webhook *NewPost
* remove empty values
* always show errs.Message
* remove utils.IsValidSlackChannel
* move IsValidSlackChannel to services/webhook package
* binding: handle empty Message case
* make IsValidSlackChannel more strict
Diffstat (limited to 'services/webhook')
-rw-r--r-- | services/webhook/slack.go | 11 | ||||
-rw-r--r-- | services/webhook/slack_test.go | 19 |
2 files changed, 30 insertions, 0 deletions
diff --git a/services/webhook/slack.go b/services/webhook/slack.go index 11e1d3c081..e3d0d406de 100644 --- a/services/webhook/slack.go +++ b/services/webhook/slack.go @@ -7,6 +7,7 @@ package webhook import ( "errors" "fmt" + "regexp" "strings" webhook_model "code.gitea.io/gitea/models/webhook" @@ -286,3 +287,13 @@ func GetSlackPayload(p api.Payloader, event webhook_model.HookEventType, meta st return convertPayloader(s, p, event) } + +var slackChannel = regexp.MustCompile(`^#?[a-z0-9_-]{1,80}$`) + +// IsValidSlackChannel validates a channel name conforms to what slack expects: +// https://api.slack.com/methods/conversations.rename#naming +// Conversation names can only contain lowercase letters, numbers, hyphens, and underscores, and must be 80 characters or less. +// Gitea accepts if it starts with a #. +func IsValidSlackChannel(name string) bool { + return slackChannel.MatchString(name) +} diff --git a/services/webhook/slack_test.go b/services/webhook/slack_test.go index 8278afb69a..0f08785d25 100644 --- a/services/webhook/slack_test.go +++ b/services/webhook/slack_test.go @@ -170,3 +170,22 @@ func TestSlackJSONPayload(t *testing.T) { require.NoError(t, err) assert.NotEmpty(t, json) } + +func TestIsValidSlackChannel(t *testing.T) { + tt := []struct { + channelName string + expected bool + }{ + {"gitea", true}, + {"#gitea", true}, + {" ", false}, + {"#", false}, + {" #", false}, + {"gitea ", false}, + {" gitea", false}, + } + + for _, v := range tt { + assert.Equal(t, v.expected, IsValidSlackChannel(v.channelName)) + } +} |