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

added notification support for discord #157

Merged
merged 24 commits into from
Oct 10, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
f891a4b
added notification support for discord
wryonik Jun 9, 2019
1edf4d8
removed changes from go.sum
wryonik Jun 9, 2019
fedfd99
changed discord icon
wryonik Jun 10, 2019
812cdc0
tried to improve notification structure
wryonik Jun 14, 2019
eed16a0
tried to implement interface for notifier
wryonik Jun 15, 2019
4c1f818
tried to implement interface for notifier. Some errors are still ther…
wryonik Jun 15, 2019
3ee5816
tried to implement interface for notifier. Some errors are still ther…
wryonik Jun 15, 2019
e946b1f
solved some errors. Some remains with sending payload
wryonik Jun 18, 2019
7e19304
enumeration of notification providers
wryonik Jun 19, 2019
7dea82c
completed notification for discord
wryonik Jun 20, 2019
5fae9bf
completed notification for discord
wryonik Jun 20, 2019
e7b4349
removed changes from go.sum
wryonik Jun 20, 2019
4a840a4
Review changes fixed
wryonik Aug 3, 2019
c485521
Merge branch 'master' into added_more_notification_channels
wryonik Aug 3, 2019
2603ae6
Formatting errors fixed
wryonik Aug 3, 2019
4b5eb5e
Changed notification functions such that now both the notification fo…
wryonik Aug 4, 2019
e34c8b3
Minor review changes
wryonik Aug 5, 2019
c7b7ac6
Minor review changes
wryonik Aug 26, 2019
d13953b
Minor changes
wryonik Sep 1, 2019
681cead
Review changes fixed
wryonik Sep 25, 2019
d091dbf
Removed discord test server link from example.config.toml
wryonik Sep 27, 2019
6215179
Some changes fixed
wryonik Oct 9, 2019
ee0bbdb
Minor changes fixes
wryonik Oct 10, 2019
6e5fd76
Documentation update
wryonik Oct 10, 2019
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
5 changes: 5 additions & 0 deletions _examples/example.config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,8 @@ ssh_key = "/home/vsts/.beast/secret.key"
url = "[email protected]:sdslabs/nonexistent.git"
name = "nonexistent"
branch = "nonexistent"

[[webhooks]]
Copy link
Member

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 to active

url = "https://discordapp.com/api/webhooks/615970326093758464/Dui7gVZVIaGvdys87I9O2Gn9Bx3ssNkdgkxSf3etEXN0ClxlNYSeTflbJd0obO81a_m1"
wryonik marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

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

Actual token kyu hai example toml me?

service_name= "discord"
status=true
9 changes: 7 additions & 2 deletions core/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ type BeastConfig struct {
AvailableSidecars []string `toml:"available_sidecars"`
GitRemote GitRemote `toml:"remote"`
JWTSecret string `toml:"jwt_secret"`
SlackWebHookURL string `toml:"slack_webhook"`
DiscordWebHookURL string `toml:"discord_webhook"`
Webhooks []Webhook `toml:webhook`
Copy link
Member

Choose a reason for hiding this comment

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

Name this NotificationWebhooks

TickerFrequency int `toml:"ticker_frequency"`

RemoteSyncPeriod time.Duration `toml:"-"`
Expand Down Expand Up @@ -216,6 +215,12 @@ func (config *GitRemote) ValidateGitConfig() error {
return nil
}

type Webhook struct {
Copy link
Member

Choose a reason for hiding this comment

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

NotificationWebhook

URL string `toml:"url"`
ServiceName string `toml:"service_name"`
Status bool `toml:"status"`
Copy link
Member

Choose a reason for hiding this comment

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

What is meant by Status - true or false. Change the name to something more sensible - Active

}

// From the path of the config file provided as an arguement this function
// loads the parse the config file and load it into the BeastConfig
// structure. After parsing it validates the data in the config file and returns
Expand Down
2 changes: 1 addition & 1 deletion core/manager/challenge.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,7 +530,7 @@ func StartUndeployChallenge(challengeName string, purge bool) error {
notify.SendNotification(notify.Success, msg)
}

log.Infof("Notification for the event sent to slack.")
log.Infof("Notification for the event sent.")
Copy link
Member

Choose a reason for hiding this comment

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

Remove the f from Infof

return err
}

Expand Down
2 changes: 1 addition & 1 deletion core/manager/pipeline.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,5 +412,5 @@ func StartDeployPipeline(challengeDir string, skipStage bool, skipCommit bool) {
notify.SendNotification(notify.Success, msg)
}

log.Debugf("%s: Notification sent to slack", challengeName)
log.Debugf("%s: Notification sent", challengeName)
}
56 changes: 25 additions & 31 deletions pkg/notify/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,42 +75,36 @@ func NewNotifier(URL string, ProviderType ProviderTypeEnum) Notifier {

func (req *Request) FillReqParams() error {
req.PostPayload = PostPayload{
Username: "Beast",
IconUrl: "https://i.ibb.co/sjC5dRY/beast-eye-39371.png",
Channel: "#beast",
Username: USERNAME,
IconUrl: ICON_URL,
Channel: CHANNEL_NAME,
}
return nil
}

func SendNotification(nType NotificationType, message string) error {
if config.Cfg.SlackWebHookURL != "" {
slackNotifier := NewNotifier(config.Cfg.SlackWebHookURL, SlackProvider)

err := slackNotifier.SendNotification(nType, message)
if err != nil {
log.Errorf("Error while sending notification to slack : %s", err)
fmt.Errorf("NOTIFICATION_SEND_ERROR: %s", err)
}

log.Infof("Notfication sent to slack.")
} else {
log.Warnf("No slack webhook url provided in beast config, cannot send notification.")
fmt.Errorf("No webhook URL in beast config.")
}

if config.Cfg.DiscordWebHookURL != "" {
discordNotifier := NewNotifier(config.Cfg.DiscordWebHookURL, DiscordProvider)

err := discordNotifier.SendNotification(nType, message)
if err != nil {
log.Errorf("Error while sending notification to discord : %s", err)
return fmt.Errorf("NOTIFICATION_SEND_ERROR: %s", err)
for _, webhook := range config.Cfg.Webhooks {
if webhook.ServiceName != "" || webhook.Status == 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.")
}

log.Infof("Notfication sent to discord.")
return nil
} else {
log.Warnf("No discord webhook url provided in beast config, cannot send notification.")
return fmt.Errorf("No webhook URL in beast config.")
}
return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ const (
WarningColor NotificationColor = "#FF4500"
SuccessColor NotificationColor = "#32CD32"
)

const (
Copy link
Member

Choose a reason for hiding this comment

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

Change the file name to constants.go only, since it is inside the notify package we dont need the noise of notification in the name.

USERNAME string = "Beast"
ICON_URL string = "https://i.ibb.co/sjC5dRY/beast-eye-39371.png"
CHANNEL_NAME string = "#beast"
)