Skip to content

Commit eaa1c8a

Browse files
author
Tossaporn Jiw
committed
fix: handle key not found
Signed-off-by: Tossaporn Jiw <[email protected]>
1 parent 7c2485f commit eaa1c8a

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

notify/msteamsv2/msteamsv2.go

+14-5
Original file line numberDiff line numberDiff line change
@@ -240,23 +240,23 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
240240
},
241241
{
242242
Title: "Alert",
243-
Value: data.CommonLabels["alertname"],
243+
Value: extractKV(data.CommonLabels, "alertname"),
244244
},
245245
{
246246
Title: "Summary",
247-
Value: data.CommonAnnotations["summary"],
247+
Value: extractKV(data.CommonAnnotations, "summary"),
248248
},
249249
{
250250
Title: "Severity",
251251
Value: renderSeverity(data.CommonLabels["severity"]),
252252
},
253253
{
254254
Title: "In Host",
255-
Value: data.CommonLabels["instance"],
255+
Value: extractKV(data.CommonLabels, "instance"),
256256
},
257257
{
258258
Title: "Description",
259-
Value: data.CommonAnnotations["description"],
259+
Value: extractKV(data.CommonAnnotations, "description"),
260260
},
261261
{
262262
Title: "Common Labels",
@@ -273,14 +273,16 @@ func (n *Notifier) Notify(ctx context.Context, as ...*types.Alert) (bool, error)
273273
{
274274
Type: "Action.OpenUrl",
275275
Title: "View details",
276-
URL: data.CommonAnnotations["runbook_url"],
276+
URL: extractKV(data.CommonAnnotations, "runbook_url"),
277277
},
278278
},
279279
},
280280
},
281281
},
282282
}
283283

284+
// Check if summary exists in CommonLabels
285+
284286
if err = json.NewEncoder(&payload).Encode(t); err != nil {
285287
return false, err
286288
}
@@ -335,3 +337,10 @@ func renderCommonAnnotations(commonLabels template.KV) string {
335337

336338
return commonLabels.Remove(removeList).String()
337339
}
340+
341+
func extractKV(kv template.KV, key string) string {
342+
if v, ok := kv[key]; ok {
343+
return v
344+
}
345+
return ""
346+
}

0 commit comments

Comments
 (0)