Skip to content

Commit

Permalink
Merge pull request #145 from keel-hq/feature/mattermost
Browse files Browse the repository at this point in the history
Feature/mattermost
  • Loading branch information
rusenask committed Feb 22, 2018
2 parents e808795 + c931672 commit 58e89c0
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 1 deletion.
5 changes: 5 additions & 0 deletions chart/keel/templates/deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,11 @@ spec:
- name: WEBHOOK_ENDPOINT
value: "{{ .Values.webhook.endpoint }}"
{{- end }}
{{- if .Values.mattermost.enabled }}
# Enable mattermost endpoint
- name: MATTERMOST_ENDPOINT
value: "{{ .Values.mattermost.endpoint }}"
{{- end }}
{{- if .Values.slack.enabled }}
- name: SLACK_TOKEN
value: "{{ .Values.slack.token }}"
Expand Down
4 changes: 4 additions & 0 deletions chart/keel/values.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,10 @@ hipchat:
user_name: ""
password: ""

mattermost:
enabled: false
endpoint: ""

# Keel service
# Enable to receive webhooks from Docker registries
service:
Expand Down
3 changes: 2 additions & 1 deletion cmd/keel/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ import (
"github.com/keel-hq/keel/util/codecs"
"github.com/keel-hq/keel/version"

// extensions
// notification extensions
_ "github.com/keel-hq/keel/extension/notification/hipchat"
_ "github.com/keel-hq/keel/extension/notification/mattermost"
_ "github.com/keel-hq/keel/extension/notification/slack"
_ "github.com/keel-hq/keel/extension/notification/webhook"

Expand Down
5 changes: 5 additions & 0 deletions constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ const (
EnvHipchatApprovalsBotName = "HIPCHAT_APPROVALS_BOT_NAME"
EnvHipchatApprovalsPasswort = "HIPCHAT_APPROVALS_PASSWORT"
EnvHipchatConnectionAttempts = "HIPCHAT_CONNECTION_ATTEMPTS"

// Mattermost webhook endpoint, see https://docs.mattermost.com/developer/webhooks-incoming.html
// for documentation on setting it up
EnvMattermostEndpoint = "MATTERMOST_ENDPOINT"
EnvMattermostName = "MATTERMOST_USERNAME"
)

// EnvNotificationLevel - minimum level for notifications, defaults to info
Expand Down
2 changes: 2 additions & 0 deletions deployment/deployment-norbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ spec:
# value: "111111_2222222"
# - name: HIPCHAT_APPROVALS_PASSWORT
# value: "pass"
# - name: MATTERMOST_ENDPOINT
# value: ""
name: keel
command: ["/bin/keel"]
ports:
Expand Down
2 changes: 2 additions & 0 deletions deployment/deployment-rbac.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ spec:
# value: "111111_2222222"
# - name: HIPCHAT_APPROVALS_PASSWORT
# value: "pass"
# - name: MATTERMOST_ENDPOINT
# value: ""
name: keel
command: ["/bin/keel"]
ports:
Expand Down
114 changes: 114 additions & 0 deletions extension/notification/mattermost/mattermost.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package mattermost

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
name string
client *http.Client
}

// Config represents the configuration of a Webhook Sender.
type Config struct {
Endpoint string
Name string
}

func init() {
notification.RegisterSender("mattermost", &sender{})
}

func (s *sender) Configure(config *notification.Config) (bool, error) {
// name in the notifications
s.name = "keel"
// Get configuration
var httpConfig Config

if os.Getenv(constants.EnvMattermostEndpoint) != "" {
httpConfig.Endpoint = os.Getenv(constants.EnvMattermostEndpoint)
} else {
return false, nil
}

if os.Getenv(constants.EnvMattermostName) != "" {
httpConfig.Name = os.Getenv(constants.EnvMattermostName)
}

// Validate endpoint URL.
if httpConfig.Endpoint == "" {
return false, nil
}

if httpConfig.Name != "" {
s.name = httpConfig.Name // setting default name
}
if _, err := url.ParseRequestURI(httpConfig.Endpoint); err != nil {
log.WithFields(log.Fields{
"endpoint": httpConfig.Endpoint,
"error": err,
}).Error("extension.notification.mattermost: endpoint invalid")
return false, fmt.Errorf("could not parse endpoint URL: %s", err)
}
s.endpoint = httpConfig.Endpoint

// Setup HTTP client.
transport := &http.Transport{}
s.client = &http.Client{
Transport: transport,
Timeout: timeout,
}

log.WithFields(log.Fields{
"name": "mattermost",
"endpoint": s.endpoint,
}).Info("extension.notification.mattermost: sender configured")

return true, nil
}

type notificationEnvelope struct {
Chanel string `json:"channel"`
Username string `json:"username"`
IconURL string `json:"icon_url"`
Text string `json:"text"`
}

func (s *sender) Send(event types.EventNotification) error {
// Marshal notification.
jsonNotification, err := json.Marshal(notificationEnvelope{
IconURL: "https://keel.sh/images/logo.png",
Username: s.name,
Text: fmt.Sprintf("#### %s \n %s", event.Type.String(), event.Message),
})
if err != nil {
return fmt.Errorf("could not marshal: %s", err)
}

// Send notification via HTTP POST.
resp, err := s.client.Post(s.endpoint, "application/json", bytes.NewBuffer(jsonNotification))
if err != nil || resp == nil || (resp.StatusCode != 200 && resp.StatusCode != 201) {
if resp != nil {
return fmt.Errorf("got status %d, expected 200/201", resp.StatusCode)
}
return err
}
defer resp.Body.Close()

return nil
}
2 changes: 2 additions & 0 deletions hack/deployment.sample.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ spec:
# value: "111111_2222222"
# - name: HIPCHAT_APPROVALS_PASSWORT
# value: "pass"
# - name: MATTERMOST_ENDPOINT
# value: ""
name: keel
command: ["/bin/keel"]
ports:
Expand Down

0 comments on commit 58e89c0

Please sign in to comment.