mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-05-18 16:03:07 +00:00
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
This commit is contained in:
parent
2b4d43dd4d
commit
c81b26b0e5
8 changed files with 179 additions and 495 deletions
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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))
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue