1// SPDX-License-Identifier: AGPL-3.0-or-later
2
3package usecase
4
5import (
6 "context"
7 "errors"
8 "fmt"
9 "strconv"
10 "strings"
11 "sync"
12 "time"
13
14 "github.com/bnema/zerowrap"
15
16 "bnema.dev/ermite/internal/domain"
17 "bnema.dev/ermite/internal/ports"
18)
19
20// DefaultEventBuffer is the number of events the Events use case remembers.
21const DefaultEventBuffer = 1024
22
23// Limits of the watchers.
24const (
25 // MaxWatchTopics is the most topics one watch may follow.
26 MaxWatchTopics = 64
27 // MaxWatchers is the most watches in progress at once.
28 MaxWatchers = 256
29 // MaxWatchersPerClient is the most watches in progress for one client
30 // address (see ports.WatchParams.Client).
31 MaxWatchersPerClient = 16
32 // DefaultRevalidateEvery is how long a watcher with a stored token goes
33 // without asking the token store whether the token still exists.
34 DefaultRevalidateEvery = 5 * time.Second
35)
36
37// Topics a watcher may follow.
38const (
39 // TopicHome follows every repository the caller may see.
40 TopicHome = "home"
41 // TopicOwner follows the merge requests and issues of every repository,
42 // for the owner only (the inbox). Anyone else is told it is an unknown
43 // topic.
44 TopicOwner = "owner"
45 // topicRepoPrefix starts the topic of one repository: "repo:<name>".
46 topicRepoPrefix = "repo:"
47)
48
49// bufferedEvent is an event with its sequence number in the buffer, and what
50// was true of its repository when it was buffered. Access to an event is
51// decided from that, and from the repository as it is when the event is
52// delivered: a repository made public later does not reveal what happened
53// while it was private, and a repository deleted and created again under the
54// same name does not inherit the events of the old one.
55type bufferedEvent struct {
56 seq uint64
57 ev domain.Event
58
59 // known is false when the repository did not exist any more when the
60 // event was buffered: nobody is told about it.
61 known bool
62 // vis is the visibility of the repository at that time.
63 vis domain.Visibility
64 // created is the creation time of that repository.
65 created time.Time
66}
67
68// Events keeps the last events of the bus in a ring buffer, numbers them, and
69// lets callers watch them: live, or replayed from the id of the last event
70// they saw. Events of a private repository only reach callers that may read
71// it, checked again for every event.
72//
73// Event ids are opaque to clients: "<boot>-<seq>", where boot identifies this
74// run of the server, so that an id from before a restart is unknown rather
75// than taken for a newer event.
76//
77// The goroutine that feeds the buffer uses the logger of the context given to
78// NewEvents and lives until Close.
79type Events struct {
80 repos *Repos
81 auth ports.Revalidator
82 clock ports.Clock
83 boot string
84 size int
85 every time.Duration // see EventsConfig.RevalidateEvery
86
87 stop context.CancelFunc
88 done chan struct{}
89
90 mu sync.Mutex
91 ring []bufferedEvent // capacity size, oldest first
92 nextSeq uint64 // sequence number of the next event, from 1
93 changed chan struct{} // closed and replaced on every append and on close
94 closed bool
95 watches int
96 clients map[string]int // watches in progress by client
97}
98
99var _ ports.EventService = (*Events)(nil)
100
101// EventsConfig configures Events.
102type EventsConfig struct {
103 // Size is the number of events remembered; DefaultEventBuffer when not
104 // positive.
105 Size int
106 // Auth confirms that the token of a watcher still exists. Required.
107 Auth ports.Revalidator
108 // RevalidateEvery is how long the answer of Auth is kept for a watcher;
109 // DefaultRevalidateEvery when zero, and negative to ask for every event.
110 RevalidateEvery time.Duration
111}
112
113// NewEvents subscribes to the bus and starts remembering its events. ctx
114// carries the logger; its cancellation is not followed, call Close.
115func NewEvents(ctx context.Context, repos *Repos, bus ports.EventBus, clock ports.Clock, cfg EventsConfig) *Events {
116 size := cfg.Size
117 if size <= 0 {
118 size = DefaultEventBuffer
119 }
120 every := cfg.RevalidateEvery
121 if every == 0 {
122 every = DefaultRevalidateEvery
123 }
124 run, stop := context.WithCancel(context.WithoutCancel(ctx))
125 e := &Events{
126 repos: repos,
127 auth: cfg.Auth,
128 clock: clock,
129 every: every,
130 clients: make(map[string]int),
131 boot: strconv.FormatInt(clock.Now().UnixMilli(), 36),
132 size: size,
133 stop: stop,
134 done: make(chan struct{}),
135 nextSeq: 1,
136 changed: make(chan struct{}),
137 }
138 sub := bus.Subscribe(run) // before returning: nothing published after is missed
139 go e.feed(run, sub)
140 return e
141}
142
143// feed appends the events of the bus until it stops.
144func (e *Events) feed(ctx context.Context, sub <-chan domain.Event) {
145 defer close(e.done)
146 defer e.shutdown()
147 for {
148 select {
149 case ev, ok := <-sub:
150 if !ok {
151 return
152 }
153 e.append(ctx, ev)
154 case <-ctx.Done():
155 return
156 }
157 }
158}
159
160// snapshot records the visibility and the generation of the repository an
161// event is about. Every event of the repository carries them.
162func (e *Events) snapshot(ctx context.Context, ev domain.Event) bufferedEvent {
163 switch ev := ev.(type) {
164 case domain.RepoCreated:
165 return bufferedEvent{known: true, vis: ev.Visibility, created: ev.At}
166 case domain.RepoUpdated:
167 return bufferedEvent{known: true, vis: ev.Visibility, created: ev.CreatedAt}
168 case domain.RepoDeleted:
169 return bufferedEvent{known: true, vis: ev.Visibility, created: ev.CreatedAt}
170 case domain.MirrorFailed:
171 return repoSnapshot(ev.Visibility, ev.CreatedAt)
172 case domain.MirrorRecovered:
173 return repoSnapshot(ev.Visibility, ev.CreatedAt)
174 case domain.PushEvent:
175 if ev.CreatedAt.IsZero() {
176 return bufferedEvent{} // repository unknown at publish time: nobody is told
177 }
178 return bufferedEvent{known: true, vis: ev.Visibility, created: ev.CreatedAt}
179 case domain.MergeRequestOpened:
180 return repoSnapshot(ev.Visibility, ev.CreatedAt)
181 case domain.MergeRequestMerged:
182 return repoSnapshot(ev.Visibility, ev.CreatedAt)
183 case domain.MergeRequestClosed:
184 return repoSnapshot(ev.Visibility, ev.CreatedAt)
185 case domain.MergeRequestReady:
186 return repoSnapshot(ev.Visibility, ev.CreatedAt)
187 case domain.CommentAdded:
188 return repoSnapshot(ev.Visibility, ev.CreatedAt)
189 case domain.IssueOpened:
190 return repoSnapshot(ev.Visibility, ev.CreatedAt)
191 case domain.IssueClosed:
192 return repoSnapshot(ev.Visibility, ev.CreatedAt)
193 case domain.IssueReopened:
194 return repoSnapshot(ev.Visibility, ev.CreatedAt)
195 case domain.IssueCommented:
196 return repoSnapshot(ev.Visibility, ev.CreatedAt)
197 case domain.IssueSubmitted, domain.IssuePendingRemoved:
198 return bufferedEvent{known: true} // owner topic only: no repository snapshot
199 case domain.CommentEdited:
200 return repoSnapshot(ev.Visibility, ev.CreatedAt)
201 case domain.CommentDeleted:
202 return repoSnapshot(ev.Visibility, ev.CreatedAt)
203 case domain.CommentResolved:
204 return repoSnapshot(ev.Visibility, ev.CreatedAt)
205 case domain.MergeRequestMergeability:
206 return repoSnapshot(ev.Visibility, ev.CreatedAt)
207 case domain.RunUpdated:
208 return repoSnapshot(ev.Visibility, ev.CreatedAt)
209 case domain.JobUpdated:
210 return repoSnapshot(ev.Visibility, ev.CreatedAt)
211 }
212 return bufferedEvent{known: true}
213}
214
215// repoSnapshot is the snapshot of an event that carries the visibility and the
216// creation time of its repository; a zero creation time means the repository
217// was unknown when it was published, and nobody is told.
218func repoSnapshot(vis domain.Visibility, created time.Time) bufferedEvent {
219 if created.IsZero() {
220 return bufferedEvent{}
221 }
222 return bufferedEvent{known: true, vis: vis, created: created}
223}
224
225func (e *Events) append(ctx context.Context, ev domain.Event) {
226 be := e.snapshot(ctx, ev) // outside the lock: it reads the store
227 e.mu.Lock()
228 defer e.mu.Unlock()
229 if len(e.ring) == e.size {
230 copy(e.ring, e.ring[1:])
231 e.ring = e.ring[:e.size-1]
232 }
233 be.seq, be.ev = e.nextSeq, ev
234 e.ring = append(e.ring, be)
235 e.nextSeq++
236 e.wake()
237}
238
239// wake releases the watchers waiting for news. The caller holds mu.
240func (e *Events) wake() {
241 close(e.changed)
242 e.changed = make(chan struct{})
243}
244
245func (e *Events) shutdown() {
246 e.mu.Lock()
247 defer e.mu.Unlock()
248 if !e.closed {
249 e.closed = true
250 e.wake()
251 }
252}
253
254// Close ends every watch cleanly, stops feeding and waits for the feeder,
255// until ctx ends. It is safe to call more than once.
256func (e *Events) Close(ctx context.Context) {
257 e.stop()
258 select {
259 case <-e.done:
260 case <-ctx.Done():
261 }
262}
263
264// Watchers returns the number of watches in progress.
265func (e *Events) Watchers() int {
266 e.mu.Lock()
267 defer e.mu.Unlock()
268 return e.watches
269}
270
271func (e *Events) id(seq uint64) string { return e.boot + "-" + strconv.FormatUint(seq, 10) }
272
273// parseID returns the sequence number of an id of this run.
274func (e *Events) parseID(id string) (uint64, bool) {
275 boot, seq, ok := strings.Cut(id, "-")
276 if !ok || boot != e.boot {
277 return 0, false
278 }
279 n, err := strconv.ParseUint(seq, 10, 64)
280 return n, err == nil && n > 0
281}
282
283// watch is the state of one Watch call.
284type watch struct {
285 p domain.Principal
286 home bool
287 owner bool // the owner topic: merge request, issue, repo update and deletion events of every repository
288 repos map[domain.RepoName]bool
289
290 checked time.Time // when the token store last confirmed the principal
291}
292
293func (w *watch) wants(name domain.RepoName) bool { return w.home || w.repos[name] }
294
295// wantsEvent is wants for an event: the owner topic also follows the events
296// about merge requests and issues of every repository.
297func (w *watch) wantsEvent(name domain.RepoName, ev domain.Event) bool {
298 return w.wants(name) || (w.owner && (isItemEvent(ev) || isRepoLifecycleEvent(ev)))
299}
300
301// parseTopics validates the topics of a watch. A repository topic needs a
302// repository the principal may read (else domain.ErrNotFound); an unknown
303// topic, or more than MaxWatchTopics, is domain.ErrInvalid.
304func (e *Events) parseTopics(ctx context.Context, p domain.Principal, topics []string) (*watch, error) {
305 if len(topics) > MaxWatchTopics {
306 return nil, fmt.Errorf("%w: %d topics, at most %d", domain.ErrInvalid, len(topics), MaxWatchTopics)
307 }
308 w := &watch{p: p, repos: make(map[domain.RepoName]bool), checked: time.Now()}
309 if len(topics) == 0 {
310 w.home = true
311 }
312 for _, t := range topics {
313 if t == TopicHome {
314 w.home = true
315 continue
316 }
317 if t == TopicOwner && p.HasOwnerRights() {
318 w.owner = true
319 continue
320 }
321 raw, ok := strings.CutPrefix(t, topicRepoPrefix)
322 if !ok {
323 // Includes "owner" for anyone but the owner: the same error as
324 // any unknown topic.
325 return nil, fmt.Errorf("%w: unknown topic %q (want %q or \"repo:<name>\")", domain.ErrInvalid, t, TopicHome)
326 }
327 name, err := domain.ParseRepoName(raw)
328 if err != nil {
329 return nil, err
330 }
331 if _, err := e.repos.Get(ctx, p, name); err != nil {
332 return nil, err
333 }
334 w.repos[name] = true
335 }
336 return w, nil
337}
338
339// Watch calls send for the events the principal may see until ctx ends, the
340// buffer closes (both return nil) or send fails (its error is returned).
341//
342// A principal with a stored token is checked again for every event, and every
343// few seconds when idle: when the token expired or was revoked the watch ends
344// with domain.ErrUnauthenticated and nothing more is sent. More watches than
345// MaxWatchers, or than MaxWatchersPerClient for one Client, give
346// domain.ErrResourceExhausted.
347//
348// Without ResumeAfter only new events are sent. With it, the buffered events
349// after that id are sent first, then new ones. When the id is no longer
350// buffered, or was not issued by this run, a gap comes first and the oldest
351// buffered event follows. A watcher that falls more than the buffer behind
352// gets a gap too, and carries on with the oldest buffered event.
353func (e *Events) Watch(ctx context.Context, p domain.Principal, params ports.WatchParams, send func(ports.WatchEvent) error) error {
354 w, err := e.parseTopics(ctx, p, params.Topics)
355 if err != nil {
356 return err
357 }
358
359 e.mu.Lock()
360 next := e.nextSeq // only new events
361 gap := false
362 if params.ResumeAfter != "" {
363 seq, ok := e.parseID(params.ResumeAfter)
364 oldest := e.nextSeq - uint64(len(e.ring)) // first buffered sequence, nextSeq when empty
365 switch {
366 case ok && seq >= oldest-1 && seq < e.nextSeq:
367 next = seq + 1
368 default:
369 next, gap = oldest, true
370 }
371 }
372 if e.watches >= MaxWatchers || (params.Client != "" && e.clients[params.Client] >= MaxWatchersPerClient) {
373 e.mu.Unlock()
374 return fmt.Errorf("%w: too many watchers", domain.ErrResourceExhausted)
375 }
376 e.watches++
377 if params.Client != "" {
378 e.clients[params.Client]++
379 }
380 e.mu.Unlock()
381 defer func() {
382 e.mu.Lock()
383 e.watches--
384 if params.Client != "" {
385 if e.clients[params.Client]--; e.clients[params.Client] == 0 {
386 delete(e.clients, params.Client)
387 }
388 }
389 e.mu.Unlock()
390 }()
391
392 if params.Ready != nil {
393 params.Ready()
394 }
395 log := zerowrap.FromCtx(ctx)
396 log.Debug().Strs("topics", params.Topics).Bool("resume", params.ResumeAfter != "").Bool("gap", gap).Msg("watch started")
397 if gap {
398 if err := send(ports.WatchEvent{Gap: true}); err != nil {
399 return err
400 }
401 }
402 for {
403 if err := e.stillAllowed(ctx, w); err != nil {
404 return err
405 }
406 batch, changed, oldest, lost, closed := e.since(next)
407 if lost {
408 if err := send(ports.WatchEvent{Gap: true}); err != nil {
409 return err
410 }
411 next = oldest
412 continue
413 }
414 for _, be := range batch {
415 if err := e.deliver(ctx, w, be, send); err != nil {
416 return err
417 }
418 next = be.seq + 1
419 }
420 if len(batch) > 0 {
421 continue
422 }
423 if closed {
424 return nil
425 }
426 e.wait(ctx, w, changed)
427 if ctx.Err() != nil {
428 return nil
429 }
430 }
431}
432
433// wait blocks until more events arrive or ctx ends. A watcher with a stored
434// token also wakes up every few seconds, so that a revocation ends the watch
435// of a quiet forge too (the caller checks the token on the next turn).
436func (e *Events) wait(ctx context.Context, w *watch, changed <-chan struct{}) {
437 var idle <-chan time.Time
438 if w.p.TokenID() != "" {
439 t := time.NewTimer(e.idleCheck())
440 defer t.Stop()
441 idle = t.C
442 }
443 select {
444 case <-changed:
445 case <-idle:
446 case <-ctx.Done():
447 }
448}
449
450// idleCheck is how long a watcher with a stored token waits for news before
451// checking its token anyway.
452func (e *Events) idleCheck() time.Duration {
453 if e.every <= 0 {
454 return DefaultRevalidateEvery
455 }
456 return min(e.every, DefaultRevalidateEvery)
457}
458
459// stillAllowed checks that the holder of a stored token may still watch: the
460// token has not expired and, at most every few seconds, still exists. Root and
461// anonymous callers have nothing to lose. The owner topic needs nothing more:
462// a stored token with owner rights loses them only by ceasing to exist, which
463// is checked here, and the owner's web session, which this use case cannot see,
464// is checked by the web adapter before it sends anything.
465func (e *Events) stillAllowed(ctx context.Context, w *watch) error {
466 if w.p.TokenID() == "" {
467 return nil
468 }
469 if exp := w.p.ExpiresAt(); !exp.IsZero() && !e.clock.Now().Before(exp) {
470 return domain.ErrUnauthenticated
471 }
472 if e.every > 0 && time.Since(w.checked) < e.every {
473 return nil
474 }
475 if err := e.auth.Revalidate(ctx, w.p); err != nil {
476 if ctx.Err() != nil {
477 return nil
478 }
479 return err
480 }
481 w.checked = time.Now()
482 return nil
483}
484
485// since returns the buffered events from sequence next on, and the channel
486// that closes when more arrive. lost is true when next fell out of the buffer;
487// the caller then restarts from oldest, the first buffered sequence. The
488// returned slice is a copy.
489func (e *Events) since(next uint64) (batch []bufferedEvent, changed <-chan struct{}, oldest uint64, lost, closed bool) {
490 e.mu.Lock()
491 defer e.mu.Unlock()
492 oldest = e.nextSeq - uint64(len(e.ring))
493 if next < oldest {
494 return nil, e.changed, oldest, true, e.closed
495 }
496 batch = append(batch, e.ring[next-oldest:]...)
497 return batch, e.changed, oldest, false, e.closed
498}
499
500// deliver sends the event if the watcher follows its topic and may see it.
501func (e *Events) deliver(ctx context.Context, w *watch, be bufferedEvent, send func(ports.WatchEvent) error) error {
502 if err := e.stillAllowed(ctx, w); err != nil {
503 return err
504 }
505 out := ports.WatchEvent{ID: e.id(be.seq), Event: be.ev}
506 switch ev := be.ev.(type) {
507 case domain.EventGap:
508 // The feeder lost events: tell every watcher.
509 return send(ports.WatchEvent{Gap: true})
510 case domain.RepoDeleted:
511 // The repository is gone: use the visibility it had.
512 if !w.wantsEvent(ev.Repo, ev) || !canReadAs(w.p, ev.Repo, ev.Visibility) {
513 return nil
514 }
515 return send(out)
516 case domain.IssueSubmitted:
517 return e.deliverOwnerOnly(ctx, w, ev.Repo, out, send)
518 case domain.IssuePendingRemoved:
519 return e.deliverOwnerOnly(ctx, w, ev.Repo, out, send)
520 case domain.RepoCreated:
521 return e.deliverRepo(ctx, w, ev.Repo, true, be, out, send)
522 case domain.RepoUpdated:
523 return e.deliverRepo(ctx, w, ev.Repo, true, be, out, send)
524 case domain.MirrorFailed:
525 return e.deliverRepo(ctx, w, ev.Repo, false, be, out, send)
526 case domain.MirrorRecovered:
527 return e.deliverRepo(ctx, w, ev.Repo, false, be, out, send)
528 case domain.PushEvent:
529 return e.deliverRepo(ctx, w, ev.Repo, false, be, out, send)
530 case domain.MergeRequestOpened:
531 return e.deliverRepo(ctx, w, ev.MergeRequest.Repo, false, be, out, send)
532 case domain.MergeRequestMerged:
533 return e.deliverRepo(ctx, w, ev.MergeRequest.Repo, false, be, out, send)
534 case domain.MergeRequestClosed:
535 return e.deliverRepo(ctx, w, ev.MergeRequest.Repo, false, be, out, send)
536 case domain.MergeRequestReady:
537 return e.deliverRepo(ctx, w, ev.MergeRequest.Repo, false, be, out, send)
538 case domain.CommentAdded:
539 return e.deliverRepo(ctx, w, ev.Repo, false, be, out, send)
540 case domain.IssueOpened:
541 return e.deliverRepo(ctx, w, ev.Issue.Repo, false, be, out, send)
542 case domain.IssueClosed:
543 return e.deliverRepo(ctx, w, ev.Issue.Repo, false, be, out, send)
544 case domain.IssueReopened:
545 return e.deliverRepo(ctx, w, ev.Issue.Repo, false, be, out, send)
546 case domain.IssueCommented:
547 return e.deliverRepo(ctx, w, ev.Repo, false, be, out, send)
548 case domain.CommentEdited:
549 return e.deliverRepo(ctx, w, ev.Repo, false, be, out, send)
550 case domain.CommentDeleted:
551 return e.deliverRepo(ctx, w, ev.Repo, false, be, out, send)
552 case domain.CommentResolved:
553 return e.deliverRepo(ctx, w, ev.Repo, false, be, out, send)
554 case domain.MergeRequestMergeability:
555 return e.deliverRepo(ctx, w, ev.Repo, false, be, out, send)
556 case domain.RunUpdated:
557 // Home shows the CI state of each repository.
558 return e.deliverRepo(ctx, w, ev.Run.Repo, false, be, out, send)
559 case domain.JobUpdated:
560 // Home gets a job when it ends only: a failed job turns the state to
561 // failing before its run ends. Its progress stays on the topic of its
562 // repository.
563 if !w.repos[ev.Job.Repo] && ev.Job.Phase != domain.CIPhaseEnded {
564 return nil
565 }
566 return e.deliverRepo(ctx, w, ev.Job.Repo, false, be, out, send)
567 }
568 return nil // an event this version does not stream
569}
570
571// deliverOwnerOnly sends an owner-topic event: never home, never a repository
572// topic, not even to an owner who watches the repository. No content travels.
573func (e *Events) deliverOwnerOnly(ctx context.Context, w *watch, name domain.RepoName, out ports.WatchEvent, send func(ports.WatchEvent) error) error {
574 if !w.owner {
575 return nil
576 }
577 repo, err := lookupRepo(ctx, e.repos.meta, name)
578 switch {
579 case errors.Is(err, domain.ErrNotFound), ctx.Err() != nil:
580 return nil
581 case err != nil:
582 return fmt.Errorf("check access to %q: %w", name, err)
583 }
584 if !repo.Settings.IssuesEnabled() {
585 return nil
586 }
587 return send(out)
588}
589
590// deliverRepo sends an event about a repository if the watcher could read the
591// repository when the event happened and can read it now, and the repository
592// is still the one the event is about. A repository that no longer exists is
593// skipped: its RepoDeleted follows or came before.
594//
595// The exception is a repository that became private: the watchers who could
596// read it before are told once, with its name and its new visibility only,
597// so that what they show can be removed.
598func (e *Events) deliverRepo(ctx context.Context, w *watch, name domain.RepoName, withRepo bool, be bufferedEvent, out ports.WatchEvent, send func(ports.WatchEvent) error) error {
599 if !w.wantsEvent(name, be.ev) || !be.known {
600 return nil
601 }
602 repo, err := lookupRepo(ctx, e.repos.meta, name)
603 switch {
604 case errors.Is(err, domain.ErrNotFound):
605 return nil
606 case ctx.Err() != nil:
607 return nil
608 case err != nil:
609 return fmt.Errorf("check access to %q: %w", name, err)
610 }
611 if repo.CreatedAt.After(be.created) {
612 return nil // the event is about a repository of this name that was deleted
613 }
614 if !canReadAs(w.p, name, be.vis) {
615 if up, ok := be.ev.(domain.RepoUpdated); ok && canReadAs(w.p, name, up.Previous) {
616 out.Repo = &ports.RepoView{Repo: domain.Repo{Name: name, Visibility: up.Visibility}}
617 return send(out)
618 }
619 return nil
620 }
621 if !canRead(w.p, repo) {
622 return nil
623 }
624 if isIssueEvent(be.ev) && !repo.Settings.IssuesEnabled() {
625 return nil // issues were turned off since: a replay must not show them
626 }
627 if withRepo {
628 view, err := e.repos.View(ctx, repo)
629 if err != nil {
630 return err
631 }
632 out.Repo = &view
633 }
634 return send(out)
635}
636
637// isIssueEvent reports whether ev is about an issue.
638func isIssueEvent(ev domain.Event) bool {
639 switch ev.(type) {
640 case domain.IssueOpened, domain.IssueClosed, domain.IssueReopened, domain.IssueCommented:
641 return true
642 }
643 return false
644}
645
646// isRepoLifecycleEvent reports whether ev is a repository update or deletion:
647// the owner topic follows them, for they change what the inbox lists.
648func isRepoLifecycleEvent(ev domain.Event) bool {
649 switch ev.(type) {
650 case domain.RepoUpdated, domain.RepoDeleted:
651 return true
652 }
653 return false
654}
655
656// isItemEvent reports whether ev is about a merge request or an issue.
657func isItemEvent(ev domain.Event) bool {
658 switch ev.(type) {
659 case domain.MergeRequestOpened, domain.MergeRequestMerged, domain.MergeRequestClosed,
660 domain.MergeRequestReady, domain.MergeRequestMergeability, domain.CommentAdded,
661 domain.CommentEdited, domain.CommentDeleted, domain.CommentResolved:
662 return true
663 }
664 return isIssueEvent(ev)
665}