From 773a51d6cddeda01de53ca95eed10b4cebbe0bbb Mon Sep 17 00:00:00 2001 From: Mattias Jiderhamn Date: Wed, 4 Dec 2019 15:17:08 +0100 Subject: [PATCH] Turn annotations with values starting with "http://" or "https://" into PagerDuty links --- main.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/main.go b/main.go index bdbd0e9..9f090d1 100644 --- a/main.go +++ b/main.go @@ -6,6 +6,7 @@ import ( "log" "os" "regexp" + "strings" "github.com/PagerDuty/go-pagerduty" "github.com/sensu-community/sensu-plugin-sdk/sensu" @@ -207,6 +208,7 @@ func manageIncident(event *corev2.Event) error { Action: action, Payload: &pdPayload, DedupKey: dedupKey, + Links: getPagerDutyLinks(event), } eventResponse, err := pagerduty.ManageEvent(pdEvent) @@ -324,3 +326,24 @@ func getDetails(event *corev2.Event) (interface{}, error) { } return details, nil } + +type Link struct { + Text string `json:"text"` + Href string `json:"href"` +} + +func getPagerDutyLinks(event *corev2.Event) []interface{} { + var output []interface{} + output = make([]interface{}, 0, len(event.Check.Annotations)) + + for name, value := range event.Check.Annotations { + if strings.HasPrefix(value, "http://") || strings.HasPrefix(value, "https://") { // value seems to be a link + output = append(output, Link{ + Text: name, + Href: value, + }) + } + } + + return output +}