Skip to content

Commit c81b26b

Browse files
authored
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
1 parent 2b4d43d commit c81b26b

File tree

8 files changed

+179
-495
lines changed

8 files changed

+179
-495
lines changed

modules/web/middleware/binding.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,16 @@ func Validate(errs binding.Errors, data map[string]interface{}, f Form, l transl
136136
case validation.ErrRegexPattern:
137137
data["ErrorMsg"] = trName + l.Tr("form.regex_pattern_error", errs[0].Message)
138138
default:
139-
data["ErrorMsg"] = l.Tr("form.unknown_error") + " " + errs[0].Classification
139+
msg := errs[0].Classification
140+
if msg != "" && errs[0].Message != "" {
141+
msg += ": "
142+
}
143+
144+
msg += errs[0].Message
145+
if msg == "" {
146+
msg = l.Tr("form.unknown_error")
147+
}
148+
data["ErrorMsg"] = trName + ": " + msg
140149
}
141150
return errs
142151
}

routers/api/v1/utils/hook.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515
"code.gitea.io/gitea/modules/json"
1616
api "code.gitea.io/gitea/modules/structs"
1717
"code.gitea.io/gitea/modules/util"
18-
"code.gitea.io/gitea/routers/utils"
1918
webhook_service "code.gitea.io/gitea/services/webhook"
2019
)
2120

@@ -141,14 +140,15 @@ func addHook(ctx *context.APIContext, form *api.CreateHookOption, orgID, repoID
141140
ctx.Error(http.StatusUnprocessableEntity, "", "Missing config option: channel")
142141
return nil, false
143142
}
143+
channel = strings.TrimSpace(channel)
144144

145-
if !utils.IsValidSlackChannel(channel) {
145+
if !webhook_service.IsValidSlackChannel(channel) {
146146
ctx.Error(http.StatusBadRequest, "", "Invalid slack channel name")
147147
return nil, false
148148
}
149149

150150
meta, err := json.Marshal(&webhook_service.SlackMeta{
151-
Channel: strings.TrimSpace(channel),
151+
Channel: channel,
152152
Username: form.Config["username"],
153153
IconURL: form.Config["icon_url"],
154154
Color: form.Config["color"],

routers/utils/utils.go

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,25 +20,6 @@ func RemoveUsernameParameterSuffix(name string) string {
2020
return name
2121
}
2222

23-
// IsValidSlackChannel validates a channel name conforms to what slack expects.
24-
// It makes sure a channel name cannot be empty and invalid ( only an # )
25-
func IsValidSlackChannel(channelName string) bool {
26-
switch len(strings.TrimSpace(channelName)) {
27-
case 0:
28-
return false
29-
case 1:
30-
// Keep default behaviour where a channel name is still
31-
// valid without an #
32-
// But if it contains only an #, it should be regarded as
33-
// invalid
34-
if channelName[0] == '#' {
35-
return false
36-
}
37-
}
38-
39-
return true
40-
}
41-
4223
// SanitizeFlashErrorString will sanitize a flash error string
4324
func SanitizeFlashErrorString(x string) string {
4425
return strings.ReplaceAll(html.EscapeString(x), "\n", "<br>")

routers/utils/utils_test.go

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,6 @@ func TestRemoveUsernameParameterSuffix(t *testing.T) {
1818
assert.Equal(t, "", RemoveUsernameParameterSuffix(""))
1919
}
2020

21-
func TestIsValidSlackChannel(t *testing.T) {
22-
tt := []struct {
23-
channelName string
24-
expected bool
25-
}{
26-
{"gitea", true},
27-
{" ", false},
28-
{"#", false},
29-
{"gitea ", true},
30-
{" gitea", true},
31-
}
32-
33-
for _, v := range tt {
34-
assert.Equal(t, v.expected, IsValidSlackChannel(v.channelName))
35-
}
36-
}
37-
3821
func TestIsExternalURL(t *testing.T) {
3922
setting.AppURL = "https://try.gitea.io/"
4023
type test struct {

0 commit comments

Comments
 (0)