Skip to content

Commit 8aff818

Browse files
authored
gitlab: fix parsing error on scalar includes (#86)
* fix: parse scalar `include` in gitlab configs * tests
1 parent ccbd195 commit 8aff818

File tree

2 files changed

+71
-1
lines changed

2 files changed

+71
-1
lines changed

models/gitlab.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ func (o *GitlabciIncludeItems) UnmarshalYAML(node *yaml.Node) error {
324324
if err := node.Decode(&includes); err != nil {
325325
return err
326326
}
327-
case yaml.MappingNode:
327+
case yaml.MappingNode, yaml.ScalarNode:
328328
var include GitlabciIncludeItem
329329
if err := node.Decode(&include); err != nil {
330330
return err

models/gitlab_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,3 +139,73 @@ deploy:
139139
assert.Equal(t, "main", config.Include[3].Ref)
140140
assert.Equal(t, "/templates/.gitlab-ci-template.yml", config.Include[3].File[0])
141141
}
142+
143+
func TestGitlabIncludes(t *testing.T) {
144+
subjects := []struct {
145+
config string
146+
expected GitlabciIncludeItems
147+
}{
148+
{
149+
config: `include: https://example.com`,
150+
expected: []GitlabciIncludeItem{
151+
{Remote: "https://example.com"},
152+
},
153+
},
154+
{
155+
config: `include: local.yml`,
156+
expected: []GitlabciIncludeItem{
157+
{Local: "local.yml"},
158+
},
159+
},
160+
{
161+
config: `include: [https://example.com]`,
162+
expected: []GitlabciIncludeItem{
163+
{Remote: "https://example.com"},
164+
},
165+
},
166+
{
167+
config: `include: [local.yml]`,
168+
expected: []GitlabciIncludeItem{
169+
{Local: "local.yml"},
170+
},
171+
},
172+
{
173+
config: `include: [{local: local.yml}]`,
174+
expected: []GitlabciIncludeItem{
175+
{Local: "local.yml"},
176+
},
177+
},
178+
{
179+
config: `include: [{remote: http://example.com}]`,
180+
expected: []GitlabciIncludeItem{
181+
{Remote: "http://example.com"},
182+
},
183+
},
184+
{
185+
config: `include: [{template: Auto-DevOps.gitlab-ci.yml}]`,
186+
expected: []GitlabciIncludeItem{
187+
{Template: "Auto-DevOps.gitlab-ci.yml"},
188+
},
189+
},
190+
{
191+
config: `include: [{project: my-group/my-project, ref: main, file: /templates/.gitlab-ci-template.yml}]`,
192+
expected: []GitlabciIncludeItem{
193+
{
194+
Project: "my-group/my-project",
195+
Ref: "main",
196+
File: []string{"/templates/.gitlab-ci-template.yml"},
197+
},
198+
},
199+
},
200+
{
201+
config: `{}`,
202+
expected: nil,
203+
},
204+
}
205+
206+
for _, subject := range subjects {
207+
config, err := ParseGitlabciConfig([]byte(subject.config))
208+
assert.Nil(t, err)
209+
assert.Equal(t, subject.expected, config.Include)
210+
}
211+
}

0 commit comments

Comments
 (0)