Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ linters:

issues:
exclude-rules:
- path: plugin.go
linters:
- goconst
- path: server/manifest.go
linters:
- deadcode
Expand Down
64 changes: 64 additions & 0 deletions server/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"strconv"
"strings"
"sync"
"time"

"github.com/pkg/errors"

Expand Down Expand Up @@ -262,6 +263,12 @@ func (p *Plugin) handleNotification(body io.Reader, channel *TeamChannel) {
return
}

if isCodeBuild, messageNotification := p.isCodeBuildEvent(notification.Message); isCodeBuild {
p.API.LogDebug("Processing CodeBuild Event")
p.sendPostNotification(p.createSNSCodeBuildEventNotificationAttachment(notification.Subject, messageNotification), channel)
return
}

if isAlarm, messageNotification := p.isCloudWatchAlarm(notification.Message); isAlarm {
p.API.LogDebug("Processing CloudWatch alarm")
p.sendPostNotification(p.createSNSMessageNotificationAttachment(notification.Subject, messageNotification), channel)
Expand Down Expand Up @@ -332,6 +339,21 @@ func (p *Plugin) isCloudformationEvent(message string) (bool, SNSCloudformationE
return false, messageNotification
}

func (p *Plugin) isCodeBuildEvent(message string) (bool, SNSCodeBuildEventNotification) {
p.API.LogInfo("AWSSNS HandleNotification Info on CodeBuild event notification", "message", message)

var messageNotification SNSCodeBuildEventNotification
if err := json.Unmarshal([]byte(message), &messageNotification); err != nil {
p.API.LogError(
"AWSSNS HandleNotification Decode Error on CodeBuild event notification",
"err", err.Error(),
"message", message)
return false, messageNotification
}

return len(messageNotification.Source) > 0 && messageNotification.Source == "aws.codebuild", messageNotification
}

func (p *Plugin) createSNSRdsEventAttachment(subject string, messageNotification SNSRdsEventNotification) model.SlackAttachment {
p.API.LogDebug("AWSSNS HandleNotification RDS Event", "MESSAGE", subject)

Expand Down Expand Up @@ -372,6 +394,48 @@ func (p *Plugin) createSNSCloudformationEventAttachment(subject string, messageN
return attachment
}

func (p *Plugin) createSNSCodeBuildEventNotificationAttachment(subject string, messageNotification SNSCodeBuildEventNotification) model.SlackAttachment {
var fields []*model.SlackAttachmentField

fields = addFields(fields, "Type", messageNotification.DetailType, true)
fields = addFields(fields, "Project Name", messageNotification.Detail.ProjectName, true)
fields = addFields(fields, "Time", messageNotification.Time.Format(time.DateTime), true)
fields = addFields(fields, "Build ID", messageNotification.Detail.BuildID, true)

if messageNotification.DetailType == "CodeBuild Build State Change" {
fields = addFields(fields, "Build Status", messageNotification.Detail.BuildStatus, true)
}

if messageNotification.DetailType == "CodeBuild Build Phase Change" {
fields = addFields(fields, "Current Phase", messageNotification.Detail.CurrentPhase, true)
fields = addFields(fields, "Current Phase Context", messageNotification.Detail.CurrentPhaseContext, true)
fields = addFields(fields, "Completed Phase", messageNotification.Detail.CompletedPhase, true)
fields = addFields(fields, "Completed Phase Context", messageNotification.Detail.CompletedPhaseContext, true)
}

var msgColor string
if messageNotification.DetailType == "CodeBuild Build State Change" {
switch messageNotification.Detail.BuildStatus {
case "FAILED", "FAULT", "TIMED_OUT":
msgColor = "#FF0000"
case "SUCCEEDED":
msgColor = "#008000"
case "IN_PROGRESS", "QUEUED":
msgColor = "#008000"
case "STOPPED":
msgColor = "#AAAAAA"
}
}

attachment := model.SlackAttachment{
Title: subject,
Fields: fields,
Color: msgColor,
}

return attachment
}

func (p *Plugin) createSNSMessageNotificationAttachment(subject string, messageNotification SNSMessageNotification) model.SlackAttachment {
p.API.LogDebug("AWSSNS HandleNotification", "MESSAGE", subject)
var fields []*model.SlackAttachmentField
Expand Down
63 changes: 63 additions & 0 deletions server/sns.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,66 @@ type SNSCloudformationEventNotification struct {
StackName string `json:"StackName"`
ClientRequestToken string `json:"ClientRequestToken"`
}

type SNSCodeBuildEventNotification struct {
AccountID string `json:"account"`
Region string `json:"region"`
DetailType string `json:"detailType"`
Source string `json:"source"`
Time time.Time `json:"time"`
Resources []string `json:"resources"`
Detail struct {
ProjectName string `json:"project-name"`
BuildID string `json:"build-id"`
BuildStatus string `json:"build-status,omitempty"`
CurrentPhase string `json:"current-phase,omitempty"`
CurrentPhaseContext string `json:"current-phase-context,omitempty"`
CompletedPhaseStatus string `json:"completed-phase-status,omitempty"`
CompletedPhase string `json:"completed-phase,omitempty"`
CompletedPhaseContext string `json:"completed-phase-context,omitempty"`
CompletedPhaseDuration float32 `json:"completed-phase-duration-seconds,omitempty"`
CompletedPhaseStart string `json:"completed-phase-start,omitempty"`
CompletedPhaseEnd string `json:"completed-phase-end,omitempty"`
AdditionalInformation struct {
Initiator string `json:"initiator"`
BuildNumber float32 `json:"build-number,omitempty"`
BuildStartTime string `json:"build-start-time"`
BuildComplete bool `json:"build-complete"`
Timeout float32 `json:"timeout-in-minutes"`
Artifact struct {
MD5Sum string `json:"md5sum,omitempty"`
SHA256Sum string `json:"sha256sum,omitempty"`
Location string `json:"location"`
} `json:"artifact"`
Environment struct {
Image string `json:"image"`
PrivilegedMode bool `json:"privileged-mode"`
ComputeType string `json:"compute-type"`
Type string `json:"type"`
EnvironmentVariables []struct {
Name string `json:"name"`
Type string `json:"type"`
Value string `json:"value"`
} `json:"environment-variables"`
} `json:"environment"`
Source struct {
Location string `json:"location"`
Type string `json:"type"`
} `json:"source"`
SourceVersion string `json:"source-version"`
Logs struct {
GroupName string `json:"group-name"`
StreamName string `json:"stream-name"`
DeepLink string `json:"deep-link"`
} `json:"logs"`
Phases []struct {
PhaseContext []interface{} `json:"phase-context,omitempty"`
StartTime string `json:"start-time"`
EndTime string `json:"end-time,omitempty"`
Duration float32 `json:"duration-in-seconds,omitempty"`
PhaseType string `json:"phase-type"`
PhaseStatus string `json:"phase-status,omitempty"`
} `json:"phases"`
} `json:"additional-information"`
} `json:"detail"`
}