-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split slack.go into slack_client.go, add new mocks & tests
Also renamed Concrete -> Default, Exist -> Exists
- Loading branch information
1 parent
b17eec2
commit 66a6f6f
Showing
6 changed files
with
169 additions
and
97 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
package webhooks | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/nlopes/slack" | ||
) | ||
|
||
const ( | ||
slackSuccessColour = "good" | ||
slackFailureColour = "danger" | ||
) | ||
|
||
//go:generate pegomock generate --use-experimental-model-gen --package mocks -o mocks/mock_slack_client.go SlackClient | ||
|
||
type SlackClient interface { | ||
AuthTest() error | ||
ChannelExists(channelName string) (bool, error) | ||
PostMessage(channel string, applyResult ApplyResult) error | ||
} | ||
|
||
type DefaultSlackClient struct { | ||
Slack *slack.Client | ||
} | ||
|
||
func NewSlackClient(token string) SlackClient { | ||
return &DefaultSlackClient{ | ||
Slack: slack.New(token), | ||
} | ||
} | ||
|
||
func (d *DefaultSlackClient) AuthTest() error { | ||
_, err := d.Slack.AuthTest() | ||
return err | ||
} | ||
|
||
func (d *DefaultSlackClient) ChannelExists(channelName string) (bool, error) { | ||
channels, err := d.Slack.GetChannels(true) | ||
if err != nil { | ||
return false, err | ||
} | ||
|
||
for _, channel := range channels { | ||
if channel.Name == channelName { | ||
return true, nil | ||
} | ||
} | ||
return false, nil | ||
} | ||
|
||
func (d *DefaultSlackClient) PostMessage(channel string, applyResult ApplyResult) error { | ||
params := slack.NewPostMessageParameters() | ||
params.Attachments = d.createAttachments(applyResult) | ||
params.AsUser = true | ||
params.EscapeText = false | ||
_, _, err := d.Slack.PostMessage(channel, "", params) | ||
return err | ||
} | ||
|
||
func (d *DefaultSlackClient) createAttachments(applyResult ApplyResult) []slack.Attachment { | ||
var colour string | ||
if applyResult.Success { | ||
colour = slackSuccessColour | ||
} else { | ||
colour = slackFailureColour | ||
} | ||
|
||
text := fmt.Sprintf("Applied in <%s|%s>.", applyResult.Pull.URL, applyResult.Repo.FullName) | ||
attachment := slack.Attachment{ | ||
Color: colour, | ||
Text: text, | ||
Fields: []slack.AttachmentField{ | ||
slack.AttachmentField{ | ||
Title: "Environment", | ||
Value: applyResult.Environment, | ||
Short: true, | ||
}, | ||
slack.AttachmentField{ | ||
Title: "User", | ||
Value: applyResult.User.Username, | ||
Short: true, | ||
}, | ||
}, | ||
} | ||
return []slack.Attachment{attachment} | ||
} |
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,65 @@ | ||
package webhooks_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/hootsuite/atlantis/server/events/models" | ||
"github.com/hootsuite/atlantis/server/events/webhooks" | ||
|
||
. "github.com/hootsuite/atlantis/testing" | ||
) | ||
|
||
const invalidSlackToken = "invalidtoken" | ||
|
||
func TestNewSlackClient_Success(t *testing.T) { | ||
t.Log("NewSlackClient should always return a non-nil client") | ||
client := webhooks.NewSlackClient(invalidSlackToken) | ||
Assert(t, client != nil, "SlackClient shouldn't be nil") | ||
|
||
client = webhooks.NewSlackClient("") | ||
Assert(t, client != nil, "SlackClient shouldn't be nil") | ||
} | ||
|
||
func TestAuthTest_Error(t *testing.T) { | ||
t.Log("When a SlackClient is created with an invalid token, AuthTest should error") | ||
client := webhooks.NewSlackClient(invalidSlackToken) | ||
err := client.AuthTest() | ||
Assert(t, err != nil, "expected error") | ||
} | ||
|
||
func TestChannelExists_Error(t *testing.T) { | ||
t.Log("When a SlackClient is created with an invalid token, ChannelExists should error") | ||
client := webhooks.NewSlackClient(invalidSlackToken) | ||
_, err := client.ChannelExists("somechannel") | ||
Assert(t, err != nil, "expected error") | ||
} | ||
|
||
func TestPostMessage_Error(t *testing.T) { | ||
t.Log("When a SlackClient is created with an invalid token, PostMessage should error") | ||
client := webhooks.NewSlackClient(invalidSlackToken) | ||
// todo: ?make this ApplyResult a fixture | ||
result := webhooks.ApplyResult{ | ||
Environment: "production", | ||
Repo: models.Repo{ | ||
CloneURL: "https://user:password@github.com/hootsuite/atlantis.git", | ||
FullName: "hootsuite/atlantis", | ||
Owner: "hootsuite", | ||
SanitizedCloneURL: "https://github.com/hootsuite/atlantis.git", | ||
Name: "atlantis", | ||
}, | ||
Pull: models.PullRequest{ | ||
Num: 1, | ||
HeadCommit: "16ca62f65c18ff456c6ef4cacc8d4826e264bb17", | ||
Branch: "branch", | ||
Author: "lkysow", | ||
URL: "url", | ||
BaseCommit: "8ed0280678d49d42cd286610aabcfceb5bb673c6", | ||
}, | ||
User: models.User{ | ||
Username: "lkysow", | ||
}, | ||
Success: true, | ||
} | ||
err := client.PostMessage("somechannel", result) | ||
Assert(t, err != nil, "expected error") | ||
} |
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