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 all commits
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
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