Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

required_approval_count for protected_environments #1482

Merged
merged 5 commits into from
Jun 7, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,5 @@ _testmain.go
# IDE specific files and folders
.idea
*.iml
*.swp
*.swo
10 changes: 6 additions & 4 deletions protected_environments.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ type ProtectedEnvironmentsService struct {
// GitLab API docs:
// https://docs.gitlab.com/ee/api/protected_environments.html
type ProtectedEnvironment struct {
Name string `json:"name"`
DeployAccessLevels []*EnvironmentAccessDescription `json:"deploy_access_levels"`
Name string `json:"name"`
RequiredApprovalCount int `json:"required_approval_count"`
thenom marked this conversation as resolved.
Show resolved Hide resolved
DeployAccessLevels []*EnvironmentAccessDescription `json:"deploy_access_levels"`
}

// EnvironmentAccessDescription represents the access decription for a protected
Expand Down Expand Up @@ -114,8 +115,9 @@ func (s *ProtectedEnvironmentsService) GetProtectedEnvironment(pid interface{},
// GitLab API docs:
// https://docs.gitlab.com/ee/api/protected_environments.html#protect-repository-environments
type ProtectRepositoryEnvironmentsOptions struct {
Name *string `url:"name,omitempty" json:"name,omitempty"`
DeployAccessLevels *[]*EnvironmentAccessOptions `url:"deploy_access_levels,omitempty" json:"deploy_access_levels,omitempty"`
Name *string `url:"name,omitempty" json:"name,omitempty"`
RequiredApprovalCount *int `url:"required_approval_count,omitempty" json:"required_approval_count,omitempty"`
DeployAccessLevels *[]*EnvironmentAccessOptions `url:"deploy_access_levels,omitempty" json:"deploy_access_levels,omitempty"`
thenom marked this conversation as resolved.
Show resolved Hide resolved
}

// EnvironmentAccessOptions represents the options for an access decription for
Expand Down
64 changes: 57 additions & 7 deletions protected_environments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ func TestListProtectedEnvironments(t *testing.T) {

mux.HandleFunc("/api/v4/projects/1/protected_environments", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `[{"name":"1.0.0", "deploy_access_levels": [{"access_level": 40, "access_level_description": "Maintainers"}]},{"name":"*-release", "deploy_access_levels": [{"access_level": 30, "access_level_description": "Developers + Maintainers"}]}]`)
fmt.Fprint(w, `[{"name":"1.0.0", "deploy_access_levels": [{"access_level": 40, "access_level_description": "Maintainers"}], "required_approval_count": 1},{"name":"*-release", "deploy_access_levels": [{"access_level": 30, "access_level_description": "Developers + Maintainers"}]}]`)
thenom marked this conversation as resolved.
Show resolved Hide resolved
})

expected := []*ProtectedEnvironment{
{
Name: "1.0.0",
Name: "1.0.0",
RequiredApprovalCount: 1,
thenom marked this conversation as resolved.
Show resolved Hide resolved
DeployAccessLevels: []*EnvironmentAccessDescription{
{
AccessLevel: 40,
Expand Down Expand Up @@ -64,15 +65,17 @@ func TestGetProtectedEnvironment(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)

// Test with RequiredApprovalCount
environmentName := "my-awesome-environment"

mux.HandleFunc(fmt.Sprintf("/api/v4/projects/1/protected_environments/%s", environmentName), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"name":"my-awesome-environment", "deploy_access_levels": [{"access_level": 30, "access_level_description": "Developers + Maintainers"}]}`)
fmt.Fprint(w, `{"name":"my-awesome-environment", "required_approval_count": 1, "deploy_access_levels": [{"access_level": 30, "access_level_description": "Developers + Maintainers"}]}`)
})

expected := &ProtectedEnvironment{
Name: environmentName,
Name: environmentName,
RequiredApprovalCount: 1,
DeployAccessLevels: []*EnvironmentAccessDescription{
{
AccessLevel: 30,
Expand All @@ -85,19 +88,44 @@ func TestGetProtectedEnvironment(t *testing.T) {

assert.NoError(t, err, "failed to get response")
assert.Equal(t, expected, environment)

// Test without RequiredApprovalCount
environmentName = "my-awesome-environment2"

mux.HandleFunc(fmt.Sprintf("/api/v4/projects/2/protected_environments/%s", environmentName), func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodGet)
fmt.Fprint(w, `{"name":"my-awesome-environment2", "deploy_access_levels": [{"access_level": 30, "access_level_description": "Developers + Maintainers"}]}`)
})

expected = &ProtectedEnvironment{
Name: environmentName,
DeployAccessLevels: []*EnvironmentAccessDescription{
{
AccessLevel: 30,
AccessLevelDescription: "Developers + Maintainers",
},
},
}

environment, _, err = client.ProtectedEnvironments.GetProtectedEnvironment(2, environmentName)

assert.NoError(t, err, "failed to get response")
assert.Equal(t, expected, environment)
}

func TestProtectRepositoryEnvironments(t *testing.T) {
mux, server, client := setup(t)
defer teardown(server)

// Test with RequiredApprovalCount
mux.HandleFunc("/api/v4/projects/1/protected_environments", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
fmt.Fprint(w, `{"name":"my-awesome-environment", "deploy_access_levels": [{"access_level": 30, "access_level_description": "Developers + Maintainers"}]}`)
fmt.Fprint(w, `{"name":"my-awesome-environment", "required_approval_count": 2, "deploy_access_levels": [{"access_level": 30, "access_level_description": "Developers + Maintainers"}]}`)
})

expected := &ProtectedEnvironment{
Name: "my-awesome-environment",
Name: "my-awesome-environment",
RequiredApprovalCount: 2,
DeployAccessLevels: []*EnvironmentAccessDescription{
{
AccessLevel: 30,
Expand All @@ -106,11 +134,33 @@ func TestProtectRepositoryEnvironments(t *testing.T) {
},
}

opt := &ProtectRepositoryEnvironmentsOptions{Name: String("my-awesome-environment"), DeployAccessLevels: &[]*EnvironmentAccessOptions{{AccessLevel: AccessLevel(30)}}}
opt := &ProtectRepositoryEnvironmentsOptions{Name: String("my-awesome-environment"), RequiredApprovalCount: Int(2), DeployAccessLevels: &[]*EnvironmentAccessOptions{{AccessLevel: AccessLevel(30)}}}
environment, _, err := client.ProtectedEnvironments.ProtectRepositoryEnvironments(1, opt)

assert.NoError(t, err, "failed to get response")
assert.Equal(t, expected, environment)

// Test without RequiredApprovalCount
mux.HandleFunc("/api/v4/projects/2/protected_environments", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, http.MethodPost)
fmt.Fprint(w, `{"name":"my-awesome-environment2", "deploy_access_levels": [{"access_level": 30, "access_level_description": "Developers + Maintainers"}]}`)
})

expected = &ProtectedEnvironment{
Name: "my-awesome-environment2",
DeployAccessLevels: []*EnvironmentAccessDescription{
{
AccessLevel: 30,
AccessLevelDescription: "Developers + Maintainers",
},
},
}

opt = &ProtectRepositoryEnvironmentsOptions{Name: String("my-awesome-environment2"), DeployAccessLevels: &[]*EnvironmentAccessOptions{{AccessLevel: AccessLevel(30)}}}
environment, _, err = client.ProtectedEnvironments.ProtectRepositoryEnvironments(2, opt)

assert.NoError(t, err, "failed to get response")
assert.Equal(t, expected, environment)
}

func TestUnprotectRepositoryEnvironments(t *testing.T) {
Expand Down