-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevents.go
77 lines (71 loc) · 1.77 KB
/
events.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
package main
import (
"context"
"sort"
"github.com/shurcooL/events"
"github.com/shurcooL/events/event"
"github.com/shurcooL/events/fs"
"github.com/shurcooL/users"
"golang.org/x/net/webdav"
)
func newEventsService(
root webdav.FileSystem,
githubEvents, gerritEvents events.Service,
users users.Service,
) (events.Service, error) {
dmitshur, err := users.Get(context.Background(), dmitshur)
if err != nil {
return nil, err
}
local, err := fs.NewService(root, dmitshur, users)
if err != nil {
return nil, err
}
return multiEvents{
githubEvents, // Events from GitHub.
local, // Events from local store.
gerritEvents, // Events from Gerrit instance at go.googlesource.com.
}, nil
}
// multiEvents is a union of multiple events.Services.
type multiEvents []events.Service
// List lists newest 100 events from all services.
//
// It keeps going even if there are errors encountered, but reports them at the end.
func (m multiEvents) List(ctx context.Context) ([]event.Event, error) {
var events []event.Event
var errors []error
for _, s := range m {
e, err := s.List(ctx)
if err != nil {
errors = append(errors, err)
}
events = append(events, e...)
}
sort.SliceStable(events, func(i, j int) bool {
return events[i].Time.After(events[j].Time)
})
if len(events) > 100 {
events = events[:100]
}
if len(errors) > 0 {
return events, errors[0]
}
return events, nil
}
// Log logs the event to all services.
//
// It keeps going even if there are errors encountered, but reports them at the end.
func (m multiEvents) Log(ctx context.Context, event event.Event) error {
var errors []error
for _, s := range m {
err := s.Log(ctx, event)
if err != nil {
errors = append(errors, err)
}
}
if len(errors) > 0 {
return errors[0]
}
return nil
}