-
Notifications
You must be signed in to change notification settings - Fork 18
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
Changes from 9 commits
39dc311
7035d5f
1cf97e3
2fe3e3a
6fcd03e
226802f
68e822e
662eee8
d0c80de
b366ffc
158c283
4820b2d
da5973c
6d857f7
7684d68
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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 | ||||||
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) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lowercase for go error convention:
Suggested change
|
||||||
} | ||||||
|
||||||
return nil | ||||||
} | ||||||
|
||||||
func colorToString(color Color) string { | ||||||
return string(color) | ||||||
} |
This file was deleted.
There was a problem hiding this comment.
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 😋