2017-08-02 06:16:17 -04:00
|
|
|
module API
|
|
|
|
class ProtectedBranches < Grape::API
|
|
|
|
include PaginationParams
|
|
|
|
|
2018-01-19 15:07:44 -05:00
|
|
|
BRANCH_ENDPOINT_REQUIREMENTS = API::PROJECT_ENDPOINT_REQUIREMENTS.merge(name: API::NO_SLASH_URL_PART_REGEX)
|
2017-08-02 06:16:17 -04:00
|
|
|
|
|
|
|
before { authorize_admin_project }
|
|
|
|
|
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
|
|
|
end
|
|
|
|
resource :projects, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS do
|
|
|
|
desc "Get a project's protected branches" do
|
|
|
|
success Entities::ProtectedBranch
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
use :pagination
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-08-02 06:16:17 -04:00
|
|
|
get ':id/protected_branches' do
|
|
|
|
protected_branches = user_project.protected_branches.preload(:push_access_levels, :merge_access_levels)
|
|
|
|
|
|
|
|
present paginate(protected_branches), with: Entities::ProtectedBranch, project: user_project
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-08-02 06:16:17 -04: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 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-08-02 06:16:17 -04: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 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-08-02 06:16:17 -04:00
|
|
|
|
|
|
|
desc 'Protect a single branch or wildcard' do
|
|
|
|
success Entities::ProtectedBranch
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :name, type: String, desc: 'The name of the protected branch'
|
2017-11-22 12:08:47 -05:00
|
|
|
optional :push_access_level, type: Integer,
|
2017-11-24 07:41:36 -05:00
|
|
|
values: ProtectedRefAccess::ALLOWED_ACCESS_LEVELS,
|
2018-05-24 04:59:24 -04:00
|
|
|
desc: 'Access levels allowed to push (defaults: `40`, maintainer access level)'
|
2017-11-22 12:08:47 -05:00
|
|
|
optional :merge_access_level, type: Integer,
|
2017-11-24 07:41:36 -05:00
|
|
|
values: ProtectedRefAccess::ALLOWED_ACCESS_LEVELS,
|
2018-05-24 04:59:24 -04:00
|
|
|
desc: 'Access levels allowed to merge (defaults: `40`, maintainer access level)'
|
2017-08-02 06:16:17 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-08-02 06:16:17 -04:00
|
|
|
post ':id/protected_branches' do
|
|
|
|
protected_branch = user_project.protected_branches.find_by(name: params[:name])
|
|
|
|
if protected_branch
|
|
|
|
conflict!("Protected branch '#{params[:name]}' already exists")
|
|
|
|
end
|
|
|
|
|
2018-03-26 09:32:02 -04:00
|
|
|
declared_params = declared_params(include_missing: false)
|
|
|
|
api_service = ::ProtectedBranches::ApiService.new(user_project, current_user, declared_params)
|
2017-11-22 12:08:47 -05:00
|
|
|
protected_branch = api_service.create
|
2017-08-15 13:44:37 -04:00
|
|
|
|
2017-08-02 06:16:17 -04: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 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-08-02 06:16:17 -04:00
|
|
|
|
|
|
|
desc 'Unprotect a single branch'
|
|
|
|
params do
|
|
|
|
requires :name, type: String, desc: 'The name of the protected branch'
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-08-02 06:16:17 -04:00
|
|
|
delete ':id/protected_branches/:name', requirements: BRANCH_ENDPOINT_REQUIREMENTS do
|
|
|
|
protected_branch = user_project.protected_branches.find_by!(name: params[:name])
|
|
|
|
|
2018-03-24 21:29:13 -04:00
|
|
|
destroy_conditionally!(protected_branch) do
|
|
|
|
destroy_service = ::ProtectedBranches::DestroyService.new(user_project, current_user)
|
|
|
|
destroy_service.execute(protected_branch)
|
|
|
|
end
|
2017-08-02 06:16:17 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2017-08-02 06:16:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|