-
Notifications
You must be signed in to change notification settings - Fork 2
/
slackcmds.go
128 lines (110 loc) · 2.8 KB
/
slackcmds.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
package kubeci
import (
"encoding/json"
"fmt"
"io"
"net/http"
"os"
"strings"
"github.com/mattn/go-shellwords"
"github.com/slack-go/slack"
)
type slackCmds struct {
signingSecret string
client *slack.Client
ciFilePath string
templates TemplateSet
}
func (scs *slackCmds) setupCmd(org, repo string, args []string) *slack.Msg {
if len(args) > 2 {
msg := "bad wrguments: /ci-setup org/repo template [branch]"
return &slack.Msg{
Text: strings.Join(args, msg),
}
}
if len(args) == 0 {
msg := fmt.Sprintf("You must select a template:\n%s", scs.templates.Help())
return &slack.Msg{
Text: strings.Join(args, msg),
}
}
branch := "master"
if len(args) == 2 {
branch = args[2]
}
return &slack.Msg{
Text: fmt.Sprintf("TODO push %s to %s/%s (branch: %s)", scs.ciFilePath, org, repo, branch),
}
}
func NewSlack(tokenFile, signingSecretFile string, ciFilePath string, templates TemplateSet) (*slackCmds, error) {
token, err := os.ReadFile(tokenFile)
if err != nil {
return nil, fmt.Errorf("couldn't read slack token, %w", err)
}
signingSecret, err := os.ReadFile(signingSecretFile)
if err != nil {
return nil, fmt.Errorf("couldn't read slack signing secret, %w", err)
}
cli := slack.New(strings.TrimSpace(string(token)))
return &slackCmds{
client: cli,
signingSecret: strings.TrimSpace(string(signingSecret)),
templates: templates,
ciFilePath: ciFilePath,
}, nil
}
func (scs *slackCmds) ServeHTTP(w http.ResponseWriter, r *http.Request) {
verifier, err := slack.NewSecretsVerifier(r.Header, scs.signingSecret)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
r.Body = io.NopCloser(io.TeeReader(r.Body, &verifier))
s, err := slack.SlashCommandParse(r)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
if err = verifier.Ensure(); err != nil {
w.WriteHeader(http.StatusUnauthorized)
return
}
var resp *slack.Msg
args, err := shellwords.Parse(s.Text)
if err != nil {
slackReply(w, &slack.Msg{Text: err.Error()})
return
}
if len(args) < 1 {
slackReply(w, &slack.Msg{Text: "need org/repo argument"})
return
}
parts := strings.SplitN(args[0], "/", 2)
if len(parts) != 2 {
slackReply(w, &slack.Msg{Text: "need org/repo argument"})
return
}
org, repo := parts[0], parts[1]
args = args[1:]
switch s.Command {
case "/ci-setup":
resp = scs.setupCmd(org, repo, args)
case "/ci-build":
resp = &slack.Msg{Text: s.Text}
case "/ci-deploy":
resp = &slack.Msg{Text: s.Text}
default:
w.WriteHeader(http.StatusInternalServerError)
return
}
slackReply(w, resp)
}
func slackReply(w http.ResponseWriter, resp *slack.Msg) {
b, err := json.Marshal(resp)
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
w.Write(b)
}