feat(workflow): routing and playbook findings carry the key's file:line
This commit is contained in:
1 parent
288e9ea370
commit
3a4af23caa
6 files changed
+138
-47
No files matched your search
@@ -98,11 +98,10 @@ func TestLintReportsLoadErrorAsFinding(t *testing.T) {
|
||||
}
|
||||
joined := strings.Join(result.Findings, "\n")
|
||||
// Both "bug" and "security" collide across the two files; the loader
|
||||
// sorts a file's keys before checking them (routing.go's
|
||||
// loadPlaybookFile), so "bug" -- alphabetically first -- is always the
|
||||
// one reported, deterministically.
|
||||
if !strings.Contains(joined, "bug") {
|
||||
t.Fatalf("Lint() findings = %q, want the colliding key named", joined)
|
||||
// checks a file's keys in document order, so the first line of b.yaml
|
||||
// is always the one reported, deterministically.
|
||||
if want := filepath.Join(dir, "b.yaml") + `:1: label "security"`; !strings.Contains(joined, want) {
|
||||
t.Fatalf("Lint() findings = %q, want %q", joined, want)
|
||||
}
|
||||
// Fail-fast like the daemon: one definition failure is reported (the
|
||||
// loader returns the first), not an enumeration of every collision.
|
||||
|
||||
@@ -66,17 +66,17 @@ func LoadKindWorkflowsYAML(path string) (KindWorkflows, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read kind-workflows file %s: %w", path, err)
|
||||
}
|
||||
raw := map[string]string{}
|
||||
if err := yaml.Unmarshal(data, &raw); err != nil {
|
||||
bindings, err := decodeBindings(data)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse kind-workflows file %s: %w", path, err)
|
||||
}
|
||||
kw := make(KindWorkflows, len(raw))
|
||||
for k, name := range raw {
|
||||
kind := workintake.Kind(k)
|
||||
kw := make(KindWorkflows, len(bindings))
|
||||
for _, b := range bindings {
|
||||
kind := workintake.Kind(b.key)
|
||||
if err := kind.Validate(); err != nil {
|
||||
return nil, fmt.Errorf("kind-workflows file %s: %w", path, err)
|
||||
return nil, fmt.Errorf("kind-workflows file %s:%d: %w", path, b.line, err)
|
||||
}
|
||||
kw[kind] = name
|
||||
kw[kind] = b.value
|
||||
}
|
||||
return kw, nil
|
||||
}
|
||||
@@ -157,22 +157,12 @@ func loadPlaybookFile(path string, kw KindWorkflows, lw LabelWorkflows) error {
|
||||
if err != nil {
|
||||
return fmt.Errorf("read playbook file %s: %w", path, err)
|
||||
}
|
||||
raw := map[string]string{}
|
||||
if err := yaml.Unmarshal(data, &raw); err != nil {
|
||||
bindings, err := decodeBindings(data)
|
||||
if err != nil {
|
||||
return fmt.Errorf("parse playbook file %s: %w", path, err)
|
||||
}
|
||||
// Sorted, not range order: LoadPlaybookDirs' doc comment promises a
|
||||
// reproducible error when a file declares more than one colliding key,
|
||||
// which a raw map range (Go's iteration order is randomized) cannot
|
||||
// keep -- the same file could report a different "first" collision on
|
||||
// every run otherwise.
|
||||
keys := make([]string, 0, len(raw))
|
||||
for key := range raw {
|
||||
keys = append(keys, key)
|
||||
}
|
||||
sort.Strings(keys)
|
||||
for _, key := range keys {
|
||||
if err := bindPlaybookKey(path, key, raw[key], kw, lw); err != nil {
|
||||
for _, b := range bindings {
|
||||
if err := bindPlaybookKey(fmt.Sprintf("%s:%d", path, b.line), b.key, b.value, kw, lw); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
@@ -182,18 +172,19 @@ func loadPlaybookFile(path string, kw KindWorkflows, lw LabelWorkflows) error {
|
||||
// bindPlaybookKey classifies one playbook binding key as a closed-vocabulary
|
||||
// kind or an arbitrary label, then merges it into kw/lw -- applying the same
|
||||
// collision and empty-value rules LoadPlaybookDirs' doc comment describes.
|
||||
func bindPlaybookKey(path, key, value string, kw KindWorkflows, lw LabelWorkflows) error {
|
||||
// pos is the key's file:line, which every finding leads with.
|
||||
func bindPlaybookKey(pos, key, value string, kw KindWorkflows, lw LabelWorkflows) error {
|
||||
trimmed := strings.TrimSpace(key)
|
||||
if trimmed == "" {
|
||||
return fmt.Errorf("playbook file %s: empty binding key", path)
|
||||
return fmt.Errorf("playbook file %s: empty binding key", pos)
|
||||
}
|
||||
if value == "" {
|
||||
return fmt.Errorf("playbook file %s: key %q has no workflow name", path, trimmed)
|
||||
return fmt.Errorf("playbook file %s: key %q has no workflow name", pos, trimmed)
|
||||
}
|
||||
if err := workintake.Kind(trimmed).Validate(); err == nil {
|
||||
// It is a kind binding: the closed Kind vocabulary accepts it.
|
||||
if existing, ok := kw[workintake.Kind(trimmed)]; ok {
|
||||
return fmt.Errorf("playbook dirs: kind %q bound in two sources (%q and %q)", trimmed, existing, value)
|
||||
return fmt.Errorf("playbook file %s: kind %q bound in two sources (%q and %q)", pos, trimmed, existing, value)
|
||||
}
|
||||
kw[workintake.Kind(trimmed)] = value
|
||||
return nil
|
||||
@@ -201,10 +192,10 @@ func bindPlaybookKey(path, key, value string, kw KindWorkflows, lw LabelWorkflow
|
||||
// Otherwise it is an arbitrary-label binding. Reject kind-owned labels
|
||||
// and empty labels with the same rules the single-file loader applies.
|
||||
if _, ok := defaultKindWorkflows[workintake.Kind(trimmed)]; ok {
|
||||
return fmt.Errorf("playbook file %s: label %q is already owned by the kind routing layer", path, trimmed)
|
||||
return fmt.Errorf("playbook file %s: label %q is already owned by the kind routing layer", pos, trimmed)
|
||||
}
|
||||
if existing, ok := lw[trimmed]; ok {
|
||||
return fmt.Errorf("playbook dirs: label %q bound in two sources (%q and %q)", trimmed, existing, value)
|
||||
return fmt.Errorf("playbook file %s: label %q bound in two sources (%q and %q)", pos, trimmed, existing, value)
|
||||
}
|
||||
lw[trimmed] = value
|
||||
return nil
|
||||
@@ -357,13 +348,15 @@ func LoadLabelWorkflowsYAML(path string) (LabelWorkflows, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("read label-workflows file %s: %w", path, err)
|
||||
}
|
||||
raw := map[string]string{}
|
||||
if err := yaml.Unmarshal(data, &raw); err != nil {
|
||||
bindings, err := decodeBindings(data)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("parse label-workflows file %s: %w", path, err)
|
||||
}
|
||||
lw := make(LabelWorkflows, len(raw))
|
||||
for label, name := range raw {
|
||||
trimmed := strings.TrimSpace(label)
|
||||
lw := make(LabelWorkflows, len(bindings))
|
||||
for _, b := range bindings {
|
||||
path := fmt.Sprintf("%s:%d", path, b.line)
|
||||
name := b.value
|
||||
trimmed := strings.TrimSpace(b.key)
|
||||
if trimmed == "" {
|
||||
return nil, fmt.Errorf("label-workflows file %s: empty label is not a valid binding", path)
|
||||
}
|
||||
@@ -380,3 +373,38 @@ func LoadLabelWorkflowsYAML(path string) (LabelWorkflows, error) {
|
||||
}
|
||||
return lw, nil
|
||||
}
|
||||
|
||||
// binding is one key of a routing file with the line it was declared on.
|
||||
type binding struct {
|
||||
key, value string
|
||||
line int
|
||||
}
|
||||
|
||||
// decodeBindings decodes a routing file's top-level string map in document
|
||||
// order, so the first finding is the same on every run and each can name
|
||||
// its key's line. Values decode exactly as they did into map[string]string,
|
||||
// including yaml.v3's duplicate-key refusal.
|
||||
func decodeBindings(data []byte) ([]binding, error) {
|
||||
var doc yaml.Node
|
||||
if err := yaml.Unmarshal(data, &doc); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if len(doc.Content) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
root := doc.Content[0]
|
||||
values := map[string]string{}
|
||||
if err := root.Decode(&values); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bindings := make([]binding, 0, len(values))
|
||||
for i := 0; i+1 < len(root.Content); i += 2 {
|
||||
keyNode := root.Content[i]
|
||||
var key string
|
||||
if err := keyNode.Decode(&key); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
bindings = append(bindings, binding{key: key, value: values[key], line: keyNode.Line})
|
||||
}
|
||||
return bindings, nil
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package workflow
|
||||
|
||||
import (
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// A key-level finding names the file and the line of the offending key, so
|
||||
// the lint tool reports it compiler-style instead of leaving the author to
|
||||
// search the file.
|
||||
func TestBindingFindingsCarryTheKeyLine(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
content string
|
||||
load func(path string) error
|
||||
line string
|
||||
}{
|
||||
{
|
||||
name: "playbook dir, empty workflow name",
|
||||
content: "# routes\nsecurity: security-review\ndocs: \"\"\n",
|
||||
load: func(path string) error {
|
||||
_, _, err := LoadPlaybookDirs([]string{filepath.Dir(path)})
|
||||
return err
|
||||
},
|
||||
line: ":3:",
|
||||
},
|
||||
{
|
||||
name: "playbook dir, kind with no workflow name",
|
||||
content: "security: security-review\n\n\nbug: \"\"\n",
|
||||
load: func(path string) error {
|
||||
_, _, err := LoadPlaybookDirs([]string{filepath.Dir(path)})
|
||||
return err
|
||||
},
|
||||
line: ":4:",
|
||||
},
|
||||
{
|
||||
name: "label file, kind-owned label",
|
||||
content: "security: security-review\nfeature: other\n",
|
||||
load: func(path string) error {
|
||||
_, err := LoadLabelWorkflowsYAML(path)
|
||||
return err
|
||||
},
|
||||
line: ":2:",
|
||||
},
|
||||
{
|
||||
name: "kind file, unknown kind",
|
||||
content: "bug: tdd\n\nnot-a-kind: x\n",
|
||||
load: func(path string) error {
|
||||
_, err := LoadKindWorkflowsYAML(path)
|
||||
return err
|
||||
},
|
||||
line: ":3:",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
path := filepath.Join(t.TempDir(), "routes.yaml")
|
||||
writeFile(t, path, tt.content)
|
||||
err := tt.load(path)
|
||||
if err == nil {
|
||||
t.Fatal("load error = nil, want a finding")
|
||||
}
|
||||
if want := path + tt.line; !strings.Contains(err.Error(), want) {
|
||||
t.Fatalf("error = %q, want it to name %q", err, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user