-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
123 lines (103 loc) · 3.63 KB
/
main.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
package main
import (
"fmt"
"os"
"regexp"
"strings"
"github.com/slack-go/slack"
)
type Commit struct {
url string
authorUsername string
authorEmail string
commitMessage string
}
type CommitMessageDetails struct {
domain string
service string
version string
environment string
}
func (c Commit) getCommitMessageTitle() string {
return strings.Split(c.commitMessage, "\n")[0]
}
func main() {
fmt.Println("Running actions-notify-slack-k8s")
slackClient := getSlackClient()
commit := buildCommit()
ok, commitMessage := isDeploymentCommit(commit)
if !ok {
fmt.Println("Commit is not a deployment commit:", commit.commitMessage)
return
}
// Example: "#deploys-mas-billing-prod"
slackChannel := "#deploys-" + commitMessage.domain + "-" + commitMessage.environment
message := fmt.Sprintf(":rocket: Deployed %s `%s` version `%s` to <%s|%s>", commitMessage.domain, commitMessage.service, commitMessage.version, commit.url, commitMessage.environment)
if commit.authorUsername != "" {
message += fmt.Sprintf(" by _%s_", commit.authorUsername)
}
ts := sendMessageToChannel(slackClient, slackChannel, message)
fmt.Println("ts:", ts)
// Add rest of the commit message if it exists
if len(strings.Split(commit.commitMessage, "\n")) > 1 {
// Remove the commit message header
commitBody := strings.TrimSpace(strings.Join(strings.Split(commit.commitMessage, "\n")[1:], "\n"))
// Send the rest of the commit message as a response to the original message using the thread ts
message = fmt.Sprintf("```%s```", commitBody)
sendMessageAsReply(slackClient, slackChannel, ts, message)
}
return
}
func getSlackClient() (client *slack.Client) {
accessToken := os.Getenv("SLACK_ACCESS_TOKEN")
client = slack.New(accessToken)
return client
}
func buildCommit() (commit Commit) {
commit = Commit{
url: os.Getenv("COMMIT_URL"),
authorUsername: os.Getenv("COMMIT_AUTHOR_USERNAME"),
authorEmail: os.Getenv("COMMIT_AUTHOR_EMAIL"),
commitMessage: os.Getenv("COMMIT_MESSAGE"),
}
return
}
func isDeploymentCommit(commit Commit) (ok bool, commitMessage CommitMessageDetails) {
// Examples: "Deployed mas-billing api-billing version v1.37.0 to prod"
// "Deployed mas-billing api-billing version v1.37.0-RC.2 to sta"
// "Deployed mas-billing api-billing version v1.37.0 to STA"
pattern := `Deployed\s(\w\S+)\s(\w\S+)\sversion\s(v\d+\.\d+\.\d+\S*)\sto\s((?i)prod|sta|dev)`
re := regexp.MustCompile(pattern)
matches := re.FindStringSubmatch(commit.getCommitMessageTitle())
if len(matches) == 0 {
ok = false
return
}
commitMessage = CommitMessageDetails{
domain: matches[1],
service: matches[2],
version: matches[3],
environment: matches[4],
}
fmt.Println("matched string:", matches[0])
fmt.Println("commitMessageDetails:", commitMessage)
return true, commitMessage
}
func sendMessageToChannel(client *slack.Client, slackChannel, message string) string {
respChannel, ts, err := client.PostMessage(slackChannel, slack.MsgOptionText(message, false), slack.MsgOptionAsUser(true))
if err != nil {
fmt.Println("got error posting message to slack channel:", err)
return ""
}
fmt.Println("message sent to channel", respChannel, "with id", ts)
return ts
}
func sendMessageAsReply(client *slack.Client, slackChannel, ts string, message string) {
respChannel, ts, err := client.PostMessage(slackChannel, slack.MsgOptionText(message, false), slack.MsgOptionAsUser(true), slack.MsgOptionTS(ts))
if err != nil {
fmt.Println("got error posting message reply to slack channel:", err)
return
}
fmt.Println("message reply sent to channel", respChannel, "with id", ts)
return
}