From 797e43dfe2b3dcbc066b3be9ddb4d28aae2bae3f Mon Sep 17 00:00:00 2001 From: Alishan Ladhani Date: Wed, 8 Dec 2021 16:49:04 -0500 Subject: [PATCH 1/2] Add required_approval_count to Protected Environment API Part of Deployment Approvals MVC --- doc/api/group_protected_environments.md | 26 +++++++++++-------- doc/api/protected_environments.md | 18 ++++++++----- ee/lib/api/protected_environments.rb | 2 ++ .../ee/api/entities/protected_environment.rb | 1 + .../public_api/v4/protected_environment.json | 3 ++- .../api/protected_environments_spec.rb | 12 ++++++--- 6 files changed, 39 insertions(+), 23 deletions(-) diff --git a/doc/api/group_protected_environments.md b/doc/api/group_protected_environments.md index 0e1cd149c519aa..15ca2b5727c9d6 100644 --- a/doc/api/group_protected_environments.md +++ b/doc/api/group_protected_environments.md @@ -48,12 +48,13 @@ Example response: "name":"production", "deploy_access_levels":[ { - "access_level":40, - "access_level_description":"Maintainers", - "user_id":null, - "group_id":null + "access_level": 40, + "access_level_description": "Maintainers", + "user_id": null, + "group_id": null } - ] + ], + "required_approval_count": 0 } ] ``` @@ -87,7 +88,8 @@ Example response: "user_id":null, "group_id":null } - ] + ], + "required_approval_count": 0 } ``` @@ -104,6 +106,7 @@ POST /groups/:id/protected_environments | `id` | integer/string | yes | The ID or [URL-encoded path of the group](index.md#namespaced-path-encoding) maintained by the authenticated user. | | `name` | string | yes | The deployment tier of the protected environment. One of `production`, `staging`, `testing`, `development`, or `other`. Read more about [deployment tiers](../ci/environments/index.md#deployment-tier-of-environments).| | `deploy_access_levels` | array | yes | Array of access levels allowed to deploy, with each described by a hash. One of `user_id`, `group_id` or `access_level`. They take the form of `{user_id: integer}`, `{group_id: integer}` or `{access_level: integer}` respectively. | +| `required_approval_count` | integer | no | The number of approvals required to deploy to this environment. Note: The Deployment Approvals feature has not been released yet. See [issue](https://gitlab.com/gitlab-org/gitlab/-/issues/343864) for details. | The assignable `user_id` are the users who belong to the given group with the Maintainer role (or above). The assignable `group_id` are the sub-groups under the given group. @@ -119,12 +122,13 @@ Example response: "name":"production", "deploy_access_levels":[ { - "access_level":40, - "access_level_description":"protected-access-group", - "user_id":null, - "group_id":9899826 + "access_level": 40, + "access_level_description": "protected-access-group", + "user_id": null, + "group_id": 9899826 } - ] + ], + "required_approval_count": 0 } ``` diff --git a/doc/api/protected_environments.md b/doc/api/protected_environments.md index c7de4c504a441c..5a30fb7dc3b77d 100644 --- a/doc/api/protected_environments.md +++ b/doc/api/protected_environments.md @@ -49,7 +49,8 @@ Example response: "user_id":null, "group_id":null } - ] + ], + "required_approval_count": 0 } ] ``` @@ -78,12 +79,13 @@ Example response: "name":"production", "deploy_access_levels":[ { - "access_level":40, - "access_level_description":"Maintainers", - "user_id":null, - "group_id":null + "access_level": 40, + "access_level_description": "Maintainers", + "user_id": null, + "group_id": null } - ] + ], + "required_approval_count": 0 } ``` @@ -107,6 +109,7 @@ curl --header 'Content-Type: application/json' --request POST \ | `id` | integer/string | yes | The ID or [URL-encoded path of the project](index.md#namespaced-path-encoding) owned by the authenticated user. | | `name` | string | yes | The name of the environment. | | `deploy_access_levels` | array | yes | Array of access levels allowed to deploy, with each described by a hash. | +| `required_approval_count` | integer | no | The number of approvals required to deploy to this environment. Note: The Deployment Approvals feature has not been released yet. See [issue](https://gitlab.com/gitlab-org/gitlab/-/issues/343864) for details. | Elements in the `deploy_access_levels` array should be one of `user_id`, `group_id` or `access_level`, and take the form `{user_id: integer}`, `{group_id: integer}` or @@ -125,7 +128,8 @@ Example response: "user_id": null, "group_id": 9899826 } - ] + ], + "required_approval_count": 0 } ``` diff --git a/ee/lib/api/protected_environments.rb b/ee/lib/api/protected_environments.rb index 020914661d8c7a..20370c48bcb76d 100644 --- a/ee/lib/api/protected_environments.rb +++ b/ee/lib/api/protected_environments.rb @@ -51,6 +51,7 @@ def protected_environment end params do requires :name, type: String, desc: 'The name of the protected environment' + optional :required_approval_count, type: Integer, desc: 'The number of approvals required to deploy to this environment', default: 0 requires :deploy_access_levels, type: Array, desc: 'An array of users/groups allowed to deploy environment' do optional :access_level, type: Integer, values: ::ProtectedEnvironment::DeployAccessLevel::ALLOWED_ACCESS_LEVELS @@ -137,6 +138,7 @@ def protected_environment end params do requires :name, type: String, desc: 'The tier name of the protected environment' + optional :required_approval_count, type: Integer, desc: 'The number of approvals required to deploy to this environment', default: 0 requires :deploy_access_levels, as: :deploy_access_levels_attributes, type: Array, desc: 'An array of users/groups allowed to deploy environment' do optional :access_level, type: Integer, values: ::ProtectedEnvironment::DeployAccessLevel::ALLOWED_ACCESS_LEVELS diff --git a/ee/lib/ee/api/entities/protected_environment.rb b/ee/lib/ee/api/entities/protected_environment.rb index 2baf4072c2cda6..f47ac263e4976e 100644 --- a/ee/lib/ee/api/entities/protected_environment.rb +++ b/ee/lib/ee/api/entities/protected_environment.rb @@ -6,6 +6,7 @@ module Entities class ProtectedEnvironment < Grape::Entity expose :name expose :deploy_access_levels, using: ::API::Entities::ProtectedRefAccess + expose :required_approval_count end end end diff --git a/ee/spec/fixtures/api/schemas/public_api/v4/protected_environment.json b/ee/spec/fixtures/api/schemas/public_api/v4/protected_environment.json index a71a20fba50764..ad92f3dc02c143 100644 --- a/ee/spec/fixtures/api/schemas/public_api/v4/protected_environment.json +++ b/ee/spec/fixtures/api/schemas/public_api/v4/protected_environment.json @@ -6,7 +6,8 @@ ], "properties": { "name": { "type": "string" }, - "deploy_access_levels": { "type": "array", "items": { "$ref": "protected_ref_access.json" } } + "deploy_access_levels": { "type": "array", "items": { "$ref": "protected_ref_access.json" } }, + "required_approval_count": { "type": "integer" } }, "additionalProperties": false } diff --git a/ee/spec/requests/api/protected_environments_spec.rb b/ee/spec/requests/api/protected_environments_spec.rb index 4f3d38dc224323..02fed4eafce1c7 100644 --- a/ee/spec/requests/api/protected_environments_spec.rb +++ b/ee/spec/requests/api/protected_environments_spec.rb @@ -12,8 +12,8 @@ let(:protected_environment_name) { 'production' } before do - create(:protected_environment, :maintainers_can_deploy, :project_level, project: project, name: protected_environment_name) - create(:protected_environment, :maintainers_can_deploy, :group_level, group: group, name: protected_environment_name) + create(:protected_environment, :maintainers_can_deploy, :project_level, project: project, name: protected_environment_name, required_approval_count: 1) + create(:protected_environment, :maintainers_can_deploy, :group_level, group: group, name: protected_environment_name, required_approval_count: 2) end shared_examples 'requests for non-maintainers' do @@ -64,6 +64,7 @@ expect(response).to match_response_schema('public_api/v4/protected_environment', dir: 'ee') expect(json_response['name']).to eq(protected_environment_name) expect(json_response['deploy_access_levels'][0]['access_level']).to eq(::Gitlab::Access::MAINTAINER) + expect(json_response['required_approval_count']).to eq(1) end context 'when protected environment does not exist' do @@ -90,12 +91,13 @@ deployer = create(:user) project.add_developer(deployer) - post api_url, params: { name: 'staging', deploy_access_levels: [{ user_id: deployer.id }] } + post api_url, params: { name: 'staging', deploy_access_levels: [{ user_id: deployer.id }], required_approval_count: 3 } expect(response).to have_gitlab_http_status(:created) expect(response).to match_response_schema('public_api/v4/protected_environment', dir: 'ee') expect(json_response['name']).to eq('staging') expect(json_response['deploy_access_levels'].first['user_id']).to eq(deployer.id) + expect(json_response['required_approval_count']).to eq(3) end it 'protects the environment with group allowed to deploy' do @@ -207,6 +209,7 @@ expect(response).to match_response_schema('public_api/v4/protected_environment', dir: 'ee') expect(json_response['name']).to eq(protected_environment_name) expect(json_response['deploy_access_levels'][0]['access_level']).to eq(::Gitlab::Access::MAINTAINER) + expect(json_response['required_approval_count']).to eq(2) end context 'when protected environment does not exist' do @@ -233,12 +236,13 @@ deployer = create(:user) group.add_maintainer(deployer) - post api_url, params: { name: 'staging', deploy_access_levels: [{ user_id: deployer.id }] } + post api_url, params: { name: 'staging', deploy_access_levels: [{ user_id: deployer.id }], required_approval_count: 3 } expect(response).to have_gitlab_http_status(:created) expect(response).to match_response_schema('public_api/v4/protected_environment', dir: 'ee') expect(json_response['name']).to eq('staging') expect(json_response['deploy_access_levels'].first['user_id']).to eq(deployer.id) + expect(json_response['required_approval_count']).to eq(3) end it 'protects the environment with group allowed to deploy' do -- GitLab From a1c4f3e71ac74564ed5f6a1616650589145cd9e9 Mon Sep 17 00:00:00 2001 From: Craig Norris Date: Fri, 17 Dec 2021 16:50:23 +0000 Subject: [PATCH 2/2] Apply 2 suggestion(s) to 2 file(s) --- doc/api/group_protected_environments.md | 2 +- doc/api/protected_environments.md | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/api/group_protected_environments.md b/doc/api/group_protected_environments.md index 15ca2b5727c9d6..6ce4e1791b0a86 100644 --- a/doc/api/group_protected_environments.md +++ b/doc/api/group_protected_environments.md @@ -106,7 +106,7 @@ POST /groups/:id/protected_environments | `id` | integer/string | yes | The ID or [URL-encoded path of the group](index.md#namespaced-path-encoding) maintained by the authenticated user. | | `name` | string | yes | The deployment tier of the protected environment. One of `production`, `staging`, `testing`, `development`, or `other`. Read more about [deployment tiers](../ci/environments/index.md#deployment-tier-of-environments).| | `deploy_access_levels` | array | yes | Array of access levels allowed to deploy, with each described by a hash. One of `user_id`, `group_id` or `access_level`. They take the form of `{user_id: integer}`, `{group_id: integer}` or `{access_level: integer}` respectively. | -| `required_approval_count` | integer | no | The number of approvals required to deploy to this environment. Note: The Deployment Approvals feature has not been released yet. See [issue](https://gitlab.com/gitlab-org/gitlab/-/issues/343864) for details. | +| `required_approval_count` | integer | no | The number of approvals required to deploy to this environment. This is part of Deployment Approvals, which isn't yet available for use. For details, see [issue](https://gitlab.com/gitlab-org/gitlab/-/issues/343864). | The assignable `user_id` are the users who belong to the given group with the Maintainer role (or above). The assignable `group_id` are the sub-groups under the given group. diff --git a/doc/api/protected_environments.md b/doc/api/protected_environments.md index 5a30fb7dc3b77d..61587136a14e17 100644 --- a/doc/api/protected_environments.md +++ b/doc/api/protected_environments.md @@ -109,7 +109,7 @@ curl --header 'Content-Type: application/json' --request POST \ | `id` | integer/string | yes | The ID or [URL-encoded path of the project](index.md#namespaced-path-encoding) owned by the authenticated user. | | `name` | string | yes | The name of the environment. | | `deploy_access_levels` | array | yes | Array of access levels allowed to deploy, with each described by a hash. | -| `required_approval_count` | integer | no | The number of approvals required to deploy to this environment. Note: The Deployment Approvals feature has not been released yet. See [issue](https://gitlab.com/gitlab-org/gitlab/-/issues/343864) for details. | +| `required_approval_count` | integer | no | The number of approvals required to deploy to this environment. This is part of Deployment Approvals, which isn't yet available for use. For details, see [issue](https://gitlab.com/gitlab-org/gitlab/-/issues/343864). | Elements in the `deploy_access_levels` array should be one of `user_id`, `group_id` or `access_level`, and take the form `{user_id: integer}`, `{group_id: integer}` or -- GitLab