-
Notifications
You must be signed in to change notification settings - Fork 0
/
events.go
101 lines (89 loc) · 2.39 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
package fastlane
import (
"github.com/google/uuid"
"strings"
)
const (
ReviewsEventName = "REVIEWS"
ReviewMergedEventName = "REVIEW-MERGED"
ReviewsMergedEventName = "REVIEWS-MERGED"
MergeEventName = "MERGE"
NotificationEventName = "NOTIFICATION"
SystemNotificationEventName = "SYSTEM-NOTIFICATION"
)
const (
SuccessType NotificationType = "success"
InfoType NotificationType = "info"
WarningType NotificationType = "warning"
ErrorType NotificationType = "error"
)
type Event struct {
Name string `json:"name"`
Data interface{} `json:"data"`
}
type NotificationType string
type Notification struct {
ID uuid.UUID `json:"id"`
Message string `json:"message"`
Type NotificationType `json:"type"`
}
type SystemNotification struct {
Title string `json:"title"`
Message string `json:"message"`
}
type ReviewMerged struct {
Review Review `json:"review"`
Pipeline Pipeline `json:"pipeline"`
HasPipeline bool `json:"has_pipeline"`
}
type ReviewUpdatedData struct {
Old Review
New Review
}
func calculateReviewEvents(current, updated []Review) (events []Event) {
m := make(map[string]Review, len(current))
for _, r := range current {
m[r.ID] = r
}
for _, r := range updated {
// if not found, old review is zero value
old := m[r.ID]
if r.MergeEnabled() && !old.MergeEnabled() {
events = append(events, Event{
Name: SystemNotificationEventName,
Data: SystemNotification{
Title: r.Title,
Message: "Review can be merged! click to merge",
}})
}
if !r.MergeEnabled() && old.MergeEnabled() {
approved := make(map[string]struct{})
for _, a := range old.Approvals {
approved[a.Username] = struct{}{}
}
for _, a := range r.Approvals {
delete(approved, a.Username)
}
var removed []string
for username := range approved {
removed = append(removed, username)
}
if len(removed) > 0 {
events = append(events, Event{
Name: SystemNotificationEventName,
Data: SystemNotification{
Title: r.Title,
Message: "Review cannot be merged anymore! @" + strings.Join(removed, ", @") + " removed the approval",
}})
} else {
events = append(events, Event{
Name: SystemNotificationEventName,
Data: SystemNotification{
Title: r.Title,
Message: "Review cannot be merged anymore!",
}})
}
}
}
return events
}