-
Notifications
You must be signed in to change notification settings - Fork 9
/
slack.go
178 lines (141 loc) · 4.22 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
// 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 (
"context"
"fmt"
"os"
"strings"
"time"
"github.com/slack-go/slack"
"k8s.io/utils/strings/slices"
"namespacelabs.dev/foundation/framework/secrets"
"namespacelabs.dev/foundation/framework/secrets/combined"
"namespacelabs.dev/foundation/internal/parsing"
"namespacelabs.dev/foundation/internal/planning/deploy"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/cfg"
)
func resolveSlackTokenAndChannel(ctx context.Context, env cfg.Context) (string, string, error) {
if DeployUpdateSlackChannel != "" {
return os.ExpandEnv(SlackToken), os.ExpandEnv(DeployUpdateSlackChannel), nil
}
if conf, ok := deploy.GetConfig(env.Configuration()); ok && conf.UpdateSlackChannel != "" {
ref, err := schema.StrictParsePackageRef(conf.SlackBotTokenSecretRef)
if err != nil {
return "", "", err
}
source, err := combined.NewCombinedSecrets(env)
if err != nil {
return "", "", err
}
pl := parsing.NewPackageLoader(env)
if _, err := pl.LoadByName(ctx, ref.AsPackageName()); err != nil {
return "", "", err
}
res, err := source.Load(ctx, pl.Seal(), &secrets.SecretLoadRequest{
SecretRef: ref,
})
if err != nil {
return "", "", err
}
return string(res.Value), conf.UpdateSlackChannel, nil
}
return "", "", nil
}
func renderSlackMessage(plan *schema.DeployPlan, start, end time.Time, message string, err error) []slack.Block {
var blocks []slack.Block
blocks = append(blocks, slack.NewHeaderBlock(slack.NewTextBlockObject(slack.PlainTextType, timeEmoji(end, err)+" "+deployLabel(end), true, false)))
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))
if message != "" {
blocks = append(blocks, slack.NewSectionBlock(slack.NewTextBlockObject(slack.PlainTextType, message, 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 {
label := "Deployed"
if end.IsZero() {
label = "Deploying"
}
if manual() {
return label + " manually"
}
return label
}
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
}
if manual() {
// TODO add actor from token
return " manually"
}
return ""
}
func manual() bool {
return os.Getenv("BUILDKITE_BUILD_URL") == ""
}