-
Notifications
You must be signed in to change notification settings - Fork 0
/
notification.go
145 lines (115 loc) · 4.2 KB
/
notification.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package contracts
import (
"encoding/json"
"fmt"
"time"
)
type NotificationType string
const (
NotificationTypeUnknown NotificationType = ""
NotificationTypeVulnerability NotificationType = "vulnerability"
NotificationTypeWarning NotificationType = "warning"
)
type NotificationLevel string
const (
NotificationLevelUnknown NotificationLevel = ""
NotificationLevelCritical NotificationLevel = "critical"
NotificationLevelHigh NotificationLevel = "high"
NotificationLevelMedium NotificationLevel = "medium"
NotificationLevelLow NotificationLevel = "low"
)
type NotificationLinkType string
const (
NotificationLinkTypeUnknown NotificationLinkType = ""
NotificationLinkTypePipeline NotificationLinkType = "pipeline"
NotificationLinkTypeContainer NotificationLinkType = "container"
)
type Notification struct {
Type NotificationType `json:"type,omitempty"`
Level NotificationLevel `json:"level,omitempty"`
Message string `json:"message,omitempty"`
}
type NotificationRecord struct {
ID string `json:"id,omitempty"`
LinkType NotificationLinkType `json:"linkType,omitempty"`
LinkID string `json:"linkID,omitempty"`
// fields mapped to link_detail column
PipelineDetail *PipelineLinkDetail `json:"pipelineDetail,omitempty"`
ContainerDetail *ContainerLinkDetail `json:"containerDetail,omitempty"`
Source string `json:"source,omitempty"`
Notifications []Notification `json:"notifications,omitempty"`
InsertedAt *time.Time `json:"insertedAt,omitempty"`
Groups []*Group `json:"groups,omitempty"`
Organizations []*Organization `json:"organizations,omitempty"`
}
type PipelineLinkDetail struct {
Branch string `json:"branch,omitempty"`
Revision string `json:"revision"`
Version string `json:"version,omitempty"`
Status Status `json:"status,omitempty"`
}
type ContainerLinkDetail struct {
Tag string `json:"tag,omitempty"`
PublicImage bool `json:"publicImage,omitempty"`
}
func (nr *NotificationRecord) GetLinkDetail() ([]byte, error) {
switch nr.LinkType {
case NotificationLinkTypePipeline:
return json.Marshal(nr.PipelineDetail)
case NotificationLinkTypeContainer:
return json.Marshal(nr.ContainerDetail)
}
return []byte{}, nil
}
func (nr *NotificationRecord) SetLinkDetail(linkDetail []byte) error {
if len(linkDetail) == 0 {
return nil
}
switch nr.LinkType {
case NotificationLinkTypePipeline:
if err := json.Unmarshal(linkDetail, &nr.PipelineDetail); err != nil {
return fmt.Errorf("LinkDetail for NotificationRecord %v of type %v is not of type PipelineLinkDetail: %w", nr.LinkID, nr.LinkType, err)
}
case NotificationLinkTypeContainer:
if err := json.Unmarshal(linkDetail, &nr.ContainerDetail); err != nil {
return fmt.Errorf("LinkDetail for NotificationRecord %v of type %v is not of type ContainerLinkDetail: %w", nr.LinkID, nr.LinkType, err)
}
}
return nil
}
func (nr *NotificationRecord) GetNotifications() ([]byte, error) {
return json.Marshal(nr.Notifications)
}
func (nr *NotificationRecord) SetNotifications(notifications []byte) error {
if len(notifications) == 0 {
return nil
}
if err := json.Unmarshal(notifications, &nr.Notifications); err != nil {
return fmt.Errorf("Notifications for NotificationRecord %v of type %v is not of type []Notification: %w", nr.LinkID, nr.LinkType, err)
}
return nil
}
func (nr *NotificationRecord) GetGroups() ([]byte, error) {
return json.Marshal(nr.Groups)
}
func (nr *NotificationRecord) SetGroups(groups []byte) error {
if len(groups) == 0 {
return nil
}
if err := json.Unmarshal(groups, &nr.Groups); err != nil {
return fmt.Errorf("Groups for NotificationRecord %v of type %v is not of type []*Group: %w", nr.LinkID, nr.LinkType, err)
}
return nil
}
func (nr *NotificationRecord) GetOrganizations() ([]byte, error) {
return json.Marshal(nr.Organizations)
}
func (nr *NotificationRecord) SetOrganizations(organizations []byte) error {
if len(organizations) == 0 {
return nil
}
if err := json.Unmarshal(organizations, &nr.Organizations); err != nil {
return fmt.Errorf("Organizations for NotificationRecord %v of type %v is not of type []*Group: %w", nr.LinkID, nr.LinkType, err)
}
return nil
}