-
Notifications
You must be signed in to change notification settings - Fork 9
/
slack.go
122 lines (97 loc) · 2.92 KB
/
slack.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package orchestration
import (
"fmt"
"os"
"strings"
"time"
"github.com/slack-go/slack"
"k8s.io/utils/strings/slices"
"namespacelabs.dev/foundation/schema"
)
func renderSlackMessage(plan *schema.DeployPlan, start, end time.Time, err error) []slack.Block {
var blocks []slack.Block
blocks = append(blocks, slack.NewHeaderBlock(slack.NewTextBlockObject(slack.PlainTextType, timeEmoji(end, err)+" "+deployLabel(end), true, false)))
if message := os.Getenv("BUILDKITE_MESSAGE"); message != "" {
blocks = append(blocks, slack.NewSectionBlock(slack.NewTextBlockObject(slack.PlainTextType, message, false, false), nil, nil))
}
blocks = append(blocks, slack.NewSectionBlock(slack.NewTextBlockObject(slack.MarkdownType,
fmt.Sprintf("%s *%s*%s with:",
workingLabel(end, err),
plan.GetEnvironment().GetName(),
maybeFrom()), false, false), nil, nil))
blocks = append(blocks, slack.NewSectionBlock(slack.NewTextBlockObject(slack.MarkdownType,
strings.Join(servers(plan), "\n"), false, false), nil, nil))
if !end.IsZero() {
blocks = append(blocks, slack.NewContextBlock("", slack.NewTextBlockObject(slack.MarkdownType, maybeTook(start, end), false, false)))
}
if err != nil {
blocks = append(blocks, slack.NewSectionBlock(slack.NewTextBlockObject(slack.MarkdownType,
fmt.Sprintf("*Error* %v", err), false, false), nil, nil))
}
return blocks
}
func deployLabel(end time.Time) string {
if end.IsZero() {
return "Deploying"
}
return "Deployed"
}
func workingLabel(end time.Time, err error) string {
if end.IsZero() {
return "Updating"
}
if err != nil {
return "Failed to update"
}
return "Updated"
}
func servers(plan *schema.DeployPlan) []string {
var fo []string
for _, ent := range plan.Stack.Entry {
srv := ent.GetPackageName().String()
if slices.Contains(plan.FocusServer, srv) {
srv = fmt.Sprintf("*%s*", srv)
}
fo = append(fo, " · "+srv)
}
return fo
}
func timeEmoji(end time.Time, err error) string {
if end.IsZero() {
return ":hourglass_flowing_sand:"
}
if err != nil {
return ":boom:"
}
return ":white_check_mark:"
}
func maybeTook(start, end time.Time) string {
if end.IsZero() {
return ""
}
return fmt.Sprintf("took %v", end.Sub(start))
}
func maybeFrom() string {
if bkUrl := os.Getenv("BUILDKITE_BUILD_URL"); bkUrl != "" {
name := os.Getenv("BUILDKITE_PIPELINE_NAME")
if name == "" {
name = "Buildkite"
} else {
if number := os.Getenv("BUILDKITE_BUILD_NUMBER"); number != "" {
name += " #" + number
}
}
if jobId := os.Getenv("BUILDKITE_JOB_ID"); jobId != "" {
bkUrl += "#" + jobId
}
from := fmt.Sprintf(" from <%s|%s>", bkUrl, name)
if author := os.Getenv("BUILDKITE_BUILD_CREATOR"); author != "" {
from += " by " + author
}
return from
}
return ""
}