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 all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
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
20 changes: 12 additions & 8 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"`
DeployAccessLevels []*EnvironmentAccessDescription `json:"deploy_access_levels"`
RequiredApprovalCount int `json:"required_approval_count"`
}

// EnvironmentAccessDescription represents the access decription for a protected
Expand All @@ -58,7 +59,8 @@ type EnvironmentAccessDescription struct {
// https://docs.gitlab.com/ee/api/protected_environments.html#list-protected-environments
type ListProtectedEnvironmentsOptions ListOptions

// ListProtectedEnvironments returns a list of protected environments from a project.
// ListProtectedEnvironments returns a list of protected environments from a
// project.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/protected_environments.html#list-protected-environments
Expand All @@ -83,7 +85,8 @@ func (s *ProtectedEnvironmentsService) ListProtectedEnvironments(pid interface{}
return pes, resp, err
}

// GetProtectedEnvironment returns a single protected environment or wildcard protected environment.
// GetProtectedEnvironment returns a single protected environment or wildcard
// protected environment.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/protected_environments.html#get-a-single-protected-environment-or-wildcard-protected-environment
Expand Down Expand Up @@ -114,8 +117,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"`
DeployAccessLevels *[]*EnvironmentAccessOptions `url:"deploy_access_levels,omitempty" json:"deploy_access_levels,omitempty"`
thenom marked this conversation as resolved.
Show resolved Hide resolved
RequiredApprovalCount *int `url:"required_approval_count,omitempty" json:"required_approval_count,omitempty"`
}

// EnvironmentAccessOptions represents the options for an access decription for
Expand All @@ -129,8 +133,8 @@ type EnvironmentAccessOptions struct {
GroupID *int `url:"group_id,omitempty" json:"group_id,omitempty"`
}

// ProtectRepositoryEnvironments protects a single repository environment or several project
// repository environments using a wildcard protected environment.
// ProtectRepositoryEnvironments protects a single repository environment or
// several project repository environments using wildcard protected environment.
//
// GitLab API docs:
// https://docs.gitlab.com/ee/api/protected_environments.html#protect-repository-environments
Expand Down
117 changes: 113 additions & 4 deletions protected_environments_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,24 @@ 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"
}
]
}]`)
})

expected := []*ProtectedEnvironment{
Expand All @@ -42,6 +59,7 @@ func TestListProtectedEnvironments(t *testing.T) {
AccessLevelDescription: "Maintainers",
},
},
RequiredApprovalCount: 1,
},
{
Name: "*-release",
Expand All @@ -64,11 +82,21 @@ 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",
"deploy_access_levels": [
{
"access_level": 30,
"access_level_description": "Developers + Maintainers"
}
],
"required_approval_count": 1
}`)
})

expected := &ProtectedEnvironment{
Expand All @@ -79,10 +107,40 @@ func TestGetProtectedEnvironment(t *testing.T) {
AccessLevelDescription: "Developers + Maintainers",
},
},
RequiredApprovalCount: 1,
}

environment, _, err := client.ProtectedEnvironments.GetProtectedEnvironment(1, environmentName)
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)
}
Expand All @@ -91,9 +149,19 @@ 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",
"deploy_access_levels": [
{
"access_level": 30,
"access_level_description": "Developers + Maintainers"
}
],
"required_approval_count": 2
}`)
})

expected := &ProtectedEnvironment{
Expand All @@ -104,11 +172,52 @@ func TestProtectRepositoryEnvironments(t *testing.T) {
AccessLevelDescription: "Developers + Maintainers",
},
},
RequiredApprovalCount: 2,
}

opt := &ProtectRepositoryEnvironmentsOptions{
Name: String("my-awesome-environment"),
DeployAccessLevels: &[]*EnvironmentAccessOptions{
{AccessLevel: AccessLevel(30)},
},
RequiredApprovalCount: Int(2),
}

opt := &ProtectRepositoryEnvironmentsOptions{Name: String("my-awesome-environment"), 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)
}
Expand Down