-
Notifications
You must be signed in to change notification settings - Fork 0
/
entrypoint.go
90 lines (76 loc) · 3.1 KB
/
entrypoint.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
package clouddeployfunctions
import (
"context"
"os"
"time"
"github.com/sakajunquality/clouddeploy-functions/clouddeploy"
"github.com/sakajunquality/clouddeploy-functions/pubsub/approvals"
"github.com/sakajunquality/clouddeploy-functions/pubsub/operations"
"github.com/sakajunquality/clouddeploy-functions/slackbot"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
_ "github.com/GoogleCloudPlatform/berglas/pkg/auto"
)
type PubSubMessage struct {
ID string `json:"id,omitempty"`
Data []byte `json:"Data,omitempty"`
Attributes map[string]string `json:"attributes,omitempty"`
PublishTime time.Time `json:"PublishTime,omitempty"`
}
func init() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
zerolog.SetGlobalLevel(zerolog.InfoLevel)
}
// AutoPromote is an entrypoint of Cloud Functions subscribing `clouddeploy-operations` topic.
func AutoPromote(ctx context.Context, m PubSubMessage) error {
ops := operations.GetOperationByAttributes(m.Attributes)
if !ops.DispachAutoPromote() {
return nil
}
log.Debug().Msg("triggering AutoPromote")
rollout := &clouddeploy.Rollout{
ProjectNumber: ops.ProjectNumber,
PipelineID: ops.DeliveryPipelineId,
Location: ops.Location,
TargetID: ops.TargetId,
RolloutID: ops.RolloutId,
ReleaseID: ops.ReleaseId,
}
return clouddeploy.AutoPromote(ctx, rollout)
}
// NotifyApprovalRequestSlackSimple is an entrypoint of Cloud Functions subscribing `clouddeploy-approvals`
func NotifyApprovalRequestSlackSimple(ctx context.Context, m PubSubMessage) error {
approval := approvals.GetApprovalByAttributes(m.Attributes)
client := slackbot.NewSlackbot(os.Getenv("SLACK_TOKEN"), os.Getenv("SLACK_APPROVAL_CHANNEL"))
return client.NotifyApproval(ctx, approval)
}
// NotifySlackWithThread is an entrypoint of Cloud Functions subscribing `clouddeploy-operations` topic.
func NotifySlackWithThread(ctx context.Context, m PubSubMessage) error {
log.Debug().Msg("running NotifySlackWithThread")
client := slackbot.NewSlackbot(os.Getenv("SLACK_TOKEN"), os.Getenv("SLACK_CHANNEL"))
client.SetStateBucket(os.Getenv("SLACK_BOT_STATE_BUCKET"))
ops := operations.GetOperationByAttributes(m.Attributes)
switch ops.ResourceType {
case operations.ResourceTypeRelease:
if err := client.NotifyReleaseUpdate(ctx, ops); err != nil {
log.Error().Err(err).Msg("failed to NotifyReleaseUpdate")
return err
}
case operations.ResourceTypeRollout:
if err := client.NotifyRolloutUpdate(ctx, ops); err != nil {
log.Error().Err(err).Msg("failed to NotifyReleaseUpdate")
return err
}
default:
// not supported
}
return nil
}
// NotifySlackApprovalWithThread is an entrypoint of Cloud Functions subscribing `clouddeploy-approvals`
func NotifySlackApprovalWithThread(ctx context.Context, m PubSubMessage) error {
log.Debug().Msg("running NotifySlackApprovalWithThread")
approval := approvals.GetApprovalByAttributes(m.Attributes)
client := slackbot.NewSlackbot(os.Getenv("SLACK_TOKEN"), os.Getenv("SLACK_CHANNEL"))
client.SetStateBucket(os.Getenv("SLACK_BOT_STATE_BUCKET"))
return client.NotifyApprovalThread(ctx, approval)
}