internal/control/events.go

f150dbe6d87e2cd72e724d229eb642d681a23c32
gitbay/internal/control/events.go

66 lines · 1658 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.edited",
35	"mr.merged",
36	"mr.milestoned",
37	"mr.retargeted",
38	"mr.reviewed",
39	"push",
40	"release.created",
41	"release.deleted",
42	"repo.archived",
43	"repo.imported",
44	"repo.unarchived",
45	"status",
46}
47
48// checkEventNames refuses a --events list naming something this forge
49// never emits. "*" is every kind. -1 means proceed.
50func checkEventNames(c *Ctx, events string) int {
51	if events == "*" {
52		return -1
53	}
54	for _, k := range strings.Split(events, ",") {
55		k = strings.TrimSpace(k)
56		if k == "" {
57			return c.fail(protocol.ExitUsage, "empty event name in --events")
58		}
59		if !slices.Contains(EventKinds, k) {
60			return c.fail(protocol.ExitUsage,
61				"%q is not an event this forge records; known events are: %s",
62				k, strings.Join(EventKinds, ", "))
63		}
64	}
65	return -1
66}