From 6af09999ad7c39f331d046002bb2d1f09558e258 Mon Sep 17 00:00:00 2001 From: James Maguire Date: Wed, 29 Mar 2023 18:41:15 +0200 Subject: [PATCH 1/3] Add `CanAdminsBypass` to `CreateUpdateEnvironment` --- github/github-accessors.go | 8 ++++++++ github/github-accessors_test.go | 10 ++++++++++ github/repos_environments.go | 5 ++++- 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/github/github-accessors.go b/github/github-accessors.go index 9c435048ae..1a9f32ef05 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -4022,6 +4022,14 @@ func (c *CreateRunnerGroupRequest) GetVisibility() string { return *c.Visibility } +// GetCanAdminsBypass returns the CanAdminsBypass field if it's non-nil, zero value otherwise. +func (c *CreateUpdateEnvironment) GetCanAdminsBypass() bool { + if c == nil || c.CanAdminsBypass == nil { + return false + } + return *c.CanAdminsBypass +} + // GetDeploymentBranchPolicy returns the DeploymentBranchPolicy field. func (c *CreateUpdateEnvironment) GetDeploymentBranchPolicy() *BranchPolicy { if c == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index 5091cc46c9..039c3e17b0 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -4725,6 +4725,16 @@ func TestCreateRunnerGroupRequest_GetVisibility(tt *testing.T) { c.GetVisibility() } +func TestCreateUpdateEnvironment_GetCanAdminsBypass(tt *testing.T) { + var zeroValue bool + c := &CreateUpdateEnvironment{CanAdminsBypass: &zeroValue} + c.GetCanAdminsBypass() + c = &CreateUpdateEnvironment{} + c.GetCanAdminsBypass() + c = nil + c.GetCanAdminsBypass() +} + func TestCreateUpdateEnvironment_GetDeploymentBranchPolicy(tt *testing.T) { c := &CreateUpdateEnvironment{} c.GetDeploymentBranchPolicy() diff --git a/github/repos_environments.go b/github/repos_environments.go index 7f01be6277..56bb3a1390 100644 --- a/github/repos_environments.go +++ b/github/repos_environments.go @@ -163,10 +163,13 @@ func (c *CreateUpdateEnvironment) MarshalJSON() ([]byte, error) { // CreateUpdateEnvironment represents the fields required for the create/update operation // following the Create/Update release example. // See https://github.com/google/go-github/issues/992 for more information. -// Removed omitempty here as the API expects null values for reviewers and deployment_branch_policy to clear them. +// Removed omitempty here for for reviewers and deployment_branch_policy as the API expects null values to clear them. +// WaitTimer is set to 0 by the marshaller when empty. +// The API does not accept a null value for can_admins_bypass, so an empty value here is omitted. type CreateUpdateEnvironment struct { WaitTimer *int `json:"wait_timer"` Reviewers []*EnvReviewers `json:"reviewers"` + CanAdminsBypass *bool `json:"can_admins_bypass,omitempty"` DeploymentBranchPolicy *BranchPolicy `json:"deployment_branch_policy"` } From 7fea45a15eb553e164c6f04e027dbb64781bec09 Mon Sep 17 00:00:00 2001 From: James Maguire Date: Wed, 29 Mar 2023 20:32:25 +0200 Subject: [PATCH 2/3] align PUT functionality --- github/repos_environments.go | 10 ++++++---- github/repos_environments_test.go | 4 ++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/github/repos_environments.go b/github/repos_environments.go index 56bb3a1390..fc4b450f1a 100644 --- a/github/repos_environments.go +++ b/github/repos_environments.go @@ -148,11 +148,15 @@ func (s *RepositoriesService) GetEnvironment(ctx context.Context, owner, repo, n // MarshalJSON implements the json.Marshaler interface. // As the only way to clear a WaitTimer is to set it to 0, a missing WaitTimer object should default to 0, not null. +// As the default value for CanAdminBypass is true, a nil value here marshals to true. func (c *CreateUpdateEnvironment) MarshalJSON() ([]byte, error) { type Alias CreateUpdateEnvironment if c.WaitTimer == nil { c.WaitTimer = Int(0) } + if c.CanAdminsBypass == nil { + c.CanAdminsBypass = Bool(true) + } return json.Marshal(&struct { *Alias }{ @@ -163,13 +167,11 @@ func (c *CreateUpdateEnvironment) MarshalJSON() ([]byte, error) { // CreateUpdateEnvironment represents the fields required for the create/update operation // following the Create/Update release example. // See https://github.com/google/go-github/issues/992 for more information. -// Removed omitempty here for for reviewers and deployment_branch_policy as the API expects null values to clear them. -// WaitTimer is set to 0 by the marshaller when empty. -// The API does not accept a null value for can_admins_bypass, so an empty value here is omitted. +// Removed omitempty here as the API expects null values for reviewers and deployment_branch_policy to clear them. type CreateUpdateEnvironment struct { WaitTimer *int `json:"wait_timer"` Reviewers []*EnvReviewers `json:"reviewers"` - CanAdminsBypass *bool `json:"can_admins_bypass,omitempty"` + CanAdminsBypass *bool `json:"can_admins_bypass"` DeploymentBranchPolicy *BranchPolicy `json:"deployment_branch_policy"` } diff --git a/github/repos_environments_test.go b/github/repos_environments_test.go index a688b221fc..0f93d29088 100644 --- a/github/repos_environments_test.go +++ b/github/repos_environments_test.go @@ -93,7 +93,7 @@ func TestCreateUpdateEnvironment_MarshalJSON(t *testing.T) { t.Errorf("MarshalJSON: %v", err) } - want := `{"wait_timer":0,"reviewers":null,"deployment_branch_policy":null}` + want := `{"wait_timer":0,"reviewers":null,"can_admins_bypass":true,"deployment_branch_policy":null}` if string(got) != want { t.Errorf("MarshalJSON = %s, want %v", got, want) } @@ -187,7 +187,7 @@ func TestRepositoriesService_CreateEnvironment(t *testing.T) { json.NewDecoder(r.Body).Decode(v) testMethod(t, r, "PUT") - want := &CreateUpdateEnvironment{WaitTimer: Int(30)} + want := &CreateUpdateEnvironment{WaitTimer: Int(30), CanAdminsBypass: Bool(true)} if !cmp.Equal(v, want) { t.Errorf("Request body = %+v, want %+v", v, want) } From 1973bc4cfbb8e582c54e041ac111f603b0e63b4e Mon Sep 17 00:00:00 2001 From: James Maguire Date: Wed, 29 Mar 2023 20:36:09 +0200 Subject: [PATCH 3/3] fix typo --- github/repos_environments.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/repos_environments.go b/github/repos_environments.go index fc4b450f1a..2399a42c74 100644 --- a/github/repos_environments.go +++ b/github/repos_environments.go @@ -148,7 +148,7 @@ func (s *RepositoriesService) GetEnvironment(ctx context.Context, owner, repo, n // MarshalJSON implements the json.Marshaler interface. // As the only way to clear a WaitTimer is to set it to 0, a missing WaitTimer object should default to 0, not null. -// As the default value for CanAdminBypass is true, a nil value here marshals to true. +// As the default value for CanAdminsBypass is true, a nil value here marshals to true. func (c *CreateUpdateEnvironment) MarshalJSON() ([]byte, error) { type Alias CreateUpdateEnvironment if c.WaitTimer == nil {