-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.go
403 lines (307 loc) · 11.1 KB
/
commands.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
package app
import (
"bytes"
"context"
"fmt"
catalyst_types "github.com/skip-mev/catalyst/pkg/types"
"regexp"
"strings"
"github.com/google/go-github/v66/github"
"github.com/nao1215/markdown"
"github.com/skip-mev/ironbird/types"
testnettypes "github.com/skip-mev/ironbird/types/testnet"
"github.com/skip-mev/ironbird/util"
"github.com/skip-mev/ironbird/workflows/testnet"
temporalclient "go.temporal.io/sdk/client"
"gopkg.in/yaml.v3"
)
var SubcommandRegex = regexp.MustCompile(`/ironbird ([^\s]*).*`)
var StartRegex = regexp.MustCompile(`/ironbird start ([^\s]*) ?([^\s]*)`)
var CustomConfigRegex = regexp.MustCompile(`--load-test-config=(\{.*\})`)
type CommandFunc func(context.Context, *Comment, string) error
type Command struct {
Usage string
Description string
Func CommandFunc
}
func (a *App) generateInitialComment() (string, error) {
var detailsOut bytes.Buffer
var mdOut bytes.Buffer
var customConfigOut bytes.Buffer
detailsMd := markdown.NewMarkdown(&detailsOut)
detailsMd = detailsMd.PlainText("To use Ironbird, you can use the following commands:").LF()
var commandEntries []string
for _, command := range a.commands {
commandEntries = append(commandEntries, fmt.Sprintf("%s - %s", command.Usage, command.Description))
}
detailsMd = detailsMd.BulletList(commandEntries...)
customConfigMd := markdown.NewMarkdown(&customConfigOut)
customConfigMd = customConfigMd.PlainText("You can provide a custom load test configuration using the `--load-test-config=` flag:").LF()
customConfigMd = customConfigMd.CodeBlocks("", `/ironbird start cosmos --load-test-config={
"block_gas_limit_target": 0.75,
"num_of_blocks": 50,
"msgs": [
{"weight": 0.3, "type": "MsgSend"},
{"weight": 0.3, "type": "MsgMultiSend"},
{"weight": 0.4, "type": "MsgArr", "ContainedType": "MsgSend", "NumMsgs": 3300}
]
}`)
customConfigMd = customConfigMd.PlainText("Use `/ironbird loadtests` to see more examples.").LF()
if err := customConfigMd.Build(); err != nil {
return "", err
}
if err := detailsMd.Build(); err != nil {
return "", err
}
md := markdown.NewMarkdown(&mdOut)
md = md.Details("Ironbird - launch a network", detailsOut.String())
md = md.Details("Custom Load Test Configuration", customConfigOut.String())
if err := md.Build(); err != nil {
return "", err
}
return mdOut.String(), nil
}
func (a *App) generatedFailedCommandComment(command string, err error) (string, error) {
var mdOut bytes.Buffer
md := markdown.NewMarkdown(&mdOut)
md = md.PlainText(fmt.Sprintf("Ironbird failed to run command `%s`:", command)).LF()
md = md.CodeBlocks("", err.Error())
if err := md.Build(); err != nil {
return "", err
}
return mdOut.String(), nil
}
func (a *App) generateStartedTestComment(chainConfig types.ChainsConfig, loadTestSpec catalyst_types.LoadTestSpec,
workflowId, runId string, runnerType testnettypes.RunnerType) (string, error) {
var mdOut bytes.Buffer
var chainDetails bytes.Buffer
var loadTestDetails bytes.Buffer
chainMd := markdown.NewMarkdown(&chainDetails)
chainMd = chainMd.LF().PlainText(fmt.Sprintf("Chain: `%s`", chainConfig.Name)).LF()
chainMd = chainMd.PlainText(fmt.Sprintf("Version: `%s`", chainConfig.Version)).LF()
chainMd = chainMd.PlainText(fmt.Sprintf("Runner: `%s`", runnerType)).LF()
chainMd = chainMd.PlainText(fmt.Sprintf("Workflow ID: `%s`", workflowId)).LF()
chainMd = chainMd.PlainText(fmt.Sprintf("Run ID: `%s`", runId)).LF()
if err := chainMd.Build(); err != nil {
return "", err
}
loadTestMd := markdown.NewMarkdown(&loadTestDetails)
loadTestMd = loadTestMd.LF().PlainText(fmt.Sprintf("Load test config: `%v`", loadTestSpec)).LF()
if err := loadTestMd.Build(); err != nil {
return "", err
}
md := markdown.NewMarkdown(&mdOut)
md = md.PlainText(fmt.Sprintf("Ironbird has started a testnet for chain `%s` using loadtest `%s` with runner `%s`", chainConfig.Name, loadTestSpec.Name, runnerType)).LF()
md = md.Details("Chain details", chainDetails.String())
md = md.Details("Load test details", loadTestDetails.String())
if err := md.Build(); err != nil {
return "", err
}
return mdOut.String(), nil
}
func (a *App) SendInitialComment(ctx context.Context, pr *PullRequest) error {
commentBody, err := a.generateInitialComment()
if err != nil {
return err
}
if _, err := a.CreateComment(ctx, pr.Issue, commentBody); err != nil {
return err
}
return nil
}
func (a *App) handleFailedCommand(ctx context.Context, comment *Comment, command string, commandErr error) error {
if commandErr == nil {
return fmt.Errorf("failed command cannot have a nil commandErr")
}
failedCommandCommentBody, err := a.generatedFailedCommandComment(command, commandErr)
if err != nil {
return err
}
if _, err := a.CreateComment(ctx, comment.Issue, failedCommandCommentBody); err != nil {
return err
}
return err
}
func (a *App) HandleCommand(ctx context.Context, comment *Comment, command string) error {
subcommandName := SubcommandRegex.FindAllStringSubmatch(command, -1)
if len(subcommandName) != 1 {
return fmt.Errorf("invalid command %s", command)
}
subcommand, ok := a.commands[subcommandName[0][1]]
if !ok {
return a.handleFailedCommand(ctx, comment, command, fmt.Errorf("unknown command %s", subcommandName[0][0]))
}
err := subcommand.Func(ctx, comment, command)
if err != nil {
return a.handleFailedCommand(ctx, comment, command, err)
}
return nil
}
func (a *App) parseCustomLoadTestSpec(yamlStr string) (*catalyst_types.LoadTestSpec, error) {
var customConfig catalyst_types.LoadTestSpec
if err := yaml.Unmarshal([]byte(yamlStr), &customConfig); err != nil {
return nil, fmt.Errorf("failed to parse custom load test config: %w", err)
}
err := customConfig.Validate()
if err != nil {
return nil, fmt.Errorf("failed to validate custom load test config: %w", err)
}
return &customConfig, nil
}
func (a *App) commandStart(ctx context.Context, comment *Comment, command string) error {
if !comment.Issue.IsPullRequest {
return fmt.Errorf("command can only be run on pull requests")
}
client, err := a.cc.NewInstallationClient(comment.InstallationID)
if err != nil {
return err
}
isMember, _, err := client.Organizations.IsMember(ctx, comment.Issue.Owner, comment.Sender)
if err != nil {
return err
}
if !isMember {
return fmt.Errorf("user %s is not a member of the organization", comment.Sender)
}
pr, _, err := client.PullRequests.Get(ctx, comment.Issue.Owner, comment.Issue.Repo, comment.Issue.Number)
if err != nil {
return err
}
if pr == nil {
return fmt.Errorf("no pull request found")
}
if pr.GetHead() == nil {
return fmt.Errorf("no head found")
}
if pr.GetHead().SHA == nil {
return fmt.Errorf("no head sha found")
}
args := StartRegex.FindAllStringSubmatch(command, -1)
if len(args) != 1 {
return fmt.Errorf("invalid command %s", command)
}
chainName := args[0][1]
secondArg := args[0][2]
customConfig := CustomConfigRegex.FindStringSubmatch(command)
hasCustomConfig := len(customConfig) > 1
// Check if the second arg is the custom config flag, not a load test mode
isCustomConfigFlag := strings.HasPrefix(secondArg, "--load-test-config=")
loadTestName := ""
// Only treat secondArg as loadTestName if it's not the custom config flag
if !isCustomConfigFlag {
loadTestName = secondArg
}
// If custom config is provided, loadTestName should be empty
if hasCustomConfig && loadTestName != "" {
return fmt.Errorf("when using --load-test-config, do not specify a load test mode (e.g., 'full')")
}
// If no custom config is provided, loadTestName is required
if !hasCustomConfig && loadTestName == "" {
return fmt.Errorf("load test mode is required when not using --load-test-config")
}
// Set runner type to DigitalOcean by default when using Github CLI
runnerType := testnettypes.DigitalOcean
chain, ok := a.cfg.Chains[chainName]
if !ok {
return fmt.Errorf("unknown chain %s", chainName)
}
var loadTest catalyst_types.LoadTestSpec
if hasCustomConfig {
customLoadTest, err := a.parseCustomLoadTestSpec(customConfig[1])
if err != nil {
return err
}
loadTest = *customLoadTest
} else {
configuredLoadTest, ok := a.cfg.LoadTests[loadTestName]
if !ok {
return fmt.Errorf("unknown load test %s", loadTestName)
}
loadTest = configuredLoadTest
}
id := fmt.Sprintf("%s/%s/%s/pr-%d", chain.Name, comment.Issue.Owner, comment.Issue.Repo, comment.Issue.Number)
workflow, err := a.temporalClient.ExecuteWorkflow(ctx, temporalclient.StartWorkflowOptions{
ID: id,
TaskQueue: testnet.TaskQueue,
}, testnet.Workflow, testnet.WorkflowOptions{
InstallationID: comment.InstallationID,
Owner: comment.Issue.Owner,
Repo: comment.Issue.Repo,
SHA: *pr.Head.SHA,
ChainConfig: chain,
LoadTestSpec: &loadTest,
RunnerType: runnerType,
})
if err != nil {
fmt.Println("failed to execute workflow", err)
return err
}
commentBody, err := a.generateStartedTestComment(chain, loadTest, workflow.GetID(), workflow.GetRunID(), runnerType)
if err != nil {
return err
}
if _, err := a.CreateComment(ctx, comment.Issue, commentBody); err != nil {
return err
}
return nil
}
func (a *App) commandChains(ctx context.Context, comment *Comment, _ string) error {
client, err := a.cc.NewInstallationClient(comment.InstallationID)
if err != nil {
return err
}
var mdOut bytes.Buffer
md := markdown.NewMarkdown(&mdOut)
var entries []string
for _, chain := range a.cfg.Chains {
entries = append(entries, fmt.Sprintf("`%s` (version `%s`)", chain.Name, chain.Version))
}
md = md.H3("Ironbird - available chains")
md = md.BulletList(entries...)
if err := md.Build(); err != nil {
return err
}
_, _, err = client.Issues.CreateComment(ctx, comment.Issue.Owner, comment.Issue.Repo, comment.Issue.Number, &github.IssueComment{
Body: util.StringPtr(mdOut.String()),
})
if err != nil {
return err
}
return nil
}
func (a *App) commandLoadTests(ctx context.Context, comment *Comment, _ string) error {
var mdOut bytes.Buffer
var exampleOut bytes.Buffer
md := markdown.NewMarkdown(&mdOut)
exampleMd := markdown.NewMarkdown(&exampleOut)
var entries []string
for name, loadTest := range a.cfg.LoadTests {
entries = append(entries, fmt.Sprintf("`%s` - `%s`", name, loadTest.Description))
}
entries = append(entries, "`custom` - Use the `--load-test-config={...}` flag with the start command to provide an inline YAML configuration")
md = md.H3("Ironbird - available loadtests")
md = md.BulletList(entries...)
exampleMd = exampleMd.PlainText("Example of using a custom load test configuration:").LF()
exampleMd = exampleMd.CodeBlocks("", `/ironbird start cosmos --load-test-config={
"name": "custom-test",
"description": "Custom load test with 1000 transactions per block",
"num_of_txs": 1000,
"num_of_blocks": 50,
"msgs": [
{"weight": 0.3, "type": "MsgSend"},
{"weight": 0.3, "type": "MsgMultiSend"},
{"weight": 0.4, "type": "MsgArr", "ContainedType": "MsgSend", "NumMsgs": 50 }
]
}`)
if err := exampleMd.Build(); err != nil {
return err
}
if err := md.Build(); err != nil {
return err
}
finalOutput := mdOut.String() + "\n\n" + exampleOut.String()
if _, err := a.CreateComment(ctx, comment.Issue, finalOutput); err != nil {
return err
}
return nil
}