Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for merged YAML return from linter #1497

Merged
merged 3 commits into from Jun 20, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 25 additions & 0 deletions validate.go
Expand Up @@ -73,6 +73,31 @@ func (s *ValidateService) Lint(content string, options ...RequestOptionFunc) (*L
return l, resp, nil
}

// LintWithMergedYAML validates .gitlab-ci.yml content and returns the merged YAML
//
// GitLab API docs: https://docs.gitlab.com/ce/api/lint.html
func (s *ValidateService) LintWithMergedYAML(content string, options ...RequestOptionFunc) (*LintResult, *Response, error) {
var opts struct {
Content string `url:"content,omitempty" json:"content,omitempty"`
IncludeMergedYAML bool `url:"include_merged_yaml,omitempty" json:"include_merged_yaml,omitempty"`
}
opts.Content = content
opts.IncludeMergedYAML = true

req, err := s.client.NewRequest(http.MethodPost, "ci/lint", &opts, options)
if err != nil {
return nil, nil, err
}

l := new(LintResult)
resp, err := s.client.Do(req, l)
if err != nil {
return nil, resp, err
}

return l, resp, nil
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of making a dedicated function like this the general approach is to make an option struct and pass that into the existing Lint function.

I do notice that the existing Lint function already does this different, but the best way forward is to correct that (now that it caught my eye 😉) instead of repeating it...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you prefer I make an entirely new function to maintain backward compatibility? Like LintWithOptions or something?


// ProjectNamespaceLintOptions represents the available ProjectNamespaceLint() options.
//
// GitLab API docs:
Expand Down
65 changes: 63 additions & 2 deletions validate_test.go
Expand Up @@ -73,7 +73,70 @@ func TestValidate(t *testing.T) {
})

got, _, err := client.Validate.Lint(tc.content)
if err != nil {
t.Errorf("Validate returned error: %v", err)
}

want := tc.want
if !reflect.DeepEqual(got, want) {
t.Errorf("Validate returned \ngot:\n%v\nwant:\n%v", Stringify(got), Stringify(want))
}
})
}
}

func TestValidateWithMergedYAML(t *testing.T) {
testCases := []struct {
description string
content string
response string
want *LintResult
}{
{
description: "valid",
content: `
build1:
stage: build
script:
- echo "Do your build here"`,
response: `{
"status": "valid",
"errors": [],
"merged_yaml":"---\nbuild1:\n stage: build\n script:\n - echo\"Do your build here\""
}`,
want: &LintResult{
Status: "valid",
MergedYaml: "---\nbuild1:\n stage: build\n script:\n - echo\"Do your build here\"",
Errors: []string{},
},
},
{
description: "invalid",
content: `
build1:
- echo "Do your build here"`,
response: `{
"status": "invalid",
"errors": ["error message when content is invalid"]
}`,
want: &LintResult{
Status: "invalid",
Errors: []string{"error message when content is invalid"},
},
},
}

for _, tc := range testCases {
t.Run(tc.description, func(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)

mux.HandleFunc("/api/v4/ci/lint", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
fmt.Fprint(w, tc.response)
})

got, _, err := client.Validate.LintWithMergedYAML(tc.content)
if err != nil {
t.Errorf("Validate returned error: %v", err)
}
Expand Down Expand Up @@ -136,7 +199,6 @@ func TestValidateProject(t *testing.T) {

opt := &ProjectLintOptions{}
got, _, err := client.Validate.ProjectLint(1, opt)

if err != nil {
t.Errorf("Validate returned error: %v", err)
}
Expand Down Expand Up @@ -207,7 +269,6 @@ func TestValidateProjectNamespace(t *testing.T) {
})

got, _, err := client.Validate.ProjectNamespaceLint(1, tc.request)

if err != nil {
t.Errorf("Validate returned error: %v", err)
}
Expand Down