From 8f5194d5312e031fa2d32cf0aa912a4a8e09d19b Mon Sep 17 00:00:00 2001 From: Patrick Rice Date: Wed, 8 Mar 2023 15:20:09 +0000 Subject: [PATCH] Add omitempty and test --- projects.go | 4 ++-- projects_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/projects.go b/projects.go index 011434309..e85b19224 100644 --- a/projects.go +++ b/projects.go @@ -633,7 +633,7 @@ func (s *ProjectsService) GetProjectEvents(pid interface{}, opt *GetProjectEvent // GitLab API docs: https://docs.gitlab.com/ee/api/projects.html#create-project type CreateProjectOptions struct { AllowMergeOnSkippedPipeline *bool `url:"allow_merge_on_skipped_pipeline,omitempty" json:"allow_merge_on_skipped_pipeline,omitempty"` - OnlyAllowMergeIfAllStatusChecksPassed *bool `url:"only_allow_merge_if_all_status_checks_passed" json:"only_allow_merge_if_all_status_checks_passed"` + OnlyAllowMergeIfAllStatusChecksPassed *bool `url:"only_allow_merge_if_all_status_checks_passed,omitempty" json:"only_allow_merge_if_all_status_checks_passed,omitempty"` AnalyticsAccessLevel *AccessControlValue `url:"analytics_access_level,omitempty" json:"analytics_access_level,omitempty"` ApprovalsBeforeMerge *int `url:"approvals_before_merge,omitempty" json:"approvals_before_merge,omitempty"` AutoCancelPendingPipelines *string `url:"auto_cancel_pending_pipelines,omitempty" json:"auto_cancel_pending_pipelines,omitempty"` @@ -853,7 +853,7 @@ func (s *ProjectsService) CreateProjectForUser(user int, opt *CreateProjectForUs // GitLab API docs: https://docs.gitlab.com/ee/api/projects.html#edit-project type EditProjectOptions struct { AllowMergeOnSkippedPipeline *bool `url:"allow_merge_on_skipped_pipeline,omitempty" json:"allow_merge_on_skipped_pipeline,omitempty"` - OnlyAllowMergeIfAllStatusChecksPassed *bool `url:"only_allow_merge_if_all_status_checks_passed" json:"only_allow_merge_if_all_status_checks_passed"` + OnlyAllowMergeIfAllStatusChecksPassed *bool `url:"only_allow_merge_if_all_status_checks_passed,omitempty" json:"only_allow_merge_if_all_status_checks_passed,omitempty"` AnalyticsAccessLevel *AccessControlValue `url:"analytics_access_level,omitempty" json:"analytics_access_level,omitempty"` ApprovalsBeforeMerge *int `url:"approvals_before_merge,omitempty" json:"approvals_before_merge,omitempty"` AutoCancelPendingPipelines *string `url:"auto_cancel_pending_pipelines,omitempty" json:"auto_cancel_pending_pipelines,omitempty"` diff --git a/projects_test.go b/projects_test.go index d3a1b9780..648558eaf 100644 --- a/projects_test.go +++ b/projects_test.go @@ -18,6 +18,7 @@ package gitlab import ( "bytes" + "encoding/json" "fmt" "net/http" "os" @@ -25,6 +26,8 @@ import ( "strings" "testing" "time" + + "github.com/stretchr/testify/assert" ) func TestListProjects(t *testing.T) { @@ -1361,3 +1364,25 @@ func TestCreateProjectApprovalRuleEligibleApprovers(t *testing.T) { t.Errorf("Projects.CreateProjectApprovalRule returned %+v, want %+v", rule, want) } } + +func TestProjectModelsOptionalMergeAttribute(t *testing.T) { + + // Create a `CreateProjectOptions` struct, ensure that merge attribute doesn't serialize + jsonString, err := json.Marshal(&CreateProjectOptions{ + Name: String("testProject"), + }) + if err != nil { + t.Fatal("Failed to marshal object", err) + } + assert.False(t, strings.Contains(string(jsonString), "only_allow_merge_if_all_status_checks_passed")) + + // Test the same thing but for `EditProjectOptions` struct + jsonString, err = json.Marshal(&EditProjectOptions{ + Name: String("testProject"), + }) + if err != nil { + t.Fatal("Failed to marshal object", err) + } + assert.False(t, strings.Contains(string(jsonString), "only_allow_merge_if_all_status_checks_passed")) + +}