-
Notifications
You must be signed in to change notification settings - Fork 6
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
added notification support for discord #157
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
f891a4b
added notification support for discord
wryonik 1edf4d8
removed changes from go.sum
wryonik fedfd99
changed discord icon
wryonik 812cdc0
tried to improve notification structure
wryonik eed16a0
tried to implement interface for notifier
wryonik 4c1f818
tried to implement interface for notifier. Some errors are still ther…
wryonik 3ee5816
tried to implement interface for notifier. Some errors are still ther…
wryonik e946b1f
solved some errors. Some remains with sending payload
wryonik 7e19304
enumeration of notification providers
wryonik 7dea82c
completed notification for discord
wryonik 5fae9bf
completed notification for discord
wryonik e7b4349
removed changes from go.sum
wryonik 4a840a4
Review changes fixed
wryonik c485521
Merge branch 'master' into added_more_notification_channels
wryonik 2603ae6
Formatting errors fixed
wryonik 4b5eb5e
Changed notification functions such that now both the notification fo…
wryonik e34c8b3
Minor review changes
wryonik c7b7ac6
Minor review changes
wryonik d13953b
Minor changes
wryonik 681cead
Review changes fixed
wryonik d091dbf
Removed discord test server link from example.config.toml
wryonik 6215179
Some changes fixed
wryonik ee0bbdb
Minor changes fixes
wryonik 6e5fd76
Documentation update
wryonik File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -28,3 +28,8 @@ ssh_key = "/home/vsts/.beast/secret.key" | |
url = "[email protected]:sdslabs/nonexistent.git" | ||
name = "nonexistent" | ||
branch = "nonexistent" | ||
|
||
[[webhooks]] | ||
url = "" | ||
service_name= "discord" | ||
active=true |
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
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
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,68 @@ | ||
package notify | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"time" | ||
) | ||
|
||
type DiscordNotificationProvider struct { | ||
Request | ||
} | ||
|
||
func (d *DiscordNotificationProvider) SendNotification(nType NotificationType, msg string) error { | ||
if d.Request.WebHookURL == "" { | ||
return fmt.Errorf("Need a WebHookURL to send notification.") | ||
} | ||
|
||
d.Request.FillReqParams() | ||
|
||
nAttachment := Attachment{ | ||
AuthorName: "Beast Notifier", | ||
AuthorLink: "https://backdoor.sdslabs.co", | ||
Footer: "Beast Discord API", | ||
FooterIcon: "https://discordapp.com/assets/e05ead6e6ebc08df9291738d0aa6986d.png", | ||
Timestamp: time.Now().Unix(), | ||
Text: msg, | ||
} | ||
|
||
switch nType { | ||
case Success: | ||
nAttachment.Color = SuccessColor | ||
nAttachment.Title = "Beast Deployment Success" | ||
break | ||
case Error: | ||
nAttachment.Color = ErrorColor | ||
nAttachment.Title = "Beast Deployment Error" | ||
break | ||
} | ||
|
||
d.Request.PostPayload.Attachments = []Attachment{nAttachment} | ||
|
||
if d.Request.PostPayload.Channel == "" || d.Request.PostPayload.Username == "" { | ||
return fmt.Errorf("Username and Channel required to send the notification.") | ||
} | ||
|
||
payload, err := json.Marshal(d.PostPayload) | ||
if err != nil { | ||
return fmt.Errorf("Error while converting payload to JSON : %s", err) | ||
} | ||
|
||
payloadReader := bytes.NewReader(payload) | ||
req, err := http.NewRequest("POST", d.Request.WebHookURL, payloadReader) | ||
if err != nil { | ||
return fmt.Errorf("Error while connecting to webhook url host : %s", err) | ||
} | ||
|
||
req.Header.Set("Content-Type", "application/json") | ||
client := http.Client{} | ||
_, err = client.Do(req) | ||
|
||
if err != nil { | ||
return fmt.Errorf("Error while posting payload for notification : %s", err) | ||
} | ||
|
||
return nil | ||
} |
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,116 @@ | ||
package notify | ||
|
||
import ( | ||
"fmt" | ||
"net/url" | ||
|
||
"github.com/sdslabs/beastv4/core/config" | ||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
type Attachment struct { | ||
Fallback string `json:"fallback,omitempty"` | ||
Color NotificationColor `json:"color,omitempty"` | ||
PreText string `json:"pretext,omitempty"` | ||
AuthorName string `json:"author_name,omitempty"` | ||
AuthorLink string `json:"author_link,omitempty"` | ||
AuthorIcon string `json:"author_icon,omitempty"` | ||
Title string `json:"title,omitempty"` | ||
TitleLink string `json:"title_link,omitempty"` | ||
Text string `json:"text,omitempty"` | ||
ImageUrl string `json:"image_url,omitempty"` | ||
Footer string `json:"footer,omitempty"` | ||
FooterIcon string `json:"footer_icon,omitempty"` | ||
Timestamp int64 `json:"ts,omitempty"` | ||
MarkdownIn []string `json:"mrkdwn_in,omitempty"` | ||
CallbackID string `json:"callback_id,omitempty"` | ||
ThumbnailUrl string `json:"thumb_url,omitempty"` | ||
} | ||
|
||
type PostPayload struct { | ||
Parse string `json:"parse,omitempty"` | ||
Username string `json:"username,omitempty"` | ||
IconUrl string `json:"icon_url,omitempty"` | ||
IconEmoji string `json:"icon_emoji,omitempty"` | ||
Channel string `json:"channel,omitempty"` | ||
Text string `json:"text,omitempty"` | ||
Attachments []Attachment `json:"attachments,omitempty"` | ||
Markdown bool `json:"mrkdwn,omitempty"` | ||
} | ||
|
||
type Notifier interface { | ||
SendNotification(nType NotificationType, msg string) error | ||
} | ||
|
||
type Request struct { | ||
WebHookURL string | ||
PostPayload | ||
wryonik marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
type ProviderTypeEnum int | ||
|
||
const ( | ||
DiscordProvider ProviderTypeEnum = 1 + iota | ||
SlackProvider | ||
) | ||
|
||
//In the Discord notification provider it was using the same payload which was used for slack. | ||
//By writing "/slack" in the discord WebHookURL, it execute Slack-Compatible Webhook | ||
func NewNotifier(URL string, ProviderType ProviderTypeEnum) Notifier { | ||
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. Validate URL here |
||
url, err := url.ParseRequestURI(URL) | ||
if url == nil || err != nil { | ||
log.Error("Invalid notification webhook URL") | ||
return nil | ||
} | ||
switch ProviderType { | ||
case SlackProvider: | ||
return &SlackNotificationProvider{ | ||
Request{ | ||
WebHookURL: url.String(), | ||
}, | ||
} | ||
case DiscordProvider: | ||
return &DiscordNotificationProvider{ | ||
Request{ | ||
WebHookURL: url.String() + "/slack", | ||
}, | ||
} | ||
} | ||
return nil | ||
} | ||
|
||
func (req *Request) FillReqParams() error { | ||
req.PostPayload = PostPayload{ | ||
Username: USERNAME, | ||
IconUrl: ICON_URL, | ||
Channel: CHANNEL_NAME, | ||
} | ||
return nil | ||
} | ||
|
||
func SendNotification(nType NotificationType, message string) error { | ||
for _, webhook := range config.Cfg.NotificationWebhooks { | ||
if webhook.ServiceName != "" || webhook.Active == true { | ||
var Provider ProviderTypeEnum | ||
if webhook.ServiceName == "slack" { | ||
Provider = SlackProvider | ||
} | ||
if webhook.ServiceName == "discord" { | ||
Provider = DiscordProvider | ||
} | ||
Notifier := NewNotifier(webhook.URL, Provider) | ||
|
||
err := Notifier.SendNotification(nType, message) | ||
if err != nil { | ||
log.Errorf("Error while sending notification to %s : %s", webhook.ServiceName, err) | ||
fmt.Errorf("NOTIFICATION_SEND_ERROR: %s", err) | ||
} | ||
|
||
log.Infof("Notfication sent to %s.", webhook.ServiceName) | ||
} else { | ||
log.Warnf("No %s webhook url provided in beast config, cannot send notification.", webhook.ServiceName) | ||
fmt.Errorf("No webhook URL in beast config.") | ||
} | ||
} | ||
return nil | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Add example for slack too.
Change the field
status
toactive