Skip to content
This repository has been archived by the owner on May 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #9 from LF-Engineering/slack
Browse files Browse the repository at this point in the history
add slack library
  • Loading branch information
aultron authored Nov 19, 2021
2 parents 930a6c3 + b14d708 commit 9c9762e
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 0 deletions.
77 changes: 77 additions & 0 deletions slack/slack.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package slack

import (
"encoding/json"
"errors"

"gopkg.in/resty.v1"
)

var (
errorBadResponse = "API call gave an other-than-200 result"
errorEmptyText = "refusing to send an empty message, consider calling slack.SetText() first"
)

// Provider provides an interface to interact with the Slack Webhook API
type Provider struct {
Debug bool
Payload Payload
WebhookURL string
}

// Payload holds information about the message we want to send. Any of these values except Text may be left empty to
// use the defaults for the Webhook. IconEmoji and IconURL are mutually exclusive, do not set both.
// https://api.slack.com/incoming-webhooks#posting_with_webhooks
type Payload struct {
Channel string `json:"channel,omitempty"`
IconEmoji string `json:"icon_emoji,omitempty"`
IconURL string `json:"icon_url,omitempty"`
Text string `json:"text,omitempty"`
Username string `json:"username,omitempty"`
}

// New creates a new invocation of this Provider wrapper
func New(webhookURL string) Provider {
return Provider{
WebhookURL: webhookURL,
}
}

// SetText sets the message text, i.e. what will be displayed in the Slack window
func (a *Provider) SetText(text string) {
a.Payload.Text = text
}

// Send sends a message to Slack
func (a *Provider) Send() error {
if a.Payload.Text == "" {
return errors.New(errorEmptyText)
}
body, err := json.Marshal(a.Payload)
if err != nil {
return err
}

restyResult, err := resty.
SetDebug(a.Debug).
SetHostURL(a.WebhookURL).
R().
SetFormData(map[string]string{"payload": string(body)}).
Post(a.WebhookURL)

if err != nil {
return err
}

if restyResult.StatusCode() != 200 {
return errors.New(errorBadResponse)
}

return nil
}

// SendText sets message text and sends the message, a convenience wrapper for SetText and Send
func (a *Provider) SendText(text string) error {
a.SetText(text)
return a.Send()
}
43 changes: 43 additions & 0 deletions slack/slack_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package slack

import (
"os"
"testing"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

// generic helper for loading Slack API wrapper with Webhook URL
func getAPI(t *testing.T) Provider {
webhookURL := os.Getenv("SLACK_WEBHOOK_URL")
require.NotEmpty(t, webhookURL, "ParameterNotFound: SLACK_WEBHOOK_URL")

api := New(webhookURL)
api.Payload.IconEmoji = ":robot_face:"
api.Payload.Username = "Insights Slack - Unit Test"

return api
}

func TestSendEmpty(t *testing.T) {
api := getAPI(t)

err := api.Send()
assert.EqualError(t, err, errorEmptyText)
}

func TestSendNotEmpty(t *testing.T) {
api := getAPI(t)
api.SetText("TestSendNotEmpty Test Content")

err := api.Send()
assert.NoError(t, err, "unexpected err from api.Send()")
}

func TestSendText(t *testing.T) {
api := getAPI(t)

err := api.SendText("TestSendText Test Content")
assert.NoError(t, err, "unexpected err from api.SendText()")
}

0 comments on commit 9c9762e

Please sign in to comment.