Skip to content

Commit

Permalink
Merge pull request #1497 from robbydyer/lint_merged_yaml
Browse files Browse the repository at this point in the history
Add support for merged YAML return from linter
  • Loading branch information
svanharmelen committed Jun 20, 2022
2 parents 0da8284 + df03464 commit 7a7ea97
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 20 deletions.
5 changes: 4 additions & 1 deletion jobs.go
Expand Up @@ -103,7 +103,10 @@ type Bridge struct {
DownstreamPipeline *PipelineInfo `json:"downstream_pipeline"`
}

// ListJobsOptions are options for two list apis
// ListJobsOptions represents the available ListProjectJobs() options.
//
// GitLab API docs:
// https://docs.gitlab.com/ce/api/jobs.html#list-project-jobs
type ListJobsOptions struct {
ListOptions
Scope *[]BuildStateValue `url:"scope[],omitempty" json:"scope,omitempty"`
Expand Down
20 changes: 13 additions & 7 deletions validate.go
Expand Up @@ -50,15 +50,21 @@ type ProjectLintResult struct {
MergedYaml string `json:"merged_yaml"`
}

// Lint validates .gitlab-ci.yml content.
// LintOptions represents the available Lint() options.
//
// GitLab API docs: https://docs.gitlab.com/ce/api/lint.html
func (s *ValidateService) Lint(content string, options ...RequestOptionFunc) (*LintResult, *Response, error) {
var opts struct {
Content string `url:"content,omitempty" json:"content,omitempty"`
}
opts.Content = content
// 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"`
}

// Lint validates .gitlab-ci.yml content.
//
// Gitlab API docs:
// https://docs.gitlab.com/ee/api/lint.html#validate-the-ci-yaml-configuration
func (s *ValidateService) Lint(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
29 changes: 17 additions & 12 deletions validate_test.go
Expand Up @@ -26,31 +26,39 @@ import (
func TestValidate(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": []
"errors": [],
"merged_yaml":"---\nbuild1:\n stage: build\n script:\n - echo\"Do your build here\""
}`,
want: &LintResult{
Status: "valid",
Errors: []string{},
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"`,
opts: &LintOptions{
Content: `
build1:
- echo "Do your build here"`,
},
response: `{
"status": "invalid",
"errors": ["error message when content is invalid"]
Expand All @@ -72,8 +80,7 @@ func TestValidate(t *testing.T) {
fmt.Fprint(w, tc.response)
})

got, _, err := client.Validate.Lint(tc.content)

got, _, err := client.Validate.Lint(tc.opts)
if err != nil {
t.Errorf("Validate returned error: %v", err)
}
Expand Down Expand Up @@ -136,7 +143,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 +213,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

0 comments on commit 7a7ea97

Please sign in to comment.