Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions pkg/notifications/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ type Config struct {
```

<a name="Level"></a>
## type [Level](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/notifications/model.go#L34>)
## type [Level](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/notifications/model.go#L36>)

Level describes the severity of the notification

Expand All @@ -83,20 +83,21 @@ const (
```

<a name="Notification"></a>
## type [Notification](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/notifications/model.go#L9-L18>)
## type [Notification](<https://github.com/greenbone/opensight-golang-libraries/blob/main/pkg/notifications/model.go#L9-L19>)



```go
type Notification struct {
// omit property `Id` here, as it is read only
Origin string
OriginUri string // can be used to provide a link to the origin
Timestamp time.Time // client will set timestamp if not set
Title string // can also be seen as the 'type'
Detail string
Level Level
CustomFields map[string]any // can contain arbitrary structured information about the notification
Origin string // name of the origin, e.g. `SBOM - React`
OriginClass string // unique identifier for the class of origins, e.g. `/vi/SBOM`
OriginResourceID string // optional, together with class it can be used to provide a link to the origin, e.g. `<id of react sbom object>`
Timestamp time.Time // client will set timestamp if not set
Title string // can also be seen as the 'type'
Detail string
Level Level
CustomFields map[string]any // optional, can contain arbitrary structured information about the notification
}
```

Expand Down
45 changes: 24 additions & 21 deletions pkg/notifications/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,28 @@ import "time"

type Notification struct {
// omit property `Id` here, as it is read only
Origin string
OriginUri string // can be used to provide a link to the origin
Timestamp time.Time // client will set timestamp if not set
Title string // can also be seen as the 'type'
Detail string
Level Level
CustomFields map[string]any // can contain arbitrary structured information about the notification
Origin string // name of the origin, e.g. `SBOM - React`
OriginClass string // unique identifier for the class of origins, e.g. `/vi/SBOM`
OriginResourceID string // optional, together with class it can be used to provide a link to the origin, e.g. `<id of react sbom object>`
Timestamp time.Time // client will set timestamp if not set
Title string // can also be seen as the 'type'
Detail string
Level Level
CustomFields map[string]any // optional, can contain arbitrary structured information about the notification
}

// notification is the object which is to the notification service.
// It is defined in notification service REST API: https://github.com/greenbone/opensight-notification-service/tree/main/api/notificationservice
type notificationModel struct {
// omit property `Id` here, as it is read only
Origin string `json:"origin"`
OriginUri string `json:"originUri,omitempty"`
Timestamp string `json:"timestamp" format:"date-time"`
Title string `json:"title"`
Detail string `json:"detail"`
Level Level `json:"level"`
CustomFields map[string]any `json:"customFields,omitempty"`
Origin string `json:"origin"`
OriginClass string `json:"originClass"`
OriginResourceID string `json:"originResourceID,omitempty"`
Timestamp string `json:"timestamp" format:"date-time"`
Title string `json:"title"`
Detail string `json:"detail"`
Level Level `json:"level"`
CustomFields map[string]any `json:"customFields,omitempty"`
}

// Level describes the severity of the notification
Expand All @@ -47,12 +49,13 @@ func toNotificationModel(n Notification) notificationModel {
}

return notificationModel{
Origin: n.Origin,
OriginUri: n.OriginUri,
Timestamp: n.Timestamp.UTC().Format(time.RFC3339Nano),
Title: n.Title,
Detail: n.Detail,
Level: n.Level,
CustomFields: n.CustomFields,
Origin: n.Origin,
OriginClass: n.OriginClass,
OriginResourceID: n.OriginResourceID,
Timestamp: n.Timestamp.UTC().Format(time.RFC3339Nano),
Title: n.Title,
Detail: n.Detail,
Level: n.Level,
CustomFields: n.CustomFields,
}
}
28 changes: 16 additions & 12 deletions pkg/notifications/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,28 @@ const checkForCurrentTimestamp = "marker to check for current timestamp"

func TestClient_CreateNotification(t *testing.T) {
notification := Notification{
Origin: "Example Task XY",
Timestamp: time.Date(1, 2, 3, 4, 5, 6, 7, time.UTC),
Title: "Example Task XY failed",
Detail: "Example Task XY failed because ...",
Level: LevelError,
CustomFields: map[string]any{"extraProperty": "value"},
Origin: "Example Task XY - 1",
OriginClass: "ab/exampletaskxy",
OriginResourceID: "1",
Timestamp: time.Date(1, 2, 3, 4, 5, 6, 7, time.UTC),
Title: "Example Task XY failed",
Detail: "Example Task XY failed because ...",
Level: LevelError,
CustomFields: map[string]any{"extraProperty": "value"},
}

notificationWithoutTimestamp := notification
notificationWithoutTimestamp.Timestamp = time.Time{}

wantNotification := notificationModel{
Origin: "Example Task XY",
Timestamp: "0001-02-03T04:05:06.000000007Z",
Title: "Example Task XY failed",
Detail: "Example Task XY failed because ...",
Level: LevelError,
CustomFields: map[string]any{"extraProperty": "value"},
Origin: "Example Task XY - 1",
OriginClass: "ab/exampletaskxy",
OriginResourceID: "1",
Timestamp: "0001-02-03T04:05:06.000000007Z",
Title: "Example Task XY failed",
Detail: "Example Task XY failed because ...",
Level: LevelError,
CustomFields: map[string]any{"extraProperty": "value"},
}
wantNotificationWithoutTimestamp := wantNotification
wantNotificationWithoutTimestamp.Timestamp = checkForCurrentTimestamp // can't test exact timestamp
Expand Down
Loading