Skip to content

Commit 61b0898

Browse files
authored
Merge pull request #160 from Icinga/nullable-incident
notifications: make incident obj optional for non-state events
2 parents 1f5f18e + b6cb018 commit 61b0898

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

notifications/plugin/plugin.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,8 @@ type NotificationRequest struct {
174174
Object *Object `json:"object"`
175175

176176
// Incident associated with this NotificationRequest.
177+
//
178+
// May be nil when sending non-state notifications and no active incident exists for the given Object.
177179
Incident *Incident `json:"incident"`
178180

179181
// Event being responsible for creating this NotificationRequest, e.g., a firing Icinga 2 Service Check.
@@ -310,7 +312,11 @@ func FormatMessage(writer io.Writer, req *NotificationRequest) {
310312
}
311313
}
312314

313-
_, _ = fmt.Fprintf(writer, "\nIncident: %s", req.Incident.Url)
315+
if req.Incident != nil {
316+
_, _ = fmt.Fprintf(writer, "\nIncident: %s", req.Incident.Url)
317+
} else {
318+
_, _ = fmt.Fprint(writer, "\nIncident: No active incident found for this object")
319+
}
314320
}
315321

316322
// FormatSubject returns the formatted subject string based on the event type.
@@ -319,8 +325,14 @@ func FormatSubject(req *NotificationRequest) string {
319325
case event.TypeState:
320326
return fmt.Sprintf("[#%d] %s %s is %s", req.Incident.Id, req.Event.Type, req.Object.Name, req.Incident.Severity)
321327
case event.TypeAcknowledgementCleared, event.TypeDowntimeRemoved:
322-
return fmt.Sprintf("[#%d] %s from %s", req.Incident.Id, req.Event.Type, req.Object.Name)
328+
if req.Incident != nil {
329+
return fmt.Sprintf("[#%d] %s from %s", req.Incident.Id, req.Event.Type, req.Object.Name)
330+
}
331+
return fmt.Sprintf("%s from %s", req.Event.Type, req.Object.Name)
323332
default:
324-
return fmt.Sprintf("[#%d] %s on %s", req.Incident.Id, req.Event.Type, req.Object.Name)
333+
if req.Incident != nil {
334+
return fmt.Sprintf("[#%d] %s on %s", req.Incident.Id, req.Event.Type, req.Object.Name)
335+
}
336+
return fmt.Sprintf("%s on %s", req.Event.Type, req.Object.Name)
325337
}
326338
}

0 commit comments

Comments
 (0)