-
Notifications
You must be signed in to change notification settings - Fork 286
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #735 from hotpheex/discord_notifications
Discord notifications
Showing
6 changed files
with
182 additions
and
0 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
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,112 @@ | ||
package discord | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
"time" | ||
|
||
"github.com/keel-hq/keel/constants" | ||
"github.com/keel-hq/keel/extension/notification" | ||
"github.com/keel-hq/keel/types" | ||
|
||
log "github.com/sirupsen/logrus" | ||
) | ||
|
||
const timeout = 5 * time.Second | ||
|
||
type sender struct { | ||
endpoint string | ||
client *http.Client | ||
} | ||
|
||
// Config represents the configuration of a Discord Webhook Sender. | ||
type Config struct { | ||
Endpoint string | ||
} | ||
|
||
func init() { | ||
log.Info(0) | ||
notification.RegisterSender("discord", &sender{}) | ||
} | ||
|
||
func (s *sender) Configure(config *notification.Config) (bool, error) { | ||
log.Info(1) | ||
// Get configuration | ||
var httpConfig Config | ||
|
||
if os.Getenv(constants.EnvDiscordWebhookUrl) != "" { | ||
httpConfig.Endpoint = os.Getenv(constants.EnvDiscordWebhookUrl) | ||
} else { | ||
return false, nil | ||
} | ||
// Validate endpoint URL. | ||
if httpConfig.Endpoint == "" { | ||
return false, nil | ||
} | ||
if _, err := url.ParseRequestURI(httpConfig.Endpoint); err != nil { | ||
return false, fmt.Errorf("could not parse endpoint URL: %s", err) | ||
} | ||
s.endpoint = httpConfig.Endpoint | ||
|
||
// Setup HTTP client. | ||
s.client = &http.Client{ | ||
Transport: http.DefaultTransport, | ||
Timeout: timeout, | ||
} | ||
|
||
log.WithFields(log.Fields{ | ||
"name": "discord", | ||
"endpoint": s.endpoint, | ||
}).Info("extension.notification.discord: sender configured") | ||
log.Info(2) | ||
return true, nil | ||
} | ||
|
||
type DiscordMessage struct { | ||
Username string `json:"username"` | ||
Content string `json:"content"` | ||
Embeds []Embed `json:"embeds"` | ||
} | ||
|
||
type Embed struct { | ||
Title string `json:"title"` | ||
Description string `json:"description"` | ||
Footer Footer `json:"footer"` | ||
} | ||
|
||
type Footer struct { | ||
Text string `json:"text"` | ||
} | ||
|
||
func (s *sender) Send(event types.EventNotification) error { | ||
discordMessage := DiscordMessage{ | ||
Username: "Keel", | ||
Embeds: []Embed{ | ||
{ | ||
Title: fmt.Sprintf("%s: %s", event.Type.String(), event.Name), | ||
Description: event.Message, | ||
Footer: Footer{Text: event.Level.String()}, | ||
}, | ||
}, | ||
} | ||
|
||
jsonMessage, err := json.Marshal(discordMessage) | ||
if err != nil { | ||
return fmt.Errorf("could not marshal: %s", err) | ||
} | ||
|
||
resp, err := s.client.Post(s.endpoint, "application/json", bytes.NewBuffer(jsonMessage)) | ||
if err != nil || resp == nil || (resp.StatusCode != 200 && resp.StatusCode != 204) { | ||
if resp != nil { | ||
return fmt.Errorf("got status %d, expected 200/204", resp.StatusCode) | ||
} | ||
return err | ||
} | ||
defer resp.Body.Close() | ||
|
||
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,58 @@ | ||
package discord | ||
|
||
import ( | ||
"io" | ||
"net/http" | ||
"net/http/httptest" | ||
"strings" | ||
"testing" | ||
"time" | ||
|
||
"github.com/keel-hq/keel/types" | ||
) | ||
|
||
func TestDiscordWebhookRequest(t *testing.T) { | ||
currentTime := time.Now() | ||
handler := func(resp http.ResponseWriter, req *http.Request) { | ||
body, err := io.ReadAll(req.Body) | ||
if err != nil { | ||
t.Errorf("failed to parse body: %s", err) | ||
} | ||
|
||
bodyStr := string(body) | ||
|
||
if !strings.Contains(bodyStr, types.NotificationPreDeploymentUpdate.String()) { | ||
t.Errorf("missing deployment type") | ||
} | ||
|
||
if !strings.Contains(bodyStr, "debug") { | ||
t.Errorf("missing level") | ||
} | ||
|
||
if !strings.Contains(bodyStr, "update deployment") { | ||
t.Errorf("missing name") | ||
} | ||
if !strings.Contains(bodyStr, "message here") { | ||
t.Errorf("missing message") | ||
} | ||
|
||
t.Log(bodyStr) | ||
} | ||
|
||
// create test server with handler | ||
ts := httptest.NewServer(http.HandlerFunc(handler)) | ||
defer ts.Close() | ||
|
||
s := &sender{ | ||
endpoint: ts.URL, | ||
client: &http.Client{}, | ||
} | ||
|
||
s.Send(types.EventNotification{ | ||
Name: "update deployment", | ||
Message: "message here", | ||
CreatedAt: currentTime, | ||
Type: types.NotificationPreDeploymentUpdate, | ||
Level: types.LevelDebug, | ||
}) | ||
} |