Skip to content
This repository has been archived by the owner on Dec 10, 2024. It is now read-only.

Commit

Permalink
Create LintWithOptions
Browse files Browse the repository at this point in the history
  • Loading branch information
Robby Dyer committed Jun 13, 2022
1 parent 217f875 commit 0f14fde
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 16 deletions.
20 changes: 11 additions & 9 deletions validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@ type ValidateService struct {
client *Client
}

// LintOptions represent options for the lint API
//
// Gitlab API docs: https://docs.gitlab.com/ee/api/lint.html#validate-the-ci-yaml-configuration
type LintOptions struct {
Content string `url:"content,omitempty" json:"content,omitempty"`
IncludeMergedYAML bool `url:"include_merged_yaml,omitempty" json:"include_merged_yaml,omitempty"`
IncludeJobs bool `url:"include_jobs,omitempty" json:"include_jobs,omitempty"`
}

// LintResult represents the linting results.
//
// GitLab API docs: https://docs.gitlab.com/ce/api/lint.html
Expand Down Expand Up @@ -73,17 +82,10 @@ func (s *ValidateService) Lint(content string, options ...RequestOptionFunc) (*L
return l, resp, nil
}

// LintWithMergedYAML validates .gitlab-ci.yml content and returns the merged YAML
// LintWithOptions validates .gitlab-ci.yml content with API Options
//
// 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

func (s *ValidateService) LintWithOptions(opts *LintOptions, options ...RequestOptionFunc) (*LintResult, *Response, error) {
req, err := s.client.NewRequest(http.MethodPost, "ci/lint", &opts, options)
if err != nil {
return nil, nil, err
Expand Down
20 changes: 13 additions & 7 deletions validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,20 +85,24 @@ func TestValidate(t *testing.T) {
}
}

func TestValidateWithMergedYAML(t *testing.T) {
func TestValidateWithOptions(t *testing.T) {
testCases := []struct {
description string
content string
opts *LintOptions
response string
want *LintResult
}{
{
description: "valid",
content: `
opts: &LintOptions{
Content: `
build1:
stage: build
script:
- echo "Do your build here"`,
IncludeMergedYAML: true,
IncludeJobs: false,
},
response: `{
"status": "valid",
"errors": [],
Expand All @@ -112,9 +116,11 @@ func TestValidateWithMergedYAML(t *testing.T) {
},
{
description: "invalid",
content: `
build1:
- echo "Do your build here"`,
opts: &LintOptions{
Content: `
build1:
- echo "Do your build here"`,
},
response: `{
"status": "invalid",
"errors": ["error message when content is invalid"]
Expand All @@ -136,7 +142,7 @@ func TestValidateWithMergedYAML(t *testing.T) {
fmt.Fprint(w, tc.response)
})

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

0 comments on commit 0f14fde

Please sign in to comment.