2018-09-29 22:34:47 +00:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-02 10:16:17 +00:00
|
|
|
module API
|
2020-04-28 03:09:53 +00:00
|
|
|
class ProtectedBranches < Grape::API
|
2017-08-02 10:16:17 +00:00
|
|
|
include PaginationParams
|
|
|
|
|
2018-11-08 12:18:17 +00:00
|
|
|
BRANCH_ENDPOINT_REQUIREMENTS = API::NAMESPACE_OR_PROJECT_REQUIREMENTS.merge(name: API::NO_SLASH_URL_PART_REGEX)
|
2017-08-02 10:16:17 +00:00
|
|
|
|
|
|
|
before { authorize_admin_project }
|
|
|
|
|
2019-05-29 12:49:56 +00:00
|
|
|
helpers Helpers::ProtectedBranchesHelpers
|
|
|
|
|
2017-08-02 10:16:17 +00:00
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
|
|
|
end
|
2018-11-08 12:18:17 +00:00
|
|
|
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
2017-08-02 10:16:17 +00:00
|
|
|
desc "Get a project's protected branches" do
|
|
|
|
success Entities::ProtectedBranch
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
use :pagination
|
2020-02-06 21:08:48 +00:00
|
|
|
optional :search, type: String, desc: 'Search for a protected branch by name'
|
2017-08-02 10:16:17 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-08-02 10:16:17 +00:00
|
|
|
get ':id/protected_branches' do
|
2020-02-06 21:08:48 +00:00
|
|
|
protected_branches =
|
|
|
|
ProtectedBranchesFinder
|
|
|
|
.new(user_project, params)
|
|
|
|
.execute
|
|
|
|
.preload(:push_access_levels, :merge_access_levels)
|
2017-08-02 10:16:17 +00:00
|
|
|
|
|
|
|
present paginate(protected_branches), with: Entities::ProtectedBranch, project: user_project
|
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-08-02 10:16:17 +00:00
|
|
|
|
|
|
|
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-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-08-02 10:16:17 +00:00
|
|
|
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-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-08-02 10:16:17 +00:00
|
|
|
|
2019-10-09 00:06:06 +00:00
|
|
|
desc 'Protect a single branch' do
|
2017-08-02 10:16:17 +00:00
|
|
|
success Entities::ProtectedBranch
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :name, type: String, desc: 'The name of the protected branch'
|
2017-11-22 17:08:47 +00:00
|
|
|
optional :push_access_level, type: Integer,
|
2018-08-28 16:30:38 +00:00
|
|
|
values: ProtectedBranch::PushAccessLevel.allowed_access_levels,
|
2018-05-24 08:59:24 +00:00
|
|
|
desc: 'Access levels allowed to push (defaults: `40`, maintainer access level)'
|
2017-11-22 17:08:47 +00:00
|
|
|
optional :merge_access_level, type: Integer,
|
2018-08-28 16:30:38 +00:00
|
|
|
values: ProtectedBranch::MergeAccessLevel.allowed_access_levels,
|
2018-05-24 08:59:24 +00:00
|
|
|
desc: 'Access levels allowed to merge (defaults: `40`, maintainer access level)'
|
2019-03-22 15:35:54 +00:00
|
|
|
|
2019-05-29 12:49:56 +00:00
|
|
|
use :optional_params_ee
|
2017-08-02 10:16:17 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-08-02 10:16:17 +00:00
|
|
|
post ':id/protected_branches' do
|
|
|
|
protected_branch = user_project.protected_branches.find_by(name: params[:name])
|
2019-09-13 13:26:31 +00:00
|
|
|
|
2017-08-02 10:16:17 +00:00
|
|
|
if protected_branch
|
|
|
|
conflict!("Protected branch '#{params[:name]}' already exists")
|
|
|
|
end
|
|
|
|
|
2018-03-26 13:32:02 +00:00
|
|
|
declared_params = declared_params(include_missing: false)
|
|
|
|
api_service = ::ProtectedBranches::ApiService.new(user_project, current_user, declared_params)
|
2017-11-22 17:08:47 +00:00
|
|
|
protected_branch = api_service.create
|
2017-08-15 17:44:37 +00:00
|
|
|
|
2017-08-02 10:16:17 +00:00
|
|
|
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-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-08-02 10:16:17 +00:00
|
|
|
|
|
|
|
desc 'Unprotect a single branch'
|
|
|
|
params do
|
|
|
|
requires :name, type: String, desc: 'The name of the protected branch'
|
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-08-02 10:16:17 +00:00
|
|
|
delete ':id/protected_branches/:name', requirements: BRANCH_ENDPOINT_REQUIREMENTS do
|
|
|
|
protected_branch = user_project.protected_branches.find_by!(name: params[:name])
|
|
|
|
|
2018-03-25 01:29:13 +00:00
|
|
|
destroy_conditionally!(protected_branch) do
|
|
|
|
destroy_service = ::ProtectedBranches::DestroyService.new(user_project, current_user)
|
|
|
|
destroy_service.execute(protected_branch)
|
|
|
|
end
|
2017-08-02 10:16:17 +00:00
|
|
|
end
|
2018-08-27 15:31:01 +00:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-08-02 10:16:17 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-10-09 00:06:06 +00:00
|
|
|
|
|
|
|
API::ProtectedBranches.prepend_if_ee('EE::API::ProtectedBranches')
|