-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
daemon: scaffold Slack notification delivery (#585)
Co-Authored-By: terryz21 <[email protected]>
- Loading branch information
Showing
10 changed files
with
284 additions
and
6 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,54 @@ | ||
package notify | ||
|
||
import ( | ||
"go.uber.org/multierr" | ||
) | ||
|
||
// Notifiers is a collection of notification targets | ||
type Notifiers []Notifier | ||
|
||
// Notify delivers a notification to all targets | ||
func (n Notifiers) Notify(msg string, opts Options) error { | ||
if len(n) == 0 { | ||
return nil | ||
} | ||
|
||
var errs error | ||
for _, notif := range n { | ||
errs = multierr.Append(errs, notif.Notify(msg, opts)) | ||
} | ||
return errs | ||
} | ||
|
||
// Notifier manages notifications | ||
type Notifier interface { | ||
Notify(string, Options) error | ||
} | ||
|
||
// Options is used to configure formatting of notifications | ||
type Options struct { | ||
Color Color | ||
} | ||
|
||
// Color is used to represent message color for different states (i.e success, fail) | ||
type Color string | ||
|
||
const ( | ||
// Green for success messages | ||
Green Color = "good" | ||
// Yellow for warning messages | ||
Yellow Color = "warning" | ||
// Red for error messages | ||
Red Color = "danger" | ||
) | ||
|
||
// 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"` | ||
} |
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,62 @@ | ||
package notify | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"errors" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
) | ||
|
||
// SlackNotifier represents slack notifications | ||
type SlackNotifier struct { | ||
hookURL string | ||
} | ||
|
||
// NewSlackNotifier creates a notifier with web hook url to slack channel. Passing | ||
// it an empty url makes it a no-op notifier. | ||
func NewSlackNotifier(webhookURL string) Notifier { | ||
return &SlackNotifier{ | ||
hookURL: webhookURL, | ||
} | ||
} | ||
|
||
// Notify sends the notification | ||
func (n *SlackNotifier) Notify(text string, options Options) error { | ||
if n.hookURL == "" { | ||
return nil | ||
} | ||
|
||
b, err := json.Marshal(MessageArray{ | ||
Attachments: []Message{ | ||
{ | ||
Text: fmt.Sprintf("*%s*", text), | ||
Color: colorToString(options.Color), | ||
}, | ||
}, | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("failed to encode request: %w", err) | ||
} | ||
|
||
resp, err := http.Post(n.hookURL, "application/json", bytes.NewBuffer(b)) | ||
if err != nil { | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
|
||
body, err := ioutil.ReadAll(resp.Body) | ||
if err != nil { | ||
return fmt.Errorf("failed to read response body: %w", err) | ||
} | ||
if resp.StatusCode < 200 || resp.StatusCode > 299 { | ||
return errors.New("http request rejected by Slack: " + string(body)) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func colorToString(color Color) string { | ||
return string(color) | ||
} |
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
Oops, something went wrong.