Skip to content

Commit 8d112ee

Browse files
committed
db: move event name validation
The valid webhook event categories and their validation logic are needed outside of the API package. Export them from pkg/db so they can be reused by CLI commands. Signed-off-by: Robin Jarry <robin@jarry.cc>
1 parent 51b4ae3 commit 8d112ee

2 files changed

Lines changed: 30 additions & 22 deletions

File tree

‎pkg/api/webhooks.go‎

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"context"
1010
"fmt"
1111
"net/http"
12-
"strings"
1312
"time"
1413

1514
"github.com/danielgtaylor/huma/v2"
@@ -47,21 +46,6 @@ func registerWebhookRoutes(api huma.API, h *handler, prefix string, mw huma.Midd
4746
}, h.DeleteWebhook)
4847
}
4948

50-
var validEventCategories = map[string]bool{
51-
"cover-created": true,
52-
"patch-created": true,
53-
"patch-completed": true,
54-
"patch-state-changed": true,
55-
"patch-delegated": true,
56-
"patch-relation-changed": true,
57-
"check-created": true,
58-
"check-updated": true,
59-
"series-created": true,
60-
"series-completed": true,
61-
"cover-comment-created": true,
62-
"patch-comment-created": true,
63-
}
64-
6549
type ListWebhooksInput struct {
6650
PageParams
6751
ProjectID int `path:"projectID" doc:"Project ID"`
@@ -155,12 +139,8 @@ func (h *handler) CreateWebhook(
155139
if events == "" {
156140
events = "*"
157141
}
158-
if events != "*" {
159-
for _, e := range strings.Split(events, ",") {
160-
if !validEventCategories[strings.TrimSpace(e)] {
161-
return nil, huma.Error400BadRequest("Invalid event category.")
162-
}
163-
}
142+
if err := db.ValidateEvents(events); err != nil {
143+
return nil, huma.Error400BadRequest("Invalid event category.")
164144
}
165145

166146
active := true

‎pkg/db/webhook.go‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,37 @@
66
package db
77

88
import (
9+
"fmt"
910
"strings"
1011
)
1112

13+
var ValidEventCategories = map[string]bool{
14+
"cover-created": true,
15+
"patch-created": true,
16+
"patch-completed": true,
17+
"patch-state-changed": true,
18+
"patch-delegated": true,
19+
"patch-relation-changed": true,
20+
"check-created": true,
21+
"check-updated": true,
22+
"series-created": true,
23+
"series-completed": true,
24+
"cover-comment-created": true,
25+
"patch-comment-created": true,
26+
}
27+
28+
func ValidateEvents(events string) error {
29+
if events == "*" {
30+
return nil
31+
}
32+
for _, e := range strings.Split(events, ",") {
33+
if !ValidEventCategories[strings.TrimSpace(e)] {
34+
return fmt.Errorf("invalid event category: %q", strings.TrimSpace(e))
35+
}
36+
}
37+
return nil
38+
}
39+
1240
func (q *Queries) GetActiveWebhooks(projectID int) ([]Webhook, error) {
1341
var hooks []Webhook
1442
err := q.DB.NewSelect().Model(&hooks).

0 commit comments

Comments
 (0)