-
Notifications
You must be signed in to change notification settings - Fork 18
/
builtin.go
91 lines (73 loc) · 2.08 KB
/
builtin.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
package loader
import (
"errors"
"fmt"
"github.com/cirruslabs/cirrus-cli/pkg/parser"
"go.starlark.net/starlark"
)
var (
ErrChangesInclude = errors.New("changes_include() failed")
ErrChangesIncludeOnly = errors.New("changes_include_only() failed")
)
func generateChangesIncludeBuiltin(affectedFiles []string) *starlark.Builtin {
result := func(
thread *starlark.Thread,
fn *starlark.Builtin,
args starlark.Tuple,
kwargs []starlark.Tuple,
) (starlark.Value, error) {
rawPatterns, err := starlarkArgsToStrings(args, kwargs)
if err != nil {
return nil, err
}
count, err := parser.CountMatchingAffectedFiles(affectedFiles, rawPatterns)
if err != nil {
return nil, err
}
if count > 0 {
return starlark.True, nil
}
return starlark.False, nil
}
return starlark.NewBuiltin("changes_include", result)
}
func generateChangesIncludeOnlyBuiltin(affectedFiles []string) *starlark.Builtin {
result := func(
thread *starlark.Thread,
fn *starlark.Builtin,
args starlark.Tuple,
kwargs []starlark.Tuple,
) (starlark.Value, error) {
rawPatterns, err := starlarkArgsToStrings(args, kwargs)
if err != nil {
return nil, err
}
count, err := parser.CountMatchingAffectedFiles(affectedFiles, rawPatterns)
if err != nil {
return nil, err
}
if count > 0 && count == len(affectedFiles) {
return starlark.True, nil
}
return starlark.False, nil
}
return starlark.NewBuiltin("changes_include_only", result)
}
func starlarkArgsToStrings(args starlark.Tuple, kwargs []starlark.Tuple) ([]string, error) {
var result []string
if len(kwargs) != 0 {
return nil, fmt.Errorf("%w: found %d keyword arguments, expected 0", ErrChangesInclude, len(kwargs))
}
if len(args) == 0 {
return nil, fmt.Errorf("%w: expected at least 1 positional argument, found 0", ErrChangesInclude)
}
for i, arg := range args {
stringArgument, ok := arg.(starlark.String)
if !ok {
return nil, fmt.Errorf("%w: expected %d'th argument to be string, got %s", ErrChangesInclude, i+1,
arg.Type())
}
result = append(result, stringArgument.GoString())
}
return result, nil
}