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

daemon: send slack notifications (#281) #585

Merged
merged 15 commits into from
Oct 3, 2019
Merged
Show file tree
Hide file tree
Changes from 9 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
Binary file removed .static/inertia-init.png
Binary file not shown.
Binary file removed .static/inertia-with-name.png
Binary file not shown.
2 changes: 2 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ mocks:
./daemon/inertiad/project/deployment.go Deployer
counterfeiter -o ./daemon/inertiad/build/mocks/builder.go \
./daemon/inertiad/build/builder.go ContainerBuilder
counterfeiter -o ./daemon/inertiad/notifier/mocks/notifier.go \
./daemon/inertiad/notifier/notifier.go Notifier

## scripts: recompile script assets
.PHONY: scripts
Expand Down
4 changes: 2 additions & 2 deletions client/internal/compiled.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

110 changes: 110 additions & 0 deletions daemon/inertiad/notifier/mocks/notifier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

104 changes: 104 additions & 0 deletions daemon/inertiad/notifier/notifier.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package notifier

import (
"bytes"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
)

// Notifier manages notifications
type Notifier interface {
Notify(string) error
}

// SlackNotifier represents slack notifications
type SlackNotifier struct {
hookURL string
}

// NewNotifier creates a notifier with web hook url to slack channel
func NewNotifier(webhook string) *SlackNotifier {
url := webhook

if webhook == "test" { // temporary for testing
url = "https://hooks.slack.com/services/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"
}
// os.Getenv("SLACK_URL")

n := &SlackNotifier{
hookURL: url,
}

return n
}

// Color is used to represent message color for different states (i.e success, fail)
type Color string

const (
// Green when build successful
Green Color = "good"
// Yellow ...
Yellow Color = "warning"
// Red when build unsuccessful
Red Color = "danger"
)

// NotifyOptions is used to configure formatting of notifications
type NotifyOptions struct {
Color Color
Warning bool
}

// MessageArray builds the json message to be posted to webhook
type MessageArray struct {
Attachments []Message `json:"attachments"`
}

// Message builds the attachments content of Message
type Message struct {
Text string `json:"text"`
Color string `json:"color"`
}

// Notify sends the notification
func (n *SlackNotifier) Notify(text string, options *NotifyOptions) error {
// check if url is empty
Copy link
Member

Choose a reason for hiding this comment

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

only really need to leave comments if the code is not self-explanatory 😋

Suggested change
// check if url is empty
// check if url is empty

if n.hookURL == "" {
return nil
}

msg := MessageArray{
Attachments: []Message{
{
Text: "*" + text + "*",
Color: colorToString(options.Color),
},
},
}
bytesRepresentation, err := json.Marshal(msg)
if err != nil {
return fmt.Errorf("failed to encode request: %s", err.Error())
}

resp, err := http.Post(n.hookURL, "application/json", bytes.NewBuffer(bytesRepresentation))
if err != nil {
return err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
Copy link
Member

Choose a reason for hiding this comment

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

should check error here

bodyString := string(body)

if resp.StatusCode < 200 || resp.StatusCode > 299 {
return errors.New("Http request rejected by Slack. Error: " + bodyString)
Copy link
Member

Choose a reason for hiding this comment

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

lowercase for go error convention:

Suggested change
return errors.New("Http request rejected by Slack. Error: " + bodyString)
return errors.New("http request rejected by Slack. Error: " + bodyString)

}

return nil
}

func colorToString(color Color) string {
return string(color)
}
27 changes: 27 additions & 0 deletions daemon/inertiad/project/deployment.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import (
"github.com/ubclaunchpad/inertia/daemon/inertiad/containers"
"github.com/ubclaunchpad/inertia/daemon/inertiad/crypto"
"github.com/ubclaunchpad/inertia/daemon/inertiad/git"
"github.com/ubclaunchpad/inertia/daemon/inertiad/notifier"
gogit "gopkg.in/src-d/go-git.v4"
"gopkg.in/src-d/go-git.v4/plumbing/transport/ssh"
)
Expand Down Expand Up @@ -182,12 +183,38 @@ func (d *Deployment) Deploy(
fmt.Fprintln(out, "Continuing...")
}

// Send build start slack notification
notification := notifier.NewNotifier("test")
notifyErr := notification.Notify("Build started", &notifier.NotifyOptions{
Color: notifier.Green,
Warning: true,
})
if notifyErr != nil {
return func() error { return nil }, notifyErr
}

// Build project
deploy, err := d.builder.Build(strings.ToLower(d.buildType), *conf, cli, out)
if err != nil {
notifyErr = notification.Notify("Build error", &notifier.NotifyOptions{
Color: notifier.Red,
Warning: true,
})
if notifyErr != nil {
fmt.Fprintln(out, notifyErr)
}
return func() error { return nil }, err
}

// Send build complete slack notification
notifyErr = notification.Notify("Build completed", &notifier.NotifyOptions{
Color: "good",
Warning: true,
})
if notifyErr != nil {
return func() error { return nil }, notifyErr
}

// Deploy
return func() error {
d.active = true
Expand Down
1 change: 1 addition & 0 deletions daemon/inertiad/project/deployment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ func TestGetStatusIntegration(t *testing.T) {
builder: fakeBuilder,
}
status, err := deployment.GetStatus(cli)

assert.NoError(t, err)
assert.False(t, status.BuildContainerActive)
assert.Equal(t, "test", status.BuildType)
Expand Down
27 changes: 0 additions & 27 deletions test/keys/id_rsa

This file was deleted.