From 948b6bf9664b9706f5fd008b6db2538ac2d713f2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20Vl=C3=A9rick?= Date: Wed, 31 May 2023 12:43:53 +0200 Subject: [PATCH 1/4] Add approval_rules in protected_environments --- go.sum | 2 - protected_environments.go | 34 +++++++++++-- protected_environments_test.go | 91 +++++++++++++++++++++++++++++++--- 3 files changed, 116 insertions(+), 11 deletions(-) diff --git a/go.sum b/go.sum index a3dbd73bd..227c040f1 100644 --- a/go.sum +++ b/go.sum @@ -43,8 +43,6 @@ google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6 google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= -google.golang.org/protobuf v1.29.0 h1:44S3JjaKmLEE4YIkjzexaP+NzZsudE3Zin5Njn/pYX0= -google.golang.org/protobuf v1.29.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= google.golang.org/protobuf v1.29.1 h1:7QBf+IK2gx70Ap/hDsOmam3GE0v9HicjfEdAxE62UoM= google.golang.org/protobuf v1.29.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= diff --git a/protected_environments.go b/protected_environments.go index 5e5962bd4..88269fe7a 100644 --- a/protected_environments.go +++ b/protected_environments.go @@ -38,6 +38,7 @@ type ProtectedEnvironment struct { Name string `json:"name"` DeployAccessLevels []*EnvironmentAccessDescription `json:"deploy_access_levels"` RequiredApprovalCount int `json:"required_approval_count"` + ApprovalRules []*EnvironmentApprovalRule `json:"approval_rules"` } // EnvironmentAccessDescription represents the access decription for a protected @@ -52,6 +53,19 @@ type EnvironmentAccessDescription struct { GroupID int `json:"group_id"` } +// EnvironmentApprovalRule represents the approval rules for a protected environment. +// +// GitLab API docs: +// https://docs.gitlab.com/ee/api/protected_environments.html#protect-a-single-environment +type EnvironmentApprovalRule struct { + UserID int `json:"user_id"` + GroupID int `json:"group_id"` + AccessLevel AccessLevelValue `json:"access_level"` + AccessLevelDescription string `json:"access_level_description"` + RequiredApprovalCount int `json:"required_approvals"` + GroupInheritanceType int `json:"group_inheritance_type"` +} + // ListProtectedEnvironmentsOptions represents the available // ListProtectedEnvironments() options. // @@ -117,9 +131,10 @@ func (s *ProtectedEnvironmentsService) GetProtectedEnvironment(pid interface{}, // GitLab API docs: // https://docs.gitlab.com/ee/api/protected_environments.html#protect-a-single-environment type ProtectRepositoryEnvironmentsOptions struct { - Name *string `url:"name,omitempty" json:"name,omitempty"` - DeployAccessLevels *[]*EnvironmentAccessOptions `url:"deploy_access_levels,omitempty" json:"deploy_access_levels,omitempty"` - RequiredApprovalCount *int `url:"required_approval_count,omitempty" json:"required_approval_count,omitempty"` + Name *string `url:"name,omitempty" json:"name,omitempty"` + DeployAccessLevels *[]*EnvironmentAccessOptions `url:"deploy_access_levels,omitempty" json:"deploy_access_levels,omitempty"` + RequiredApprovalCount *int `url:"required_approval_count,omitempty" json:"required_approval_count,omitempty"` + ApprovalRules *[]*EnvironmentApprovalRuleOptions `url:"approval_rules,omitempty" json:"approval_rules,omitempty"` } // EnvironmentAccessOptions represents the options for an access decription for @@ -133,6 +148,19 @@ type EnvironmentAccessOptions struct { GroupID *int `url:"group_id,omitempty" json:"group_id,omitempty"` } +// EnvironmentApprovalRuleOptions represents the approval rules for a protected environment. +// +// GitLab API docs: +// https://docs.gitlab.com/ee/api/protected_environments.html#protect-a-single-environment +type EnvironmentApprovalRuleOptions struct { + UserID *int `url:"user_id,omitempty" json:"user_id,omitempty"` + GroupID *int `url:"group_id,omitempty" json:"group_id,omitempty"` + AccessLevel *AccessLevelValue `url:"access_level,omitempty" json:"access_level,omitempty"` + AccessLevelDescription *string `url:"access_level_description,omitempty" json:"access_level_description,omitempty"` + RequiredApprovalCount *int `url:"required_approvals,omitempty" json:"required_approvals,omitempty"` + GroupInheritanceType *int `url:"group_inheritance_type,omitempty" json:"group_inheritance_type,omitempty"` +} + // ProtectRepositoryEnvironments protects a single repository environment or // several project repository environments using wildcard protected environment. // diff --git a/protected_environments_test.go b/protected_environments_test.go index dac5e24bc..1c579b6fe 100644 --- a/protected_environments_test.go +++ b/protected_environments_test.go @@ -37,7 +37,27 @@ func TestListProtectedEnvironments(t *testing.T) { "access_level_description": "Maintainers" } ], - "required_approval_count": 1 + "required_approval_count": 1, + "approval_rules": [ + { + "id": 38, + "user_id": 42, + "group_id": null, + "access_level": null, + "access_level_description": "qa-group", + "required_approvals": 1, + "group_inheritance_type": 0 + }, + { + "id": 39, + "user_id": null, + "group_id": 135, + "access_level": 30, + "access_level_description": "security-group", + "required_approvals": 2, + "group_inheritance_type": 1 + } + ] },{ "name":"*-release", "deploy_access_levels": [ @@ -59,6 +79,20 @@ func TestListProtectedEnvironments(t *testing.T) { }, }, RequiredApprovalCount: 1, + ApprovalRules: []*EnvironmentApprovalRule{ + { + UserID: 42, + AccessLevelDescription: "qa-group", + RequiredApprovalCount: 1, + }, + { + GroupID: 135, + AccessLevel: 30, + AccessLevelDescription: "security-group", + RequiredApprovalCount: 2, + GroupInheritanceType: 1, + }, + }, }, { Name: "*-release", @@ -93,7 +127,18 @@ func TestGetProtectedEnvironment(t *testing.T) { "access_level_description": "Developers + Maintainers" } ], - "required_approval_count": 1 + "required_approval_count": 1, + "approval_rules": [ + { + "id": 1, + "user_id": null, + "group_id": 10, + "access_level": 5, + "access_level_description": "devops", + "required_approvals": 0, + "group_inheritance_type": 0 + } + ] }`) }) @@ -106,13 +151,20 @@ func TestGetProtectedEnvironment(t *testing.T) { }, }, RequiredApprovalCount: 1, + ApprovalRules: []*EnvironmentApprovalRule{ + { + GroupID: 10, + AccessLevel: 5, + AccessLevelDescription: "devops", + }, + }, } environment, _, err := client.ProtectedEnvironments.GetProtectedEnvironment(1, environmentName) assert.NoError(t, err, "failed to get response") assert.Equal(t, expected, environment) - // Test without RequiredApprovalCount + // Test without RequiredApprovalCount nor ApprovalRules environmentName = "my-awesome-environment2" mux.HandleFunc(fmt.Sprintf("/api/v4/projects/2/protected_environments/%s", environmentName), func(w http.ResponseWriter, r *http.Request) { @@ -146,7 +198,7 @@ func TestGetProtectedEnvironment(t *testing.T) { func TestProtectRepositoryEnvironments(t *testing.T) { mux, client := setup(t) - // Test with RequiredApprovalCount + // Test with RequiredApprovalCount and ApprovalRules mux.HandleFunc("/api/v4/projects/1/protected_environments", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodPost) fmt.Fprint(w, `{ @@ -157,7 +209,18 @@ func TestProtectRepositoryEnvironments(t *testing.T) { "access_level_description": "Developers + Maintainers" } ], - "required_approval_count": 2 + "required_approval_count": 2, + "approval_rules": [ + { + "id": 1, + "user_id": null, + "group_id": 10, + "access_level": 5, + "access_level_description": "devops", + "required_approvals": 0, + "group_inheritance_type": 0 + } + ] }`) }) @@ -170,21 +233,37 @@ func TestProtectRepositoryEnvironments(t *testing.T) { }, }, RequiredApprovalCount: 2, + ApprovalRules: []*EnvironmentApprovalRule{ + { + GroupID: 10, + AccessLevel: 5, + AccessLevelDescription: "devops", + }, + }, } + noPermissions := NoPermissions + opt := &ProtectRepositoryEnvironmentsOptions{ Name: String("my-awesome-environment"), DeployAccessLevels: &[]*EnvironmentAccessOptions{ {AccessLevel: AccessLevel(30)}, }, RequiredApprovalCount: Int(2), + ApprovalRules: &[]*EnvironmentApprovalRuleOptions{ + { + GroupID: Int(10), + AccessLevel: &noPermissions, + AccessLevelDescription: String("devops"), + }, + }, } environment, _, err := client.ProtectedEnvironments.ProtectRepositoryEnvironments(1, opt) assert.NoError(t, err, "failed to get response") assert.Equal(t, expected, environment) - // Test without RequiredApprovalCount + // Test without RequiredApprovalCount nor ApprovalRules mux.HandleFunc("/api/v4/projects/2/protected_environments", func(w http.ResponseWriter, r *http.Request) { testMethod(t, r, http.MethodPost) fmt.Fprint(w, `{ From 542beb6b067653893cdd46df7c19989edfc4153f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20Vl=C3=A9rick?= Date: Wed, 31 May 2023 20:16:11 +0200 Subject: [PATCH 2/4] Review comments --- go.sum | 2 ++ protected_environments.go | 1 + protected_environments_test.go | 6 +++--- 3 files changed, 6 insertions(+), 3 deletions(-) diff --git a/go.sum b/go.sum index 227c040f1..a3dbd73bd 100644 --- a/go.sum +++ b/go.sum @@ -43,6 +43,8 @@ google.golang.org/appengine v1.6.7 h1:FZR1q0exgwxzPzp/aF+VccGrSfxfPpkBqjIIEq3ru6 google.golang.org/appengine v1.6.7/go.mod h1:8WjMMxjGQR8xUklV/ARdw2HLXBOI7O7uCIDZVag1xfc= google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw= google.golang.org/protobuf v1.26.0/go.mod h1:9q0QmTI4eRPtz6boOQmLYwt+qCgq0jsYwAQnmE0givc= +google.golang.org/protobuf v1.29.0 h1:44S3JjaKmLEE4YIkjzexaP+NzZsudE3Zin5Njn/pYX0= +google.golang.org/protobuf v1.29.0/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= google.golang.org/protobuf v1.29.1 h1:7QBf+IK2gx70Ap/hDsOmam3GE0v9HicjfEdAxE62UoM= google.golang.org/protobuf v1.29.1/go.mod h1:HV8QOd/L58Z+nl8r43ehVNZIU/HEI6OcFqwMG9pJV4I= gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= diff --git a/protected_environments.go b/protected_environments.go index 88269fe7a..a25b96d77 100644 --- a/protected_environments.go +++ b/protected_environments.go @@ -58,6 +58,7 @@ type EnvironmentAccessDescription struct { // GitLab API docs: // https://docs.gitlab.com/ee/api/protected_environments.html#protect-a-single-environment type EnvironmentApprovalRule struct { + ID int `json:"id"` UserID int `json:"user_id"` GroupID int `json:"group_id"` AccessLevel AccessLevelValue `json:"access_level"` diff --git a/protected_environments_test.go b/protected_environments_test.go index 1c579b6fe..43cc09335 100644 --- a/protected_environments_test.go +++ b/protected_environments_test.go @@ -57,7 +57,7 @@ func TestListProtectedEnvironments(t *testing.T) { "required_approvals": 2, "group_inheritance_type": 1 } - ] + ] },{ "name":"*-release", "deploy_access_levels": [ @@ -138,7 +138,7 @@ func TestGetProtectedEnvironment(t *testing.T) { "required_approvals": 0, "group_inheritance_type": 0 } - ] + ] }`) }) @@ -220,7 +220,7 @@ func TestProtectRepositoryEnvironments(t *testing.T) { "required_approvals": 0, "group_inheritance_type": 0 } - ] + ] }`) }) From d59139c6d119abd979f4751462546b8ec996f41b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Philippe=20Vl=C3=A9rick?= Date: Wed, 31 May 2023 20:19:29 +0200 Subject: [PATCH 3/4] fixup: adding ID to test suite --- protected_environments_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/protected_environments_test.go b/protected_environments_test.go index 43cc09335..21437f501 100644 --- a/protected_environments_test.go +++ b/protected_environments_test.go @@ -81,11 +81,13 @@ func TestListProtectedEnvironments(t *testing.T) { RequiredApprovalCount: 1, ApprovalRules: []*EnvironmentApprovalRule{ { + ID: 38, UserID: 42, AccessLevelDescription: "qa-group", RequiredApprovalCount: 1, }, { + ID: 39, GroupID: 135, AccessLevel: 30, AccessLevelDescription: "security-group", @@ -153,6 +155,7 @@ func TestGetProtectedEnvironment(t *testing.T) { RequiredApprovalCount: 1, ApprovalRules: []*EnvironmentApprovalRule{ { + ID: 1, GroupID: 10, AccessLevel: 5, AccessLevelDescription: "devops", @@ -235,6 +238,7 @@ func TestProtectRepositoryEnvironments(t *testing.T) { RequiredApprovalCount: 2, ApprovalRules: []*EnvironmentApprovalRule{ { + ID: 1, GroupID: 10, AccessLevel: 5, AccessLevelDescription: "devops", From cad317938ac022b82538d0561770ef22549c441f Mon Sep 17 00:00:00 2001 From: Sander van Harmelen Date: Wed, 31 May 2023 21:38:34 +0200 Subject: [PATCH 4/4] Final few tweaks --- protected_environments.go | 7 ++- protected_environments_test.go | 88 +++++++++++++++++----------------- 2 files changed, 48 insertions(+), 47 deletions(-) diff --git a/protected_environments.go b/protected_environments.go index a25b96d77..21820ab39 100644 --- a/protected_environments.go +++ b/protected_environments.go @@ -47,13 +47,15 @@ type ProtectedEnvironment struct { // GitLab API docs: // https://docs.gitlab.com/ee/api/protected_environments.html type EnvironmentAccessDescription struct { + ID int `json:"id"` AccessLevel AccessLevelValue `json:"access_level"` AccessLevelDescription string `json:"access_level_description"` UserID int `json:"user_id"` GroupID int `json:"group_id"` } -// EnvironmentApprovalRule represents the approval rules for a protected environment. +// EnvironmentApprovalRule represents the approval rules for a protected +// environment. // // GitLab API docs: // https://docs.gitlab.com/ee/api/protected_environments.html#protect-a-single-environment @@ -149,7 +151,8 @@ type EnvironmentAccessOptions struct { GroupID *int `url:"group_id,omitempty" json:"group_id,omitempty"` } -// EnvironmentApprovalRuleOptions represents the approval rules for a protected environment. +// EnvironmentApprovalRuleOptions represents the approval rules for a protected +// environment. // // GitLab API docs: // https://docs.gitlab.com/ee/api/protected_environments.html#protect-a-single-environment diff --git a/protected_environments_test.go b/protected_environments_test.go index 21437f501..4dd8a80db 100644 --- a/protected_environments_test.go +++ b/protected_environments_test.go @@ -38,26 +38,26 @@ func TestListProtectedEnvironments(t *testing.T) { } ], "required_approval_count": 1, - "approval_rules": [ - { - "id": 38, - "user_id": 42, - "group_id": null, - "access_level": null, - "access_level_description": "qa-group", - "required_approvals": 1, - "group_inheritance_type": 0 - }, - { - "id": 39, - "user_id": null, - "group_id": 135, - "access_level": 30, - "access_level_description": "security-group", - "required_approvals": 2, - "group_inheritance_type": 1 - } - ] + "approval_rules": [ + { + "id": 38, + "user_id": 42, + "group_id": null, + "access_level": null, + "access_level_description": "qa-group", + "required_approvals": 1, + "group_inheritance_type": 0 + }, + { + "id": 39, + "user_id": null, + "group_id": 135, + "access_level": 30, + "access_level_description": "security-group", + "required_approvals": 2, + "group_inheritance_type": 1 + } + ] },{ "name":"*-release", "deploy_access_levels": [ @@ -130,17 +130,17 @@ func TestGetProtectedEnvironment(t *testing.T) { } ], "required_approval_count": 1, - "approval_rules": [ - { - "id": 1, - "user_id": null, - "group_id": 10, - "access_level": 5, - "access_level_description": "devops", - "required_approvals": 0, - "group_inheritance_type": 0 - } - ] + "approval_rules": [ + { + "id": 1, + "user_id": null, + "group_id": 10, + "access_level": 5, + "access_level_description": "devops", + "required_approvals": 0, + "group_inheritance_type": 0 + } + ] }`) }) @@ -213,17 +213,17 @@ func TestProtectRepositoryEnvironments(t *testing.T) { } ], "required_approval_count": 2, - "approval_rules": [ - { - "id": 1, - "user_id": null, - "group_id": 10, - "access_level": 5, - "access_level_description": "devops", - "required_approvals": 0, - "group_inheritance_type": 0 - } - ] + "approval_rules": [ + { + "id": 1, + "user_id": null, + "group_id": 10, + "access_level": 5, + "access_level_description": "devops", + "required_approvals": 0, + "group_inheritance_type": 0 + } + ] }`) }) @@ -246,8 +246,6 @@ func TestProtectRepositoryEnvironments(t *testing.T) { }, } - noPermissions := NoPermissions - opt := &ProtectRepositoryEnvironmentsOptions{ Name: String("my-awesome-environment"), DeployAccessLevels: &[]*EnvironmentAccessOptions{ @@ -257,7 +255,7 @@ func TestProtectRepositoryEnvironments(t *testing.T) { ApprovalRules: &[]*EnvironmentApprovalRuleOptions{ { GroupID: Int(10), - AccessLevel: &noPermissions, + AccessLevel: AccessLevel(0), AccessLevelDescription: String("devops"), }, },