Skip to content

Commit

Permalink
Merge pull request #3821 from uselagoon/notification-build-warnings
Browse files Browse the repository at this point in the history
feat: update notifications with more information for build states
  • Loading branch information
tobybellwood authored Oct 25, 2024
2 parents c253d6d + 4bb4548 commit 8f91857
Show file tree
Hide file tree
Showing 37 changed files with 342 additions and 669 deletions.
1 change: 0 additions & 1 deletion services/logs2notifications/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ go 1.22
require (
github.com/aws/aws-sdk-go v1.55.5
github.com/cheshir/go-mq/v2 v2.0.1
github.com/dgrijalva/jwt-go v3.2.0+incompatible
github.com/matryer/try v0.0.0-20161228173917-9ac251b645a2
github.com/slack-go/slack v0.13.1
github.com/uselagoon/machinery v0.0.29
Expand Down
1 change: 0 additions & 1 deletion services/logs2notifications/go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,6 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/denverdino/aliyungo v0.0.0-20190125010748-a747050bb1ba/go.mod h1:dV8lFg6daOBZbT6/BDGIz6Y3WFGn8juu6G+CQ6LHtl0=
github.com/dgrijalva/jwt-go v0.0.0-20170104182250-a601269ab70c/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgrijalva/jwt-go v3.2.0+incompatible h1:7qlOGliEKZXTDg6OTjfoBKDXWrumCAMpl/TFQ4/5kLM=
github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ=
github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no=
github.com/dnaeon/go-vcr v1.0.1/go.mod h1:aBB1+wY4s93YsC3HHjMBMrwTj2R9FHDzUr9KyGc8n1E=
Expand Down
104 changes: 47 additions & 57 deletions services/logs2notifications/internal/handler/email_events.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"crypto/tls"
"fmt"
"log"
"regexp"
"strconv"
"strings"
"text/template"
Expand Down Expand Up @@ -96,17 +97,23 @@ func (h *Messaging) processEmailTemplates(notification *Notification) (string, s
mainHTMLTpl = `<code>{{.OpenshiftProject}}</code> not deleted.`
plainTextTpl = `[{{.ProjectName}}] {{.OpenshiftProject}} not deleted. {{.Error}}`
case "deployError":
mainHTMLTpl = `[{{.ProjectName}}] <code>{{.BranchName}}</code>{{ if ne .ShortSha "" }} ({{.ShortSha}}){{end}} Build <code>{{.BuildName}}</code> error.
mainHTMLTpl = `[{{.ProjectName}}] <code>{{.BranchName}}</code>{{ if ne .ShortSha "" }} ({{.ShortSha}}){{end}} Build <code>{{.BuildName}}</code> failed at build step ` + "`{{.BuildStep}}`" + `.
{{if ne .LogLink ""}} <a href="{{.LogLink}}">Logs</a>{{end}}`
plainTextTpl = `[{{.ProjectName}}] {{.BranchName}}{{ if ne .ShortSha "" }} ({{.ShortSha}}){{end}} Build {{.BuildName}} error.
plainTextTpl = `[{{.ProjectName}}] {{.BranchName}}{{ if ne .ShortSha "" }} ({{.ShortSha}}){{end}} Build {{.BuildName}} failed at build step {{.BuildStep}}.
{{if ne .LogLink ""}} [Logs]({{.LogLink}}){{end}}`
subject += fmt.Sprintf("[%s] %s Build %s error.",
notification.Meta.ProjectName,
notification.Meta.BranchName,
notification.Meta.BuildName,
)
case "deployFinished":
mainHTMLTpl = `[{{.ProjectName}}] <code>{{.BranchName}}</code>{{ if ne .ShortSha "" }} ({{.ShortSha}}){{end}} Build <code>{{.BuildName}}</code> complete. {{if ne .LogLink ""}}<a href="{{.LogLink}}">Logs</a>{{end}}
match, _ := regexp.MatchString(".*WithWarnings$", notification.Meta.BuildStep)
msg := "completed"
if match {
emoji = warningEmoji
msg = "completed with warnings, check the build log for more information"
}
mainHTMLTpl = `[{{.ProjectName}}] <code>{{.BranchName}}</code>{{ if ne .ShortSha "" }} ({{.ShortSha}}){{end}} Build <code>{{.BuildName}}</code> ` + msg + `. {{if ne .LogLink ""}}<a href="{{.LogLink}}">Logs</a>{{end}}
</p>
</div>
<div>
Expand All @@ -115,14 +122,15 @@ func (h *Messaging) processEmailTemplates(notification *Notification) (string, s
<li><a href="{{.Route}}">{{.Route}}</a></li>
{{range .Routes}}{{if ne . $.Route}}<li><a href="{{.}}">{{.}}</a></li>
{{end}}{{end}}</ul>`
plainTextTpl = `[{{.ProjectName}}] {{.BranchName}}{{ if ne .ShortSha "" }} ({{.ShortSha}}){{end}} Build {{.BuildName}} complete. {{if ne .LogLink ""}}[Logs]({{.LogLink}}){{end}}
plainTextTpl = `[{{.ProjectName}}] {{.BranchName}}{{ if ne .ShortSha "" }} ({{.ShortSha}}){{end}} Build {{.BuildName}} ` + msg + `. {{if ne .LogLink ""}}[Logs]({{.LogLink}}){{end}}
{{.Route}}
{{range .Routes}}{{if ne . $.Route}}{{.}}
{{end}}{{end}}`
subject += fmt.Sprintf("[%s] %s Build %s complete.",
subject += fmt.Sprintf("[%s] %s Build %s %s.",
notification.Meta.ProjectName,
notification.Meta.BranchName,
notification.Meta.BuildName,
msg,
)
case "problemNotification":
eventSplit := strings.Split(notification.Event, ":")
Expand Down Expand Up @@ -221,70 +229,52 @@ func getEmailEvent(msgEvent string) (string, string, string, error) {
}

var emailEvent = map[string]EventMap{
"github:pull_request:opened:handled": {Emoji: "ℹ️", Color: "#E8E8E8", Template: "mergeRequestOpened"},
"gitlab:merge_request:opened:handled": {Emoji: "ℹ️", Color: "#E8E8E8", Template: "mergeRequestOpened"},
"bitbucket:pullrequest:created:opened:handled": {Emoji: "ℹ️", Color: "#E8E8E8", Template: "mergeRequestOpened"}, //not in slack
"bitbucket:pullrequest:created:handled": {Emoji: "ℹ️", Color: "#E8E8E8", Template: "mergeRequestOpened"}, //not in teams
"github:pull_request:opened:handled": {Emoji: infoEmoji, Color: "#E8E8E8", Template: "mergeRequestOpened"},
"gitlab:merge_request:opened:handled": {Emoji: infoEmoji, Color: "#E8E8E8", Template: "mergeRequestOpened"},
"bitbucket:pullrequest:created:opened:handled": {Emoji: infoEmoji, Color: "#E8E8E8", Template: "mergeRequestOpened"}, //not in slack
"bitbucket:pullrequest:created:handled": {Emoji: infoEmoji, Color: "#E8E8E8", Template: "mergeRequestOpened"}, //not in teams

"github:pull_request:synchronize:handled": {Emoji: "ℹ️", Color: "#E8E8E8", Template: "mergeRequestUpdated"},
"gitlab:merge_request:updated:handled": {Emoji: "ℹ️", Color: "#E8E8E8", Template: "mergeRequestUpdated"},
"bitbucket:pullrequest:updated:opened:handled": {Emoji: "ℹ️", Color: "#E8E8E8", Template: "mergeRequestUpdated"}, //not in slack
"bitbucket:pullrequest:updated:handled": {Emoji: "ℹ️", Color: "#E8E8E8", Template: "mergeRequestUpdated"}, //not in teams
"github:pull_request:synchronize:handled": {Emoji: infoEmoji, Color: "#E8E8E8", Template: "mergeRequestUpdated"},
"gitlab:merge_request:updated:handled": {Emoji: infoEmoji, Color: "#E8E8E8", Template: "mergeRequestUpdated"},
"bitbucket:pullrequest:updated:opened:handled": {Emoji: infoEmoji, Color: "#E8E8E8", Template: "mergeRequestUpdated"}, //not in slack
"bitbucket:pullrequest:updated:handled": {Emoji: infoEmoji, Color: "#E8E8E8", Template: "mergeRequestUpdated"}, //not in teams

"github:pull_request:closed:handled": {Emoji: "ℹ️", Color: "#E8E8E8", Template: "mergeRequestClosed"},
"bitbucket:pullrequest:fulfilled:handled": {Emoji: "ℹ️", Color: "#E8E8E8", Template: "mergeRequestClosed"},
"bitbucket:pullrequest:rejected:handled": {Emoji: "ℹ️", Color: "#E8E8E8", Template: "mergeRequestClosed"},
"gitlab:merge_request:closed:handled": {Emoji: "ℹ️", Color: "#E8E8E8", Template: "mergeRequestClosed"},
"github:pull_request:closed:handled": {Emoji: infoEmoji, Color: "#E8E8E8", Template: "mergeRequestClosed"},
"bitbucket:pullrequest:fulfilled:handled": {Emoji: infoEmoji, Color: "#E8E8E8", Template: "mergeRequestClosed"},
"bitbucket:pullrequest:rejected:handled": {Emoji: infoEmoji, Color: "#E8E8E8", Template: "mergeRequestClosed"},
"gitlab:merge_request:closed:handled": {Emoji: infoEmoji, Color: "#E8E8E8", Template: "mergeRequestClosed"},

"github:delete:handled": {Emoji: "ℹ️", Color: "#E8E8E8", Template: "deleteEnvironment"},
"gitlab:remove:handled": {Emoji: "ℹ️", Color: "#E8E8E8", Template: "deleteEnvironment"}, //not in slack
"bitbucket:delete:handled": {Emoji: "ℹ️", Color: "#E8E8E8", Template: "deleteEnvironment"}, //not in slack
"api:deleteEnvironment": {Emoji: "ℹ️", Color: "#E8E8E8", Template: "deleteEnvironment"}, //not in teams
"github:delete:handled": {Emoji: infoEmoji, Color: "#E8E8E8", Template: "deleteEnvironment"},
"gitlab:remove:handled": {Emoji: infoEmoji, Color: "#E8E8E8", Template: "deleteEnvironment"}, //not in slack
"bitbucket:delete:handled": {Emoji: infoEmoji, Color: "#E8E8E8", Template: "deleteEnvironment"}, //not in slack
"api:deleteEnvironment": {Emoji: infoEmoji, Color: "#E8E8E8", Template: "deleteEnvironment"}, //not in teams

"github:push:handled": {Emoji: "ℹ️", Color: "#E8E8E8", Template: "repoPushHandled"},
"bitbucket:repo:push:handled": {Emoji: "ℹ️", Color: "#E8E8E8", Template: "repoPushHandled"},
"gitlab:push:handled": {Emoji: "ℹ️", Color: "#E8E8E8", Template: "repoPushHandled"},
"github:push:handled": {Emoji: infoEmoji, Color: "#E8E8E8", Template: "repoPushHandled"},
"bitbucket:repo:push:handled": {Emoji: infoEmoji, Color: "#E8E8E8", Template: "repoPushHandled"},
"gitlab:push:handled": {Emoji: infoEmoji, Color: "#E8E8E8", Template: "repoPushHandled"},

"github:push:skipped": {Emoji: "ℹ️", Color: "#E8E8E8", Template: "repoPushSkipped"},
"gitlab:push:skipped": {Emoji: "ℹ️", Color: "#E8E8E8", Template: "repoPushSkipped"},
"bitbucket:push:skipped": {Emoji: "ℹ️", Color: "#E8E8E8", Template: "repoPushSkipped"},
"github:push:skipped": {Emoji: infoEmoji, Color: "#E8E8E8", Template: "repoPushSkipped"},
"gitlab:push:skipped": {Emoji: infoEmoji, Color: "#E8E8E8", Template: "repoPushSkipped"},
"bitbucket:push:skipped": {Emoji: infoEmoji, Color: "#E8E8E8", Template: "repoPushSkipped"},

"api:deployEnvironmentLatest": {Emoji: "ℹ️", Color: "#E8E8E8", Template: "deployEnvironment"},
"api:deployEnvironmentBranch": {Emoji: "ℹ️", Color: "#E8E8E8", Template: "deployEnvironment"},
"api:deployEnvironmentLatest": {Emoji: infoEmoji, Color: "#E8E8E8", Template: "deployEnvironment"},
"api:deployEnvironmentBranch": {Emoji: infoEmoji, Color: "#E8E8E8", Template: "deployEnvironment"},

"task:deploy-openshift:finished": {Emoji: "✅", Color: "lawngreen", Template: "deployFinished"},
"task:remove-openshift-resources:finished": {Emoji: "✅", Color: "lawngreen", Template: "deployFinished"},
"task:builddeploy-openshift:complete": {Emoji: "✅", Color: "lawngreen", Template: "deployFinished"},
"task:builddeploy-kubernetes:complete": {Emoji: "✅", Color: "lawngreen", Template: "deployFinished"}, //not in teams
"task:deploy-openshift:finished": {Emoji: successEmoji, Color: "lawngreen", Template: "deployFinished"},
"task:remove-openshift-resources:finished": {Emoji: successEmoji, Color: "lawngreen", Template: "deployFinished"},
"task:builddeploy-openshift:complete": {Emoji: successEmoji, Color: "lawngreen", Template: "deployFinished"},
"task:builddeploy-kubernetes:complete": {Emoji: successEmoji, Color: "lawngreen", Template: "deployFinished"}, //not in teams

"task:remove-openshift:finished": {Emoji: "✅", Color: "lawngreen", Template: "removeFinished"},
"task:remove-kubernetes:finished": {Emoji: "✅", Color: "lawngreen", Template: "removeFinished"},
"task:remove-openshift:finished": {Emoji: successEmoji, Color: "lawngreen", Template: "removeFinished"},
"task:remove-kubernetes:finished": {Emoji: successEmoji, Color: "lawngreen", Template: "removeFinished"},

"task:remove-openshift:error": {Emoji: "‼️", Color: "red", Template: "deployError"},
"task:remove-kubernetes:error": {Emoji: "‼️", Color: "red", Template: "deployError"},
"task:builddeploy-kubernetes:failed": {Emoji: "‼️", Color: "red", Template: "deployError"}, //not in teams
"task:builddeploy-openshift:failed": {Emoji: "‼️", Color: "red", Template: "deployError"},

"github:pull_request:closed:CannotDeleteProductionEnvironment": {Emoji: "⚠️", Color: "gold", Template: "notDeleted"},
"github:push:CannotDeleteProductionEnvironment": {Emoji: "⚠️", Color: "gold", Template: "notDeleted"},
"bitbucket:repo:push:CannotDeleteProductionEnvironment": {Emoji: "⚠️", Color: "gold", Template: "notDeleted"},
"gitlab:push:CannotDeleteProductionEnvironment": {Emoji: "⚠️", Color: "gold", Template: "notDeleted"},

// deprecated
// "rest:remove:CannotDeleteProductionEnvironment": {Emoji: "⚠️", Color: "gold"},
// "rest:deploy:receive": {Emoji: "ℹ️", Color: "#E8E8E8"},
// "rest:remove:receive": {Emoji: "ℹ️", Color: "#E8E8E8"},
// "rest:promote:receive": {Emoji: "ℹ️", Color: "#E8E8E8"},
// "rest:pullrequest:deploy": {Emoji: "ℹ️", Color: "#E8E8E8"},
// "rest:pullrequest:remove": {Emoji: "ℹ️", Color: "#E8E8E8"},

// deprecated
// "task:deploy-openshift:error": {Emoji: "‼️", Color: "red", Template: "deployError"},
// "task:remove-openshift-resources:error": {Emoji: "‼️", Color: "red", Template: "deployError"},

// deprecated
// "task:deploy-openshift:retry": {Emoji: "⚠️", Color: "gold", Template: "removeRetry"},
// "task:remove-openshift:retry": {Emoji: "⚠️", Color: "gold", Template: "removeRetry"},
// "task:remove-kubernetes:retry": {Emoji: "⚠️", Color: "gold", Template: "removeRetry"},
// "task:remove-openshift-resources:retry": {Emoji: "⚠️", Color: "gold", Template: "removeRetry"},
"github:pull_request:closed:CannotDeleteProductionEnvironment": {Emoji: warningEmoji, Color: "gold", Template: "notDeleted"},
"github:push:CannotDeleteProductionEnvironment": {Emoji: warningEmoji, Color: "gold", Template: "notDeleted"},
"bitbucket:repo:push:CannotDeleteProductionEnvironment": {Emoji: warningEmoji, Color: "gold", Template: "notDeleted"},
"gitlab:push:CannotDeleteProductionEnvironment": {Emoji: warningEmoji, Color: "gold", Template: "notDeleted"},
}
9 changes: 9 additions & 0 deletions services/logs2notifications/internal/handler/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ type Notification struct {
Timestamp string `json:"timestamp"`
ShortSha string `json:"shortSha"`
BuildName string `json:"buildName"`
BuildPhase string `json:"buildPhase"`
BuildStep string `json:"buildStep"`
CommitURL string `json:"commitUrl"`
Environment string `json:"environment"`
EnvironmentID string `json:"environmentId"`
Expand Down Expand Up @@ -134,6 +136,13 @@ type EventMap struct {
Template string `json:"template"`
}

var (
warningEmoji string = "⚠️"
infoEmoji string = "ℹ️"
successEmoji string = "✅"
failEmoji string = "🛑"
)

// NewMessaging returns a messaging with config
func NewMessaging(config mq.Config,
lagoonAPI LagoonAPI,
Expand Down
Loading

0 comments on commit 8f91857

Please sign in to comment.