2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
module API
|
2021-01-03 14:25:43 +05:30
|
|
|
class ProtectedBranches < ::API::Base
|
2017-09-10 17:25:29 +05:30
|
|
|
include PaginationParams
|
|
|
|
|
2019-02-15 15:39:39 +05:30
|
|
|
BRANCH_ENDPOINT_REQUIREMENTS = API::NAMESPACE_OR_PROJECT_REQUIREMENTS.merge(name: API::NO_SLASH_URL_PART_REGEX)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2021-01-29 00:20:46 +05:30
|
|
|
feature_category :source_code_management
|
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
helpers Helpers::ProtectedBranchesHelpers
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
params do
|
2023-01-13 00:05:48 +05:30
|
|
|
requires :id,
|
|
|
|
types: [String, Integer],
|
|
|
|
desc: 'The ID or URL-encoded path of the project',
|
|
|
|
documentation: { example: 'gitlab-org/gitlab' }
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2019-02-15 15:39:39 +05:30
|
|
|
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
2017-09-10 17:25:29 +05:30
|
|
|
desc "Get a project's protected branches" do
|
2023-01-13 00:05:48 +05:30
|
|
|
success code: 200, model: Entities::ProtectedBranch
|
|
|
|
is_array true
|
|
|
|
failure [
|
|
|
|
{ code: 404, message: '404 Project Not Found' },
|
|
|
|
{ code: 401, message: '401 Unauthorized' }
|
|
|
|
]
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
params do
|
|
|
|
use :pagination
|
2023-01-13 00:05:48 +05:30
|
|
|
optional :search, type: String, desc: 'Search for a protected branch by name', documentation: { example: 'mai' }
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-09-10 17:25:29 +05:30
|
|
|
get ':id/protected_branches' do
|
2023-06-20 00:43:36 +05:30
|
|
|
authorize_read_code!
|
|
|
|
|
2020-03-13 15:44:24 +05:30
|
|
|
protected_branches =
|
|
|
|
ProtectedBranchesFinder
|
|
|
|
.new(user_project, params)
|
|
|
|
.execute
|
|
|
|
.preload(:push_access_levels, :merge_access_levels)
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
present paginate(protected_branches), with: Entities::ProtectedBranch, project: user_project
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
desc 'Get a single protected branch' do
|
2023-01-13 00:05:48 +05:30
|
|
|
success code: 200, model: Entities::ProtectedBranch
|
|
|
|
failure [
|
|
|
|
{ code: 404, message: '404 Project Not Found' },
|
|
|
|
{ code: 401, message: '401 Unauthorized' }
|
|
|
|
]
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
params do
|
2023-01-13 00:05:48 +05:30
|
|
|
requires :name, type: String, desc: 'The name of the branch or wildcard', documentation: { example: 'main' }
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-09-10 17:25:29 +05:30
|
|
|
get ':id/protected_branches/:name', requirements: BRANCH_ENDPOINT_REQUIREMENTS do
|
2023-06-20 00:43:36 +05:30
|
|
|
authorize_read_code!
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
protected_branch = user_project.protected_branches.find_by!(name: params[:name])
|
|
|
|
|
|
|
|
present protected_branch, with: Entities::ProtectedBranch, project: user_project
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2019-12-21 20:55:43 +05:30
|
|
|
desc 'Protect a single branch' do
|
2023-01-13 00:05:48 +05:30
|
|
|
success code: 201, model: Entities::ProtectedBranch
|
|
|
|
failure [
|
|
|
|
{ code: 422, message: 'name is missing' },
|
|
|
|
{ code: 409, message: "Protected branch 'main' already exists" },
|
|
|
|
{ code: 404, message: '404 Project Not Found' },
|
|
|
|
{ code: 401, message: '401 Unauthorized' }
|
|
|
|
]
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
params do
|
2023-01-13 00:05:48 +05:30
|
|
|
requires :name, type: String, desc: 'The name of the protected branch', documentation: { example: 'main' }
|
2018-03-17 18:26:18 +05:30
|
|
|
optional :push_access_level, type: Integer,
|
2018-12-05 23:21:45 +05:30
|
|
|
values: ProtectedBranch::PushAccessLevel.allowed_access_levels,
|
2018-11-08 19:23:39 +05:30
|
|
|
desc: 'Access levels allowed to push (defaults: `40`, maintainer access level)'
|
2018-03-17 18:26:18 +05:30
|
|
|
optional :merge_access_level, type: Integer,
|
2018-12-05 23:21:45 +05:30
|
|
|
values: ProtectedBranch::MergeAccessLevel.allowed_access_levels,
|
2018-11-08 19:23:39 +05:30
|
|
|
desc: 'Access levels allowed to merge (defaults: `40`, maintainer access level)'
|
2021-04-17 20:07:23 +05:30
|
|
|
optional :allow_force_push, type: Boolean,
|
2022-08-27 11:52:29 +05:30
|
|
|
default: false,
|
|
|
|
desc: 'Allow force push for all users with push access.'
|
2019-07-07 11:18:12 +05:30
|
|
|
|
2019-09-04 21:01:54 +05:30
|
|
|
use :optional_params_ee
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-09-10 17:25:29 +05:30
|
|
|
post ':id/protected_branches' do
|
2023-06-20 00:43:36 +05:30
|
|
|
authorize_admin_project
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
protected_branch = user_project.protected_branches.find_by(name: params[:name])
|
2019-12-04 20:38:33 +05:30
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
if protected_branch
|
|
|
|
conflict!("Protected branch '#{params[:name]}' already exists")
|
|
|
|
end
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
declared_params = declared_params(include_missing: false)
|
2018-03-17 18:26:18 +05:30
|
|
|
api_service = ::ProtectedBranches::ApiService.new(user_project, current_user, declared_params)
|
|
|
|
protected_branch = api_service.create
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
if protected_branch.persisted?
|
|
|
|
present protected_branch, with: Entities::ProtectedBranch, project: user_project
|
|
|
|
else
|
|
|
|
render_api_error!(protected_branch.errors.full_messages, 422)
|
|
|
|
end
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-09-10 17:25:29 +05:30
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
desc 'Update a protected branch' do
|
|
|
|
success code: 200, model: Entities::ProtectedBranch
|
|
|
|
failure [
|
|
|
|
{ code: 422, message: 'Push access levels access level has already been taken' },
|
|
|
|
{ code: 404, message: '404 Project Not Found' },
|
2023-05-27 22:25:52 +05:30
|
|
|
{ code: 401, message: '401 Unauthorized' },
|
|
|
|
{ code: 400, message: '400 Bad request' }
|
2023-01-13 00:05:48 +05:30
|
|
|
]
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :name, type: String, desc: 'The name of the branch', documentation: { example: 'main' }
|
|
|
|
optional :allow_force_push, type: Boolean,
|
2023-05-27 22:25:52 +05:30
|
|
|
desc: 'Allow force push for all users with push access.',
|
|
|
|
allow_blank: false
|
2023-01-13 00:05:48 +05:30
|
|
|
|
|
|
|
use :optional_params_ee
|
|
|
|
end
|
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
patch ':id/protected_branches/:name', requirements: BRANCH_ENDPOINT_REQUIREMENTS do
|
2023-06-20 00:43:36 +05:30
|
|
|
authorize_admin_project
|
|
|
|
|
2023-01-13 00:05:48 +05:30
|
|
|
protected_branch = user_project.protected_branches.find_by!(name: params[:name])
|
|
|
|
|
|
|
|
declared_params = declared_params(include_missing: false)
|
|
|
|
api_service = ::ProtectedBranches::ApiService.new(user_project, current_user, declared_params)
|
|
|
|
protected_branch = api_service.update(protected_branch)
|
|
|
|
|
|
|
|
if protected_branch.valid?
|
|
|
|
present protected_branch, with: Entities::ProtectedBranch, project: user_project
|
|
|
|
else
|
|
|
|
render_api_error!(protected_branch.errors.full_messages, 422)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
desc 'Unprotect a single branch'
|
|
|
|
params do
|
2023-01-13 00:05:48 +05:30
|
|
|
requires :name, type: String, desc: 'The name of the protected branch', documentation: { example: 'main' }
|
|
|
|
end
|
|
|
|
desc 'Unprotect a single branch' do
|
|
|
|
success code: 204
|
|
|
|
failure [
|
|
|
|
{ code: 404, message: '404 Project Not Found' },
|
|
|
|
{ code: 401, message: '401 Unauthorized' }
|
|
|
|
]
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2021-12-11 22:18:48 +05:30
|
|
|
delete ':id/protected_branches/:name', requirements: BRANCH_ENDPOINT_REQUIREMENTS, urgency: :low do
|
2023-06-20 00:43:36 +05:30
|
|
|
authorize_admin_project
|
|
|
|
|
2017-09-10 17:25:29 +05:30
|
|
|
protected_branch = user_project.protected_branches.find_by!(name: params[:name])
|
|
|
|
|
2018-05-09 12:01:36 +05:30
|
|
|
destroy_conditionally!(protected_branch) do
|
|
|
|
destroy_service = ::ProtectedBranches::DestroyService.new(user_project, current_user)
|
|
|
|
destroy_service.execute(protected_branch)
|
|
|
|
end
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-09-10 17:25:29 +05:30
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-12-21 20:55:43 +05:30
|
|
|
|
2021-06-08 01:23:25 +05:30
|
|
|
API::ProtectedBranches.prepend_mod_with('API::ProtectedBranches')
|