-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstep.go
126 lines (113 loc) · 3.02 KB
/
step.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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2024-Present Harry Randazzo
package vai
import (
"github.com/invopop/jsonschema"
)
// Step is a single step in a task
//
// While a step can have any combination of `run`, `eval`, and `uses` fields, only one of them should be set
// at a time.
//
// This is enforced by JSON schema validation.
type Step struct {
// Run is the command/script to run
Run string `json:"run,omitempty"`
// Eval is an expression to evaluate with tengo
Eval string `json:"eval,omitempty"`
// Uses is a reference to another task
Uses string `json:"uses,omitempty"`
// With is a map of additional parameters for the step/task call
With `json:"with,omitempty"`
// ID is a unique identifier for the step
ID string `json:"id,omitempty"`
// Name is a human-readable name for the step
Name string `json:"name,omitempty"`
}
// JSONSchemaExtend extends the JSON schema for a step
func (Step) JSONSchemaExtend(schema *jsonschema.Schema) {
not := &jsonschema.Schema{
Not: &jsonschema.Schema{},
}
props := jsonschema.NewProperties()
props.Set("run", &jsonschema.Schema{
Type: "string",
Description: "Command/script to run",
})
props.Set("uses", &jsonschema.Schema{
Type: "string",
Description: "Location of a remote task to call conforming to the purl spec",
})
props.Set("eval", &jsonschema.Schema{
Type: "string",
Description: "Expression to evaluate with tengo",
})
props.Set("id", &jsonschema.Schema{
Type: "string",
Description: "Unique identifier for the step",
})
props.Set("name", &jsonschema.Schema{
Type: "string",
Description: "Human-readable name for the step",
})
oneOfStringIntBool := &jsonschema.Schema{
OneOf: []*jsonschema.Schema{
{
Type: "string",
},
{
Type: "boolean",
},
{
Type: "integer",
},
},
}
var single uint64 = 1
with := &jsonschema.Schema{
Type: "object",
Description: "Additional parameters for the step/task call",
MinItems: &single,
PatternProperties: map[string]*jsonschema.Schema{
EnvVariablePattern.String(): oneOfStringIntBool,
},
AdditionalProperties: jsonschema.FalseSchema,
}
props.Set("with", with)
runProps := jsonschema.NewProperties()
runProps.Set("run", &jsonschema.Schema{
Type: "string",
})
runProps.Set("uses", not)
runProps.Set("eval", not)
oneOfRun := &jsonschema.Schema{
Required: []string{"run"},
Properties: runProps,
}
usesProps := jsonschema.NewProperties()
usesProps.Set("run", not)
usesProps.Set("eval", not)
usesProps.Set("uses", &jsonschema.Schema{
Type: "string",
})
oneOfUses := &jsonschema.Schema{
Required: []string{"uses"},
Properties: usesProps,
}
evalProps := jsonschema.NewProperties()
evalProps.Set("run", not)
evalProps.Set("uses", not)
evalProps.Set("eval", &jsonschema.Schema{
Type: "string",
})
oneOfEval := &jsonschema.Schema{
Required: []string{"eval"},
Properties: evalProps,
}
schema.Properties = props
schema.OneOf = []*jsonschema.Schema{
oneOfRun,
oneOfUses,
oneOfEval,
}
}