mirror of
https://code.forgejo.org/forgejo/runner.git
synced 2025-10-10 19:32:04 +00:00
fix: schema validation must accept aliases and merges (#1012)
- aliases are an indirection to follow https://yaml.org/spec/1.2.1/#id2785586 - merges cannot be conveniently validated and are skipped https://yaml.org/type/merge.html Resolves forgejo/runner#1011 <!--start release-notes-assistant--> <!--URL:https://code.forgejo.org/forgejo/runner--> - bug fixes - [PR](https://code.forgejo.org/forgejo/runner/pulls/1012): <!--number 1012 --><!--line 0 --><!--description Zml4OiBzY2hlbWEgdmFsaWRhdGlvbiBtdXN0IGFjY2VwdCBhbGlhc2VzIGFuZCBtZXJnZXM=-->fix: schema validation must accept aliases and merges<!--description--> <!--end release-notes-assistant--> Reviewed-on: https://code.forgejo.org/forgejo/runner/pulls/1012 Reviewed-by: Michael Kriese <michael.kriese@gmx.de> Co-authored-by: Earl Warren <contact@earl-warren.org> Co-committed-by: Earl Warren <contact@earl-warren.org>
This commit is contained in:
parent
797e0cd250
commit
b8ab05e367
2 changed files with 48 additions and 0 deletions
|
@ -237,6 +237,9 @@ func (s *Node) UnmarshalYAML(node *yaml.Node) error {
|
||||||
if node != nil && node.Kind == yaml.DocumentNode {
|
if node != nil && node.Kind == yaml.DocumentNode {
|
||||||
return s.UnmarshalYAML(node.Content[0])
|
return s.UnmarshalYAML(node.Content[0])
|
||||||
}
|
}
|
||||||
|
if node != nil && node.Kind == yaml.AliasNode {
|
||||||
|
return s.UnmarshalYAML(node.Alias)
|
||||||
|
}
|
||||||
def := s.Schema.GetDefinition(s.Definition)
|
def := s.Schema.GetDefinition(s.Definition)
|
||||||
if s.Context == nil {
|
if s.Context == nil {
|
||||||
s.Context = def.Context
|
s.Context = def.Context
|
||||||
|
@ -360,6 +363,15 @@ func (s *Node) checkMapping(node *yaml.Node, def Definition) error {
|
||||||
if node.Kind != yaml.MappingNode {
|
if node.Kind != yaml.MappingNode {
|
||||||
return fmt.Errorf("%sExpected a mapping got %v", formatLocation(node), getStringKind(node.Kind))
|
return fmt.Errorf("%sExpected a mapping got %v", formatLocation(node), getStringKind(node.Kind))
|
||||||
}
|
}
|
||||||
|
// merges cannot be conveniently validated and are skipped
|
||||||
|
// https://yaml.org/type/merge.html
|
||||||
|
for i, n := range node.Content {
|
||||||
|
if i%2 == 0 {
|
||||||
|
if n.Kind == yaml.ScalarNode && n.Value == "<<" && (n.Tag == "" || n.ShortTag() == "!!merge") {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
insertDirective := regexp.MustCompile(`\${{\s*insert\s*}}`)
|
insertDirective := regexp.MustCompile(`\${{\s*insert\s*}}`)
|
||||||
var allErrors error
|
var allErrors error
|
||||||
for i, k := range node.Content {
|
for i, k := range node.Content {
|
||||||
|
|
|
@ -243,3 +243,39 @@ runs:
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// https://yaml.org/spec/1.2.1/#id2785586
|
||||||
|
// An anchor is denoted by the “&” indicator. It marks a node for future reference.
|
||||||
|
// https://yaml.org/type/merge.html
|
||||||
|
// Specify one or more mappings to be merged with the current one.
|
||||||
|
func TestSchema_AnchorAndReference(t *testing.T) {
|
||||||
|
var node yaml.Node
|
||||||
|
err := yaml.Unmarshal([]byte(`
|
||||||
|
on: [push]
|
||||||
|
jobs:
|
||||||
|
test1:
|
||||||
|
runs-on: docker
|
||||||
|
steps:
|
||||||
|
- &step
|
||||||
|
run: echo All good!
|
||||||
|
- *step
|
||||||
|
test2:
|
||||||
|
runs-on: docker
|
||||||
|
steps:
|
||||||
|
- << : *step
|
||||||
|
name: other name
|
||||||
|
test3:
|
||||||
|
runs-on: docker
|
||||||
|
steps:
|
||||||
|
- !!merge << : *step
|
||||||
|
name: other name
|
||||||
|
`), &node)
|
||||||
|
if !assert.NoError(t, err) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
err = (&Node{
|
||||||
|
Definition: "workflow-root",
|
||||||
|
Schema: GetWorkflowSchema(),
|
||||||
|
}).UnmarshalYAML(&node)
|
||||||
|
assert.NoError(t, err)
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue