-
Notifications
You must be signed in to change notification settings - Fork 137
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #236 from SomtochiAma/lark-notification
Add Lark alerting provider
- Loading branch information
Showing
6 changed files
with
170 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package notifier | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
"strings" | ||
|
||
"github.com/fluxcd/pkg/runtime/events" | ||
) | ||
|
||
type Lark struct { | ||
URL string | ||
} | ||
|
||
type LarkPayload struct { | ||
MsgType string `json:"msg_type"` | ||
Card LarkCard `json:"card"` | ||
} | ||
|
||
type LarkCard struct { | ||
Config LarkConfig `json:"config"` | ||
|
||
Header LarkHeader `json:"header"` | ||
|
||
Elements []LarkElement `json:"elements"` | ||
} | ||
|
||
type LarkConfig struct { | ||
WideScreenMode bool `json:"wide_screen_mode"` | ||
} | ||
|
||
type LarkHeader struct { | ||
Title LarkTitle `json:"title"` | ||
Template string `json:"template"` | ||
} | ||
|
||
type LarkTitle struct { | ||
Tag string `json:"tag"` | ||
Content string `json:"content"` | ||
} | ||
|
||
type LarkElement struct { | ||
Tag string `json:"tag"` | ||
Text LarkText `json:"text"` | ||
} | ||
|
||
type LarkText struct { | ||
Tag string `json:"tag"` | ||
Content string `json:"content"` | ||
} | ||
|
||
func NewLark(address string) (*Lark, error) { | ||
_, err := url.ParseRequestURI(address) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid Slack hook URL %s", address) | ||
} | ||
|
||
return &Lark{ | ||
URL: address, | ||
}, nil | ||
} | ||
|
||
func (l *Lark) Post(event events.Event) error { | ||
// Skip any update events | ||
if isCommitStatus(event.Metadata, "update") { | ||
return nil | ||
} | ||
|
||
emoji := "💫" | ||
color := "turquoise" | ||
if event.Severity == events.EventSeverityError { | ||
emoji = "🚨" | ||
color = "red" | ||
} | ||
|
||
message := fmt.Sprintf("**%s**\n\n", event.Message) | ||
for k, v := range event.Metadata { | ||
message = message + fmt.Sprintf("%s: %s\n", k, v) | ||
} | ||
|
||
element := LarkElement{ | ||
Tag: "div", | ||
Text: LarkText{ | ||
Tag: "lark_md", | ||
Content: message, | ||
}, | ||
} | ||
|
||
card := LarkCard{ | ||
Config: LarkConfig{ | ||
WideScreenMode: true, | ||
}, | ||
Header: LarkHeader{ | ||
Title: LarkTitle{ | ||
Tag: "plain_text", | ||
Content: fmt.Sprintf("%s %s/%s.%s", emoji, strings.ToLower(event.InvolvedObject.Kind), | ||
event.InvolvedObject.Name, event.InvolvedObject.Namespace), | ||
}, | ||
Template: color, | ||
}, | ||
Elements: []LarkElement{ | ||
element, | ||
}, | ||
} | ||
|
||
payload := LarkPayload{ | ||
MsgType: "interactive", | ||
Card: card, | ||
} | ||
|
||
return postMessage(l.URL, "", nil, payload) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package notifier | ||
|
||
import ( | ||
"encoding/json" | ||
"io/ioutil" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestLark_Post(t *testing.T) { | ||
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
b, err := ioutil.ReadAll(r.Body) | ||
require.NoError(t, err) | ||
var payload LarkPayload | ||
err = json.Unmarshal(b, &payload) | ||
require.NoError(t, err) | ||
|
||
require.Equal(t, "💫 gitrepository/webapp.gitops-system", payload.Card.Header.Title.Content) | ||
require.Equal(t, "turquoise", payload.Card.Header.Template) | ||
})) | ||
defer ts.Close() | ||
|
||
lark, err := NewLark(ts.URL) | ||
require.NoError(t, err) | ||
|
||
err = lark.Post(testEvent()) | ||
require.NoError(t, err) | ||
} |