internal/control/events.go
68 lines · 1695 bytes
1package control
2
3import (
4 "slices"
5 "strings"
6
7 "gitbay.org/gitbay/internal/protocol"
8)
9
10// EventKinds is every event this forge records, and so every value a
11// webhook's `--events` filter may name. It is the published list: the API
12// wiki page renders it, and TestEventKindsAreRecorded asserts the code
13// still emits exactly these, so the documentation cannot drift from the
14// server (#112).
15//
16// Not here, deliberately: repo.deleted. events.repo_id and
17// webhooks.repo_id both cascade from repos, so recording one would delete
18// it and every webhook that could have received it in the same statement.
19var EventKinds = []string{
20 "build.cancelled",
21 "build.failure",
22 "build.success",
23 "issue.assigned",
24 "issue.closed",
25 "issue.commented",
26 "issue.created",
27 "issue.edited",
28 "issue.labeled",
29 "issue.milestoned",
30 "issue.open",
31 "mr.closed",
32 "mr.commented",
33 "mr.created",
34 "mr.draft",
35 "mr.edited",
36 "mr.merged",
37 "mr.milestoned",
38 "mr.retargeted",
39 "mr.review_requested",
40 "mr.reviewed",
41 "push",
42 "release.created",
43 "release.deleted",
44 "repo.archived",
45 "repo.imported",
46 "repo.unarchived",
47 "status",
48}
49
50// checkEventNames refuses a --events list naming something this forge
51// never emits. "*" is every kind. -1 means proceed.
52func checkEventNames(c *Ctx, events string) int {
53 if events == "*" {
54 return -1
55 }
56 for _, k := range strings.Split(events, ",") {
57 k = strings.TrimSpace(k)
58 if k == "" {
59 return c.fail(protocol.ExitUsage, "empty event name in --events")
60 }
61 if !slices.Contains(EventKinds, k) {
62 return c.fail(protocol.ExitUsage,
63 "%q is not an event this forge records; known events are: %s",
64 k, strings.Join(EventKinds, ", "))
65 }
66 }
67 return -1
68}