Skip to content

Commit

Permalink
gitlab: fix parsing error on scalar includes (#86)
Browse files Browse the repository at this point in the history
* fix: parse scalar `include` in gitlab configs

* tests
  • Loading branch information
becojo authored May 15, 2024
1 parent ccbd195 commit 8aff818
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 1 deletion.
2 changes: 1 addition & 1 deletion models/gitlab.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ func (o *GitlabciIncludeItems) UnmarshalYAML(node *yaml.Node) error {
if err := node.Decode(&includes); err != nil {
return err
}
case yaml.MappingNode:
case yaml.MappingNode, yaml.ScalarNode:
var include GitlabciIncludeItem
if err := node.Decode(&include); err != nil {
return err
Expand Down
70 changes: 70 additions & 0 deletions models/gitlab_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,73 @@ deploy:
assert.Equal(t, "main", config.Include[3].Ref)
assert.Equal(t, "/templates/.gitlab-ci-template.yml", config.Include[3].File[0])
}

func TestGitlabIncludes(t *testing.T) {
subjects := []struct {
config string
expected GitlabciIncludeItems
}{
{
config: `include: https://example.com`,
expected: []GitlabciIncludeItem{
{Remote: "https://example.com"},
},
},
{
config: `include: local.yml`,
expected: []GitlabciIncludeItem{
{Local: "local.yml"},
},
},
{
config: `include: [https://example.com]`,
expected: []GitlabciIncludeItem{
{Remote: "https://example.com"},
},
},
{
config: `include: [local.yml]`,
expected: []GitlabciIncludeItem{
{Local: "local.yml"},
},
},
{
config: `include: [{local: local.yml}]`,
expected: []GitlabciIncludeItem{
{Local: "local.yml"},
},
},
{
config: `include: [{remote: http://example.com}]`,
expected: []GitlabciIncludeItem{
{Remote: "http://example.com"},
},
},
{
config: `include: [{template: Auto-DevOps.gitlab-ci.yml}]`,
expected: []GitlabciIncludeItem{
{Template: "Auto-DevOps.gitlab-ci.yml"},
},
},
{
config: `include: [{project: my-group/my-project, ref: main, file: /templates/.gitlab-ci-template.yml}]`,
expected: []GitlabciIncludeItem{
{
Project: "my-group/my-project",
Ref: "main",
File: []string{"/templates/.gitlab-ci-template.yml"},
},
},
},
{
config: `{}`,
expected: nil,
},
}

for _, subject := range subjects {
config, err := ParseGitlabciConfig([]byte(subject.config))
assert.Nil(t, err)
assert.Equal(t, subject.expected, config.Include)
}
}

0 comments on commit 8aff818

Please sign in to comment.