Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

slack sdk change #52

Merged
merged 14 commits into from
Jun 11, 2024
2 changes: 1 addition & 1 deletion .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ jobs:
steps:
- uses: actions/setup-go@v3
with:
go-version: 1.17
go-version: 1.22
- uses: actions/checkout@v3
- name: golangci-lint
uses: golangci/golangci-lint-action@v3
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
- name: Set up Go
uses: actions/setup-go@v1
with:
go-version: 1.14.x
go-version: 1.22.x
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v1
with:
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Go Test

on: [push, pull_request]
on: [push]

jobs:
test:
Expand All @@ -12,10 +12,10 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Go 1.14
- name: Set up Go 1.22
uses: actions/setup-go@v1
with:
go-version: 1.14
go-version: 1.22
id: go
- name: Test
run: go test -v ./...
run: go test -v ./...
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [1.6.0] - 2024-05-30

### Changed
- Update the slack dependent sdk from "github.com/bluele/slack" to "github.com/slack-go/slack"

## [1.5.0] - 2021-03-022

### Changed
Expand Down
8 changes: 3 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,8 @@ module github.com/sensu/sensu-slack-handler
go 1.14

require (
github.com/bluele/slack v0.0.0-20180528010058-b4b4d354a079
github.com/coreos/etcd v3.3.25+incompatible // indirect
github.com/fsnotify/fsnotify v1.4.9 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/protobuf v1.4.3 // indirect
github.com/google/uuid v1.2.0 // indirect
Expand All @@ -18,15 +17,14 @@ require (
github.com/sensu/sensu-go/api/core/v2 v2.6.0
github.com/sensu/sensu-go/types v0.5.0 // indirect
github.com/sirupsen/logrus v1.7.0 // indirect
github.com/slack-go/slack v0.13.0
github.com/spf13/afero v1.5.1 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/cobra v1.1.1 // indirect
github.com/spf13/jwalterweatherman v1.1.0 // indirect
github.com/spf13/viper v1.7.1 // indirect
github.com/stretchr/testify v1.6.0
golang.org/x/net v0.0.0-20210119194325-5f4716e94777 // indirect
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c // indirect
golang.org/x/text v0.3.5 // indirect
golang.org/x/net v0.26.0 //indirect
google.golang.org/genproto v0.0.0-20210204154452-deb828366460 // indirect
google.golang.org/grpc v1.35.0 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
Expand Down
118 changes: 66 additions & 52 deletions go.sum

Large diffs are not rendered by default.

24 changes: 12 additions & 12 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,12 @@ package main

import (
"fmt"
"os"
"strings"

"github.com/bluele/slack"
"github.com/sensu-community/sensu-plugin-sdk/sensu"
"github.com/sensu-community/sensu-plugin-sdk/templates"
corev2 "github.com/sensu/sensu-go/api/core/v2"
"github.com/slack-go/slack"
"os"
"strings"
)

// HandlerConfig contains the Slack handler configuration
Expand Down Expand Up @@ -186,19 +185,19 @@ func messageStatus(event *corev2.Event) string {
}
}

func messageAttachment(event *corev2.Event) *slack.Attachment {
func messageAttachment(event *corev2.Event) slack.Attachment {
description, err := templates.EvalTemplate("description", config.slackDescriptionTemplate, event)
if err != nil {
fmt.Printf("%s: Error processing template: %s", config.PluginConfig.Name, err)
}

description = strings.Replace(description, `\n`, "\n", -1)
attachment := &slack.Attachment{
attachment := slack.Attachment{
Title: "Description",
Text: description,
Fallback: formattedMessage(event),
Color: messageColor(event),
Fields: []*slack.AttachmentField{
Fields: []slack.AttachmentField{
{
Title: "Status",
Value: messageStatus(event),
Expand All @@ -220,13 +219,14 @@ func messageAttachment(event *corev2.Event) *slack.Attachment {
}

func sendMessage(event *corev2.Event) error {
hook := slack.NewWebHook(config.slackwebHookURL)
err := hook.PostMessage(&slack.WebHookPostPayload{
Attachments: []*slack.Attachment{messageAttachment(event)},
hookmsg := &slack.WebhookMessage{
Attachments: []slack.Attachment{messageAttachment(event)},
Channel: config.slackChannel,
IconUrl: config.slackIconURL,
IconURL: config.slackIconURL,
Username: config.slackUsername,
})
}

err := slack.PostWebhook(config.slackwebHookURL, hookmsg)
if err != nil {
return fmt.Errorf("Failed to send Slack message: %v", err)
}
Expand Down
51 changes: 15 additions & 36 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
package main

import (
"encoding/json"
"io/ioutil"
corev2 "github.com/sensu/sensu-go/api/core/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"io"
"net/http"
"net/http/httptest"
"os"
"testing"

corev2 "github.com/sensu/sensu-go/api/core/v2"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestFormattedEventAction(t *testing.T) {
Expand Down Expand Up @@ -108,8 +106,8 @@ func TestSendMessage(t *testing.T) {
event := corev2.FixtureEvent("entity1", "check1")

var apiStub = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
body, _ := ioutil.ReadAll(r.Body)
expectedBody := `{"channel":"#test","attachments":[{"color":"good","fallback":"RESOLVED - entity1/check1:","title":"Description","text":"","fields":[{"title":"Status","value":"Resolved","short":false},{"title":"Entity","value":"entity1","short":true},{"title":"Check","value":"check1","short":true}]}]}`
body, _ := io.ReadAll(r.Body)
expectedBody := `{"channel":"#test","attachments":[{"color":"good","fallback":"RESOLVED - entity1/check1:","title":"Description","fields":[{"title":"Status","value":"Resolved","short":false},{"title":"Entity","value":"entity1","short":true},{"title":"Check","value":"check1","short":true}],"blocks":null}],"replace_original":false,"delete_original":false}`
assert.Equal(expectedBody, string(body))
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(`{"ok": true}`))
Expand All @@ -123,34 +121,15 @@ func TestSendMessage(t *testing.T) {
assert.NoError(err)
}

func TestMain(t *testing.T) {
func TestCheckArgs(t *testing.T) {
assert := assert.New(t)
file, _ := ioutil.TempFile(os.TempDir(), "sensu-handler-slack-")
defer func() {
_ = os.Remove(file.Name())
}()

config := HandlerConfig{}
event := corev2.FixtureEvent("entity1", "check1")
eventJSON, _ := json.Marshal(event)
_, err := file.WriteString(string(eventJSON))
require.NoError(t, err)
require.NoError(t, file.Sync())
_, err = file.Seek(0, 0)
require.NoError(t, err)
os.Stdin = file
requestReceived := false

var apiStub = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
requestReceived = true
w.WriteHeader(http.StatusOK)
_, err := w.Write([]byte(`{"ok": true}`))
require.NoError(t, err)
}))

oldArgs := os.Args
os.Args = []string{"slack", "-w", apiStub.URL}
defer func() { os.Args = oldArgs }()

main()
assert.True(requestReceived)
config.slackDescriptionTemplate = "Sensu Event Details"
config.slackUsername = "Dummy user"
config.slackChannel = "Test"
config.slackIconURL = "https://www.sensu.io/img/sensu-logo.png"
_ = os.Setenv("SLACK_WEBHOOK_URL", "http://example.com/webhook")
config.slackwebHookURL = os.Getenv("SLACK_WEBHOOK_URL")
assert.NoError(checkArgs(event))
}
Loading