Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ work_directory: &work_directory
resource_class: &resource_class
resource_class: small

release_filter: &release_filter
filters:
branches:
only: non-existent-branch
tags:
only: /^v[0-9\.]+$/

executors:
docker-executor:
docker:
Expand Down Expand Up @@ -116,15 +123,12 @@ workflows:
- publish_release:
context:
- docker-credentials
filters:
branches:
only: non-existent-branch
tags:
only: /^v[0-9\.]+$/
<<: *release_filter
- notify:
context:
- docker-credentials
- slack-webhook
<<: *release_filter
requires:
- publish_release
pr_check:
Expand Down
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
# Changelog

## [Unreleased]
### Changed
- feat: Quit with non-zero exit code when API call to Slack fails([#35](https://github.com/devatherock/simple-slack/issues/35))

## [0.6.0] - 2021-02-14
### Added
- feat: Used `VELA_BUILD_STATUS` environment variable to choose message highlight color in vela
- feat: Added support for sprig functions within the text template([#32](https://github.com/devatherock/simple-slack/issues/32))
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,10 @@ func run(context *cli.Context) error {
defer res.Body.Close()
log.Println("Message posted to webhook with http status", res.StatusCode)

if res.StatusCode > 399 {
return errors.New("HTTP request to Slack failed")
}

return nil
}

Expand Down
51 changes: 50 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ func TestRunApp(test *testing.T) {
var capturedRequest []byte
testServer := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
capturedRequest, _ = ioutil.ReadAll(request.Body)
writer.WriteHeader(400)
writer.Header().Set("Content-Type", "application/json")
fmt.Fprintln(writer, `{"success":true}`)
}))
defer testServer.Close()
setEnvironmentVariable(test, "WEBHOOK", testServer.URL)
Expand Down Expand Up @@ -134,6 +135,54 @@ func TestRun(test *testing.T) {
assert.Equal(test, "general", jsonRequest["channel"])
}

func TestRunSlackCallFailed(test *testing.T) {
cases := []int{
400,
500,
}

for _, statusCode := range cases {
// Test HTTP server
var capturedRequest []byte
testServer := httptest.NewServer(http.HandlerFunc(func(writer http.ResponseWriter, request *http.Request) {
capturedRequest, _ = ioutil.ReadAll(request.Body)
writer.WriteHeader(statusCode)
}))
defer testServer.Close()

set := flag.NewFlagSet("test", 0)
set.String("text", "Build failed!", "")
set.String("color", "red", "")
set.String("title", "Build notification", "")
set.String("channel", "general", "")
set.String("webhook", testServer.URL, "")

context := cli.NewContext(nil, set, nil)
actual := run(context)

// Verify error
assert.NotNil(test, actual)
assert.Equal(test, "HTTP request to Slack failed", actual.Error())

// Verify request
jsonRequest := make(map[string]interface{})
json.Unmarshal(capturedRequest, &jsonRequest)
assert.Equal(test, 2, len(jsonRequest))

var attachments []interface{}
attachments = jsonRequest["attachments"].([]interface{})
var attachment map[string]interface{}
attachment = attachments[0].(map[string]interface{})

assert.Equal(test, 1, len(attachments))
assert.Equal(test, 3, len(attachment))
assert.Equal(test, "Build failed!", attachment["text"])
assert.Equal(test, "red", attachment["color"])
assert.Equal(test, "Build notification", attachment["title"])
assert.Equal(test, "general", jsonRequest["channel"])
}
}

func TestBuildPayload(test *testing.T) {
cases := []struct {
parameters map[string]string
Expand Down