Repository: xanzy/go-gitlab
Pull request #1497 (2022-06-12 03:09:09)

Add support for merged YAML return from linter
The lint API requires setting the `include_merged_yaml` option to true to in order for it to return the merged YAML. Since the existing `Lint` function doesn't take options as a parameter, this adds a new function that sets that option to true. https://docs.gitlab.com/ee/api/lint.html#validate-the-ci-yaml-configuration


Patch:
diff --git a/jobs.go b/jobs.go
index 5fe3d3f..b238fc8 100644
--- a/jobs.go
+++ b/jobs.go
@@ -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"`
diff --git a/validate.go b/validate.go
index 75b0c75..23650f8 100644
--- a/validate.go
+++ b/validate.go
@@ -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


Test patch:
diff --git a/validate_test.go b/validate_test.go
index be44b73..b63421a 100644
--- a/validate_test.go
+++ b/validate_test.go
@@ -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"]
@@ -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)
 			}
@@ -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)
 			}
@@ -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)
 			}