-
Notifications
You must be signed in to change notification settings - Fork 2
/
slashcmds.go
304 lines (257 loc) · 7.54 KB
/
slashcmds.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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
// Copyright 2019 Qubit Ltd.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package kubeci
import (
"context"
"errors"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"strconv"
"strings"
"github.com/google/go-github/v45/github"
"github.com/mattn/go-shellwords"
)
type SlashHandler struct {
Runner workflowRunner
CIContextPath string
CIYAMLFile string
CIStarlarkFile string
Templates TemplateSet
}
func (s *SlashHandler) slashComment(ctx context.Context, ghClient GithubClientInterface, event *github.IssueCommentEvent, body string) error {
err := ghClient.CreateIssueComment(
ctx,
int(*event.Issue.Number),
&github.IssueComment{
Body: &body,
},
)
if err != nil {
return fmt.Errorf("failed to create comment, %w", err)
}
return nil
}
func (s *SlashHandler) slashUnknown(ctx context.Context, ghClient GithubClientInterface, event *github.IssueCommentEvent, args ...string) error {
body := `
known command:
- *run*: run the workflow onthe current branch (only valid for PRs)
- *deploy* [environment (default: "staging")]: run the deploy workflow (not implemented yet)
- *setup* TEMPLATENAME: add/replace the current workflow with the specified template
`
tsHelp := strings.Split(s.Templates.Help(), "\n")
for _, line := range tsHelp {
body += fmt.Sprintf(" %s\n", line)
}
body = strings.TrimSpace(body)
return s.slashComment(ctx, ghClient, event, body)
}
func (s *SlashHandler) slashRun(ctx context.Context, ghClient GithubClientInterface, event *github.IssueCommentEvent, args ...string) error {
prlinks := event.GetIssue().GetPullRequestLinks()
if prlinks == nil {
body := strings.TrimSpace(`
run is only valid on PR comments
`)
return s.slashComment(ctx, ghClient, event, body)
}
parts := strings.Split(prlinks.GetURL(), "/")
pridstr := parts[len(parts)-1]
prid, _ := strconv.Atoi(pridstr)
pr, err := ghClient.GetPullRequest(ctx, prid)
if err != nil {
return fmt.Errorf("failed lookup up PR, %w", err)
}
if pr == nil {
return errors.New("failed lookup up PR")
}
headsha := pr.GetHead().GetSHA()
headref := pr.GetHead().GetRef()
wctx := WorkflowContext{
Repo: event.Repo,
SHA: headsha,
RefType: "branch",
Ref: headref,
Entrypoint: "",
PRs: []*github.PullRequest{pr},
Event: event,
}
_, err = s.Runner.runWorkflow(
ctx,
ghClient,
&wctx,
)
return err
}
func (s *SlashHandler) slashDeploy(ctx context.Context, ghClient GithubClientInterface, event *github.IssueCommentEvent, args ...string) error {
if len(args) > 1 {
return s.slashComment(ctx, ghClient, event, "please specify one environment")
}
env := "staging"
if len(args) == 1 {
env = args[0]
}
_, sha, err := s.issueHead(ctx, ghClient, event)
if err != nil {
return fmt.Errorf("cannot setup ci for repository, %w", err)
}
msg := fmt.Sprintf("deploying the thing to %v", env)
dep, err := ghClient.CreateDeployment(
ctx,
&github.DeploymentRequest{
Ref: &sha,
Description: &msg,
Environment: &env,
},
)
if err != nil {
return fmt.Errorf("failed to create deploy, %w", err)
}
log.Printf("Deployment created, %v", *dep.ID)
return nil
}
func (s *SlashHandler) slashSetup(ctx context.Context, ghClient GithubClientInterface, event *github.IssueCommentEvent, args ...string) error {
if len(args) != 1 {
s.slashComment(ctx, ghClient, event, "you mest specify a template")
return nil
}
tmpl, ok := s.Templates[args[0]]
if !ok {
s.slashComment(ctx, ghClient, event, "unknown template "+args[0])
return nil
}
branch, sha, err := s.issueHead(ctx, ghClient, event)
if err != nil {
s.slashComment(ctx, ghClient, event, "blah")
return fmt.Errorf("cannot setup ci for repository, %w", err)
}
path := s.CIContextPath
fileName := filepath.Join(path, s.CIYAMLFile)
files, err := ghClient.GetContents(
ctx,
path,
&github.RepositoryContentGetOptions{
Ref: branch,
},
)
if err != nil {
if ghErr, ok := err.(*github.ErrorResponse); ok {
if ghErr.Response.StatusCode != http.StatusNotFound {
return fmt.Errorf("couldn't get directory listing, %w", ghErr)
}
log.Printf("couldn't get %s for %s", path, sha)
} else {
return fmt.Errorf("couldn't get directory listing, %w", err)
}
}
var existingSHA *string
for _, f := range files {
log.Printf("checking %s %s", *f.Path, *f.SHA)
if *f.Path == fileName {
existingSHA = f.SHA
log.Printf("found existing %v (%v)", *f.Path, *existingSHA)
break
}
}
bs, err := os.ReadFile(tmpl.CI)
if err != nil {
s.slashComment(ctx, ghClient, event, fmt.Sprintf("couldn't read template file %s, ci server config is broken!", fileName))
return nil
}
body := fmt.Sprintf(`kube-ci configured by %s via %s`, *event.Comment.User.Login, *event.Comment.HTMLURL)
opts := &github.RepositoryContentFileOptions{
Message: &body,
Content: bs,
Branch: &branch,
SHA: existingSHA,
}
err = ghClient.CreateFile(
ctx,
fileName,
opts)
if err != nil {
return err
}
return nil
}
func (s *SlashHandler) issueHead(ctx context.Context, ghClient GithubClientInterface, event *github.IssueCommentEvent) (string, string, error) {
if event.Issue.PullRequestLinks == nil {
branch, err := ghClient.GetBranch(
ctx,
*event.Repo.DefaultBranch,
)
if err != nil {
return "", "", err
}
return *event.Repo.DefaultBranch, *branch.Commit.SHA, nil
}
link := event.Issue.PullRequestLinks.URL
parts := strings.Split(*link, "/")
prid, err := strconv.Atoi(parts[len(parts)-1])
if err != nil {
return "", "", fmt.Errorf("couldn't parse pull-request ID from %s, %w", *link, err)
}
pr, err := ghClient.GetPullRequest(ctx, prid)
if err != nil {
return "", "", fmt.Errorf("couldn't get PR, %w", err)
}
if *pr.Head.Repo.URL != *pr.Base.Repo.URL ||
*pr.Head.Repo.URL != *event.Repo.URL {
return "", "", fmt.Errorf("refusing to update cross-repo PR")
}
return *pr.Head.Ref, *pr.Head.SHA, nil
}
func (s *SlashHandler) slashCommand(ctx context.Context, client GithubClientInterface, event *github.IssueCommentEvent) error {
cmdparts := strings.SplitN(strings.TrimSpace(*event.Comment.Body), " ", 3)
user := event.Comment.GetUser()
ok, err := client.IsMember(ctx, user.GetLogin())
if err != nil {
log.Printf("error querying github membership, %v", err)
return fmt.Errorf("failed to check org membership")
}
if !ok {
s.slashComment(ctx, client, event, "you must be an organisation member to execute commands")
return nil
}
if len(cmdparts) == 1 {
s.slashUnknown(ctx, client, event)
return nil
}
cmd := cmdparts[1]
var args []string
if len(cmdparts) > 2 {
args, err = shellwords.Parse(cmdparts[2])
if err != nil {
return nil
}
}
type slashCommand func(ctx context.Context, ghc GithubClientInterface, event *github.IssueCommentEvent, args ...string) error
handlers := map[string]slashCommand{
"run": s.slashRun,
"deploy": s.slashDeploy,
"setup": s.slashSetup,
}
f, ok := handlers[cmd]
if !ok {
s.slashUnknown(ctx, client, event, cmd)
return nil
}
err = f(ctx, client, event, args...)
if err != nil {
log.Printf("slash command failed, %v", err)
return err
}
return nil
}