debian-mirror-gitlab/lib/api/protected_branches.rb

108 lines
4.2 KiB
Ruby
Raw Normal View History

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
before { authorize_admin_project }
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
requires :id, type: String, desc: 'The ID of a project'
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
success Entities::ProtectedBranch
end
params do
use :pagination
2020-03-13 15:44:24 +05:30
optional :search, type: String, desc: 'Search for a protected branch by name'
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
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
success Entities::ProtectedBranch
end
params do
requires :name, type: String, desc: 'The name of the branch or wildcard'
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
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
2017-09-10 17:25:29 +05:30
success Entities::ProtectedBranch
end
params do
requires :name, type: String, desc: 'The name of the protected branch'
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,
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
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
desc 'Unprotect a single branch'
params do
requires :name, type: String, desc: 'The name of the protected branch'
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
delete ':id/protected_branches/:name', requirements: BRANCH_ENDPOINT_REQUIREMENTS do
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')