-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathlessons.go
More file actions
272 lines (227 loc) · 7.34 KB
/
Copy pathlessons.go
File metadata and controls
272 lines (227 loc) · 7.34 KB
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
package api
import (
"fmt"
"github.com/goccy/go-json"
)
type Lesson struct {
Lesson struct {
Type string
LessonDataCLI *LessonDataCLI
}
}
type LessonDataCLI struct {
// Readme string
CLIData CLIData
}
const BaseURLOverrideRequired = "override"
type CLIData struct {
// ContainsCompleteDir bool
BaseURLDefault string `yaml:"baseURLDefault"`
Steps []CLIStep `yaml:"steps"`
AllowedOperatingSystems []string `yaml:"allowedOperatingSystems"`
}
type CLIStep struct {
CLICommand *CLIStepCLICommand `yaml:"cliCommand"`
HTTPRequest *CLIStepHTTPRequest `yaml:"httpRequest"`
NoPenaltyOnFail bool `yaml:"noPenaltyOnFail"`
}
type CLIStepCLICommand struct {
Command string `yaml:"command"`
Tests []CLICommandTest `yaml:"tests"`
SleepAfterMs *int `yaml:"sleepAfterMs"`
StdoutFilterTmdl *string `yaml:"stdoutFilterTmdl"`
}
type CLICommandTest struct {
ExitCode *int `yaml:"exitCode"`
StdoutContainsAll []string `yaml:"stdoutContainsAll"`
StdoutContainsNone []string `yaml:"stdoutContainsNone"`
StdoutLinesGt *int `yaml:"stdoutLinesGt"`
StdoutJq *StdoutJqTest `yaml:"stdoutJq"`
}
type StdoutJqTest struct {
InputMode string `yaml:"inputMode"` // "json" or "jsonl"
Query string `yaml:"query"`
ExpectedResults []JqExpectedResult `yaml:"expectedResults"`
}
type JqExpectedResult struct {
Type JqValueType `yaml:"type"`
Operator JqOperator `yaml:"operator"`
Value any `yaml:"value"`
}
type (
JqValueType string
JqOperator string // defined fully on backend
)
const (
JqTypeString JqValueType = "string"
JqTypeInt JqValueType = "int"
JqTypeBool JqValueType = "bool"
)
type CLIStepHTTPRequest struct {
ResponseVariables []HTTPRequestResponseVariable `yaml:"responseVariables"`
ResponseHeaderVariables []HTTPRequestResponseHeaderVariable `yaml:"responseHeaderVariables"`
Tests []HTTPRequestTest `yaml:"tests"`
Request HTTPRequest `yaml:"request"`
SleepAfterMs *int `yaml:"sleepAfterMs"`
}
type Sleepable interface {
GetSleepAfterMs() *int
}
func (c *CLIStepCLICommand) GetSleepAfterMs() *int {
return c.SleepAfterMs
}
func (h *CLIStepHTTPRequest) GetSleepAfterMs() *int {
return h.SleepAfterMs
}
const BaseURLPlaceholder = "${baseURL}"
type HTTPRequest struct {
Method string `yaml:"method"`
FullURL string `yaml:"fullURL"`
Headers map[string]string `yaml:"headers"`
BodyJSON map[string]any `yaml:"bodyJSON"`
BodyForm map[string]string `yaml:"bodyForm"`
FollowRedirects *bool `yaml:"followRedirects"`
BasicAuth *HTTPBasicAuth `yaml:"basicAuth"`
}
type HTTPBasicAuth struct {
Username string `yaml:"username"`
Password string `yaml:"password"`
}
type HTTPRequestResponseVariable struct {
Name string `yaml:"name"`
Path string `yaml:"path"`
BodyRegex string `yaml:"bodyRegex"`
}
type HTTPRequestResponseHeaderVariable struct {
Name string `yaml:"name"`
Header string `yaml:"header"`
Regex string `yaml:"regex"`
}
// HTTPRequestTest should have only one field set
type HTTPRequestTest struct {
StatusCode *int `yaml:"statusCode"`
BodyContains *string `yaml:"bodyContains"`
BodyContainsNone *string `yaml:"bodyContainsNone"`
HeadersContain *HTTPRequestTestHeader `yaml:"headersContain"`
TrailersContain *HTTPRequestTestHeader `yaml:"trailersContain"`
JSONValue *HTTPRequestTestJSONValue `yaml:"jsonValue"`
}
type HTTPRequestTestHeader struct {
Key string `yaml:"key"`
Value string `yaml:"value"`
}
type HTTPRequestTestJSONValue struct {
Path string `yaml:"path"`
Operator OperatorType `yaml:"operator"`
IntValue *int `yaml:"intValue"`
StringValue *string `yaml:"stringValue"`
BoolValue *bool `yaml:"boolValue"`
}
type OperatorType string
const (
OpEquals OperatorType = "eq"
OpGreaterThan OperatorType = "gt"
OpContains OperatorType = "contains"
OpNotContains OperatorType = "not_contains"
)
func FetchLesson(uuid string) (*Lesson, error) {
resp, err := fetchWithAuth("GET", "/v1/lessons/"+uuid)
if err != nil {
return nil, err
}
var data Lesson
err = json.Unmarshal(resp, &data)
if err != nil {
return nil, err
}
return &data, nil
}
type CLIStepResult struct {
CLICommandResult *CLICommandResult
HTTPRequestResult *HTTPRequestResult
}
type CLICommandResult struct {
ExitCode int
FinalCommand string `json:"-"`
Command CLIStepCLICommand `json:"-"`
Stdout string
Variables map[string]string
JqOutputs []CLICommandJqOutput `json:"-"`
}
type CLICommandJqOutput struct {
Query string
Results []string
Error string
}
type HTTPRequestResult struct {
Err string `json:"-"`
StatusCode int
ResponseHeaders map[string]string
ResponseTrailers map[string]string
BodyString string
Variables map[string]string
Request CLIStepHTTPRequest
}
type lessonSubmissionCLI struct {
CLIResults []CLIStepResult
}
type SubmissionDebugData struct {
Endpoint string
RequestBody string
ResponseStatusCode int
ResponseBody string
}
type XPBreakdownItem struct {
Name string
Percent float64
XP int
}
type LessonSubmissionEvent struct {
ResultSlug VerificationResultSlug
StructuredErrCLI *StructuredErrCLI
XPReward int
XPBreakdown []XPBreakdownItem
}
type StructuredErrCLI struct {
ErrorMessage string `json:"Error"`
FailedStepIndex int `json:"FailedStepIndex"`
FailedTestIndex int `json:"FailedTestIndex"`
}
type VerificationResultSlug string
const (
// "noop" is for "noPenaltyOnFail" on the CLI type
VerificationResultSlugNoop VerificationResultSlug = "noop"
VerificationResultSlugSystemError VerificationResultSlug = "system-error"
VerificationResultSlugSuccess VerificationResultSlug = "success"
VerificationResultSlugFailure VerificationResultSlug = "failure"
)
func SubmitCLILesson(uuid string, results []CLIStepResult, captureDebug bool) (LessonSubmissionEvent, SubmissionDebugData, error) {
endpoint := fmt.Sprintf("/v1/lessons/%v/", uuid)
debugData := SubmissionDebugData{Endpoint: endpoint}
bytes, err := json.Marshal(lessonSubmissionCLI{CLIResults: results})
if err != nil {
return LessonSubmissionEvent{}, debugData, err
}
if captureDebug {
debugData.RequestBody = string(bytes)
}
resp, code, err := fetchWithAuthAndPayload("POST", endpoint, bytes)
debugData.ResponseStatusCode = code
if captureDebug {
debugData.ResponseBody = string(resp)
}
if err != nil {
return LessonSubmissionEvent{}, debugData, err
}
if code == 402 {
return LessonSubmissionEvent{}, debugData, fmt.Errorf("to run and submit the tests for this lesson, you must have an active Boot.dev membership\nhttps://boot.dev/pricing")
}
if code != 200 {
return LessonSubmissionEvent{}, debugData, fmt.Errorf("failed to submit CLI lesson (code %v): %s", code, string(resp))
}
result := LessonSubmissionEvent{}
if err := json.Unmarshal(resp, &result); err != nil {
return LessonSubmissionEvent{}, debugData, err
}
return result, debugData, nil
}