From 42b5002d33e3924ba11a4fc6207d86274bb7390f Mon Sep 17 00:00:00 2001 From: Robby Dyer Date: Fri, 22 Mar 2024 15:06:44 -0400 Subject: [PATCH 1/2] Add missing params to ProjectLint --- validate.go | 2 ++ validate_test.go | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/validate.go b/validate.go index 1a0f56a14..d92bba849 100644 --- a/validate.go +++ b/validate.go @@ -124,6 +124,8 @@ type ProjectLintOptions struct { DryRun *bool `url:"dry_run,omitempty" json:"dry_run,omitempty"` IncludeJobs *bool `url:"include_jobs,omitempty" json:"include_jobs,omitempty"` Ref *string `url:"ref,omitempty" json:"ref,omitempty"` + ContentRef *string `url:"content_ref" json:"content_ref,omitempty"` + DryRunRef *string `url:"dry_run_ref" json:"dry_run_ref,omitempty"` } // ProjectLint validates .gitlab-ci.yml content by project. diff --git a/validate_test.go b/validate_test.go index ab4bfc350..20171e24f 100644 --- a/validate_test.go +++ b/validate_test.go @@ -223,3 +223,54 @@ func TestValidateProjectNamespace(t *testing.T) { }) } } + +func TestValidateProjectLint(t *testing.T) { + testCases := []struct { + description string + request *ProjectLintOptions + response string + want *ProjectLintResult + }{ + { + description: "valid", + request: &ProjectLintOptions{ + DryRun: Ptr(false), + IncludeJobs: Ptr(true), + ContentRef: Ptr("foo"), + }, + response: `{ + "valid": true, + "errors": [], + "warnings": [], + "merged_yaml": "---\n:build:\n :script:\n - echo build" + }`, + want: &ProjectLintResult{ + Valid: true, + Warnings: []string{}, + Errors: []string{}, + MergedYaml: "---\n:build:\n :script:\n - echo build", + }, + }, + } + + for _, tc := range testCases { + t.Run(tc.description, func(t *testing.T) { + mux, client := setup(t) + + mux.HandleFunc("/api/v4/projects/1/ci/lint", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, http.MethodGet) + fmt.Fprint(w, tc.response) + }) + + got, _, err := client.Validate.ProjectLint(1, tc.request) + 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)) + } + }) + } +} From 7be424ff4df9fffe6fc44a8390d99230e30c1d0c Mon Sep 17 00:00:00 2001 From: Sander van Harmelen Date: Sat, 23 Mar 2024 10:52:42 +0100 Subject: [PATCH 2/2] Order and add omitempty as this is a request struct --- validate.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/validate.go b/validate.go index d92bba849..cb79ac838 100644 --- a/validate.go +++ b/validate.go @@ -121,11 +121,11 @@ func (s *ValidateService) ProjectNamespaceLint(pid interface{}, opt *ProjectName // GitLab API docs: // https://docs.gitlab.com/ee/api/lint.html#validate-a-projects-ci-configuration type ProjectLintOptions struct { + ContentRef *string `url:"content_ref,omitempty" json:"content_ref,omitempty"` + DryRunRef *string `url:"dry_run_ref,omitempty" json:"dry_run_ref,omitempty"` DryRun *bool `url:"dry_run,omitempty" json:"dry_run,omitempty"` IncludeJobs *bool `url:"include_jobs,omitempty" json:"include_jobs,omitempty"` Ref *string `url:"ref,omitempty" json:"ref,omitempty"` - ContentRef *string `url:"content_ref" json:"content_ref,omitempty"` - DryRunRef *string `url:"dry_run_ref" json:"dry_run_ref,omitempty"` } // ProjectLint validates .gitlab-ci.yml content by project.