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
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ 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/gogo/protobuf v1.3.2 // indirect
Expand All @@ -18,6 +17,7 @@ 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 // indirect
github.com/spf13/afero v1.5.1 // indirect
github.com/spf13/cast v1.3.1 // indirect
github.com/spf13/cobra v1.1.1 // indirect
Expand All @@ -31,4 +31,4 @@ require (
google.golang.org/grpc v1.35.0 // indirect
gopkg.in/ini.v1 v1.62.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
)
)
5 changes: 5 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-test/deep v1.0.4/go.mod h1:wGDj63lr65AM2AQyKZd/NYHGb0R+1RLqB8NKt3aSFNA=
github.com/gogo/protobuf v1.1.1 h1:72R+M5VuhED/KujmZVcIquuo8mBgX4oVda//DQb3PXo=
github.com/gogo/protobuf v1.1.1/go.mod h1:r8qH/GZQm5c6nD/R0oafs1akxWv10x8SbQlK7atdtwQ=
github.com/gogo/protobuf v1.2.1/go.mod h1:hp+jE20tsWTFYpLwKvXlhS1hjn+gTNwPg2I6zVXpSg4=
Expand Down Expand Up @@ -107,6 +108,7 @@ github.com/google/go-cmp v0.3.1/go.mod h1:8QqcDgzrUqlUb/G2PQTWiueGozuR1884gddMyw
github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.0 h1:/QaMHBdZ26BB3SSst0Iwl10Epc+xhTquomWX0oZEB6w=
github.com/google/go-cmp v0.5.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.7/go.mod h1:n+brtR0CgQNWTVd5ZUFpTBC8YFBDLK/h/bpaJ8/DtOE=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/google/martian v2.1.0+incompatible/go.mod h1:9I4somxYTbIHy5NJKHRl3wXiIaQGbYVAs8BPL6v8lEs=
github.com/google/pprof v0.0.0-20181206194817-3ea8567a2e57/go.mod h1:zfwlbNMJ+OItoe0UupaVj+oy1omPYYDuagoSzA8v9mc=
Expand All @@ -122,6 +124,7 @@ github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5m
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1 h1:EGx4pi6eqNxGaHF6qqu48+N2wcFQ5qg5FXgOdqsJ5d8=
github.com/gopherjs/gopherjs v0.0.0-20181017120253-0766667cb4d1/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ=
github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc=
github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs=
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0/go.mod h1:8NvIoxWQoOIhqOTXgfV/d3M/q6VIi02HzZEHgUlZvzk=
Expand Down Expand Up @@ -257,6 +260,8 @@ github.com/sirupsen/logrus v1.6.0 h1:UBcNElsrwanuuMsnGSlYmtmgbb23qDR5dG+6X6Oo89I
github.com/sirupsen/logrus v1.6.0/go.mod h1:7uNnSEd1DgxDLC74fIahvMZmmYsHGZGEOFrfsX/uA88=
github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/slack-go/slack v0.13.0 h1:7my/pR2ubZJ9912p9FtvALYpbt0cQPAqkRy2jaSI1PQ=
github.com/slack-go/slack v0.13.0/go.mod h1:hlGi5oXA+Gt+yWTPP0plCdRKmjsDxecdHxYQdlMQKOw=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykElWQ6/NYmHa3jpm/yHnI4xSofP+UP6SpjHcSeM=
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
Expand Down
22 changes: 12 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ import (
"os"
"strings"

"github.com/bluele/slack"
//"github.com/bluele/slack"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick: commented out code should be removed :)

"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"
)

// HandlerConfig contains the Slack handler configuration
Expand Down Expand Up @@ -186,19 +187,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,15 +221,16 @@ 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)
return fmt.Errorf("Failed to send Slack message: %v ", err)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you accidentally added an extra white space at the end of the caption.

}

// FUTURE: send to AH
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