2018-09-29 18:34:47 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2014-03-31 09:31:53 -04:00
|
|
|
require 'mime/types'
|
|
|
|
|
|
|
|
module API
|
|
|
|
class Branches < Grape::API
|
2017-01-16 23:45:07 -05:00
|
|
|
include PaginationParams
|
|
|
|
|
2018-11-08 07:18:17 -05:00
|
|
|
BRANCH_ENDPOINT_REQUIREMENTS = API::NAMESPACE_OR_PROJECT_REQUIREMENTS.merge(branch: API::NO_SLASH_URL_PART_REGEX)
|
2017-07-27 07:01:14 -04:00
|
|
|
|
2019-06-24 18:12:42 -04:00
|
|
|
before do
|
|
|
|
require_repository_enabled!
|
|
|
|
authorize! :download_code, user_project
|
|
|
|
end
|
2014-03-31 09:31:53 -04:00
|
|
|
|
2017-10-14 05:32:24 -04:00
|
|
|
helpers do
|
2018-03-05 07:57:47 -05:00
|
|
|
params :filter_params do
|
|
|
|
optional :search, type: String, desc: 'Return list of branches matching the search criteria'
|
2018-08-07 11:15:56 -04:00
|
|
|
optional :sort, type: String, desc: 'Return list of branches sorted by the given field'
|
2018-03-05 07:57:47 -05:00
|
|
|
end
|
2017-10-14 05:32:24 -04:00
|
|
|
end
|
|
|
|
|
2016-09-13 14:28:29 -04:00
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
|
|
|
end
|
2018-11-08 07:18:17 -05:00
|
|
|
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
|
2016-09-13 14:28:29 -04:00
|
|
|
desc 'Get a project repository branches' do
|
2017-10-05 04:48:05 -04:00
|
|
|
success Entities::Branch
|
2016-09-13 14:28:29 -04:00
|
|
|
end
|
2017-01-16 23:45:07 -05:00
|
|
|
params do
|
|
|
|
use :pagination
|
2018-03-05 07:57:47 -05:00
|
|
|
use :filter_params
|
2017-01-16 23:45:07 -05:00
|
|
|
end
|
2017-07-26 08:33:09 -04:00
|
|
|
get ':id/repository/branches' do
|
2019-11-12 04:06:14 -05:00
|
|
|
user_project.preload_protected_branches
|
2018-01-15 10:21:04 -05:00
|
|
|
|
2017-11-06 07:29:17 -05:00
|
|
|
repository = user_project.repository
|
2018-03-05 07:57:47 -05:00
|
|
|
|
|
|
|
branches = BranchesFinder.new(repository, declared_params(include_missing: false)).execute
|
2019-03-29 18:16:31 -04:00
|
|
|
branches = paginate(::Kaminari.paginate_array(branches))
|
2017-11-06 07:29:17 -05:00
|
|
|
merged_branch_names = repository.merged_branch_names(branches.map(&:name))
|
2016-07-19 07:30:23 -04:00
|
|
|
|
2018-03-05 07:57:47 -05:00
|
|
|
present(
|
2019-03-29 18:16:31 -04:00
|
|
|
branches,
|
2018-03-05 07:57:47 -05:00
|
|
|
with: Entities::Branch,
|
2018-06-25 12:39:23 -04:00
|
|
|
current_user: current_user,
|
2018-03-05 07:57:47 -05:00
|
|
|
project: user_project,
|
|
|
|
merged_branch_names: merged_branch_names
|
|
|
|
)
|
2014-03-31 09:31:53 -04:00
|
|
|
end
|
|
|
|
|
2017-09-04 19:55:12 -04:00
|
|
|
resource ':id/repository/branches/:branch', requirements: BRANCH_ENDPOINT_REQUIREMENTS do
|
|
|
|
desc 'Get a single branch' do
|
2017-10-05 04:48:05 -04:00
|
|
|
success Entities::Branch
|
2017-09-04 19:55:12 -04:00
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :branch, type: String, desc: 'The name of the branch'
|
|
|
|
end
|
|
|
|
head do
|
2020-01-16 13:08:46 -05:00
|
|
|
user_project.repository.branch_exists?(params[:branch]) ? no_content! : not_found!
|
2017-09-04 19:55:12 -04:00
|
|
|
end
|
|
|
|
get do
|
2017-10-14 05:32:24 -04:00
|
|
|
branch = find_branch!(params[:branch])
|
2017-09-04 19:55:12 -04:00
|
|
|
|
2018-06-25 12:39:23 -04:00
|
|
|
present branch, with: Entities::Branch, current_user: current_user, project: user_project
|
2017-09-04 19:55:12 -04:00
|
|
|
end
|
2014-03-31 09:31:53 -04:00
|
|
|
end
|
|
|
|
|
2017-08-02 06:16:17 -04:00
|
|
|
# Note: This API will be deprecated in favor of the protected branches API.
|
2016-07-25 10:44:53 -04:00
|
|
|
# Note: The internal data model moved from `developers_can_{merge,push}` to `allowed_to_{merge,push}`
|
|
|
|
# in `gitlab-org/gitlab-ce!5081`. The API interface has not been changed (to maintain compatibility),
|
|
|
|
# but it works with the changed data model to infer `developers_can_merge` and `developers_can_push`.
|
2016-09-13 14:28:29 -04:00
|
|
|
desc 'Protect a single branch' do
|
2017-10-05 04:48:05 -04:00
|
|
|
success Entities::Branch
|
2016-09-13 14:28:29 -04:00
|
|
|
end
|
|
|
|
params do
|
2018-09-11 18:02:09 -04:00
|
|
|
requires :branch, type: String, desc: 'The name of the branch', allow_blank: false
|
2016-09-13 14:28:29 -04:00
|
|
|
optional :developers_can_push, type: Boolean, desc: 'Flag if developers can push to that branch'
|
|
|
|
optional :developers_can_merge, type: Boolean, desc: 'Flag if developers can merge to that branch'
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-07-27 07:01:14 -04:00
|
|
|
put ':id/repository/branches/:branch/protect', requirements: BRANCH_ENDPOINT_REQUIREMENTS do
|
2014-03-31 09:31:53 -04:00
|
|
|
authorize_admin_project
|
|
|
|
|
2017-10-14 05:32:24 -04:00
|
|
|
branch = find_branch!(params[:branch])
|
2016-09-13 14:28:29 -04:00
|
|
|
|
|
|
|
protected_branch = user_project.protected_branches.find_by(name: branch.name)
|
2016-07-29 02:13:07 -04:00
|
|
|
|
2016-09-22 11:08:05 -04:00
|
|
|
protected_branch_params = {
|
2016-09-13 14:28:29 -04:00
|
|
|
name: branch.name,
|
|
|
|
developers_can_push: params[:developers_can_push],
|
|
|
|
developers_can_merge: params[:developers_can_merge]
|
2016-07-25 10:44:53 -04:00
|
|
|
}
|
|
|
|
|
2016-09-30 03:44:46 -04:00
|
|
|
service_args = [user_project, current_user, protected_branch_params]
|
2016-07-29 02:13:07 -04:00
|
|
|
|
2016-09-06 01:05:34 -04:00
|
|
|
protected_branch = if protected_branch
|
2017-11-22 10:16:08 -05:00
|
|
|
::ProtectedBranches::LegacyApiUpdateService.new(*service_args).execute(protected_branch)
|
2016-09-06 01:05:34 -04:00
|
|
|
else
|
2017-11-22 10:16:08 -05:00
|
|
|
::ProtectedBranches::LegacyApiCreateService.new(*service_args).execute
|
2016-09-06 01:05:34 -04:00
|
|
|
end
|
2016-07-12 10:31:55 -04:00
|
|
|
|
2016-09-06 01:05:34 -04:00
|
|
|
if protected_branch.valid?
|
2018-06-25 12:39:23 -04:00
|
|
|
present branch, with: Entities::Branch, current_user: current_user, project: user_project
|
2016-07-29 02:13:07 -04:00
|
|
|
else
|
2016-09-06 01:05:34 -04:00
|
|
|
render_api_error!(protected_branch.errors.full_messages, 422)
|
2016-07-29 02:13:07 -04:00
|
|
|
end
|
2014-03-31 09:31:53 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2014-03-31 09:31:53 -04:00
|
|
|
|
2017-08-02 06:16:17 -04:00
|
|
|
# Note: This API will be deprecated in favor of the protected branches API.
|
2016-09-13 14:28:29 -04:00
|
|
|
desc 'Unprotect a single branch' do
|
2017-10-05 04:48:05 -04:00
|
|
|
success Entities::Branch
|
2016-09-13 14:28:29 -04:00
|
|
|
end
|
|
|
|
params do
|
2018-09-11 18:02:09 -04:00
|
|
|
requires :branch, type: String, desc: 'The name of the branch', allow_blank: false
|
2016-09-13 14:28:29 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2017-07-27 07:01:14 -04:00
|
|
|
put ':id/repository/branches/:branch/unprotect', requirements: BRANCH_ENDPOINT_REQUIREMENTS do
|
2014-03-31 09:31:53 -04:00
|
|
|
authorize_admin_project
|
|
|
|
|
2017-10-14 05:32:24 -04:00
|
|
|
branch = find_branch!(params[:branch])
|
2016-09-13 14:28:29 -04:00
|
|
|
protected_branch = user_project.protected_branches.find_by(name: branch.name)
|
2017-02-07 09:16:46 -05:00
|
|
|
protected_branch&.destroy
|
2014-03-31 09:31:53 -04:00
|
|
|
|
2018-06-25 12:39:23 -04:00
|
|
|
present branch, with: Entities::Branch, current_user: current_user, project: user_project
|
2014-03-31 09:31:53 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2014-04-01 03:39:53 -04:00
|
|
|
|
2016-09-13 14:28:29 -04:00
|
|
|
desc 'Create branch' do
|
2017-10-05 04:48:05 -04:00
|
|
|
success Entities::Branch
|
2016-09-13 14:28:29 -04:00
|
|
|
end
|
|
|
|
params do
|
2018-09-11 18:02:09 -04:00
|
|
|
requires :branch, type: String, desc: 'The name of the branch', allow_blank: false
|
|
|
|
requires :ref, type: String, desc: 'Create branch from commit sha or existing branch', allow_blank: false
|
2016-09-13 14:28:29 -04:00
|
|
|
end
|
2017-07-26 08:33:09 -04:00
|
|
|
post ':id/repository/branches' do
|
2014-04-01 03:39:53 -04:00
|
|
|
authorize_push_project
|
2017-03-20 04:37:14 -04:00
|
|
|
|
2019-12-03 13:06:49 -05:00
|
|
|
result = ::Branches::CreateService.new(user_project, current_user)
|
2017-06-21 09:48:12 -04:00
|
|
|
.execute(params[:branch], params[:ref])
|
2014-10-30 11:28:59 -04:00
|
|
|
|
2014-07-27 10:40:00 -04:00
|
|
|
if result[:status] == :success
|
|
|
|
present result[:branch],
|
2017-10-05 04:48:05 -04:00
|
|
|
with: Entities::Branch,
|
2018-06-25 12:39:23 -04:00
|
|
|
current_user: current_user,
|
2014-07-27 10:40:00 -04:00
|
|
|
project: user_project
|
|
|
|
else
|
|
|
|
render_api_error!(result[:message], 400)
|
|
|
|
end
|
2014-04-01 03:39:53 -04:00
|
|
|
end
|
2014-05-22 07:39:50 -04:00
|
|
|
|
2016-09-13 14:28:29 -04:00
|
|
|
desc 'Delete a branch'
|
|
|
|
params do
|
2018-09-11 18:02:09 -04:00
|
|
|
requires :branch, type: String, desc: 'The name of the branch', allow_blank: false
|
2016-09-13 14:28:29 -04:00
|
|
|
end
|
2017-07-27 07:01:14 -04:00
|
|
|
delete ':id/repository/branches/:branch', requirements: BRANCH_ENDPOINT_REQUIREMENTS do
|
2014-05-22 07:39:50 -04:00
|
|
|
authorize_push_project
|
2016-09-13 14:28:29 -04:00
|
|
|
|
2017-10-14 05:32:24 -04:00
|
|
|
branch = find_branch!(params[:branch])
|
2017-03-02 08:21:36 -05:00
|
|
|
|
|
|
|
commit = user_project.repository.commit(branch.dereferenced_target)
|
|
|
|
|
2017-08-30 08:33:39 -04:00
|
|
|
destroy_conditionally!(commit, last_updated: commit.authored_date) do
|
2019-12-03 13:06:49 -05:00
|
|
|
result = ::Branches::DeleteService.new(user_project, current_user)
|
2017-03-02 08:21:36 -05:00
|
|
|
.execute(params[:branch])
|
2014-05-23 08:34:02 -04:00
|
|
|
|
2019-04-19 11:39:12 -04:00
|
|
|
if result.error?
|
|
|
|
render_api_error!(result.message, result.http_status)
|
2017-03-02 08:21:36 -05:00
|
|
|
end
|
2014-05-23 08:34:02 -04:00
|
|
|
end
|
2014-05-22 07:39:50 -04:00
|
|
|
end
|
2016-09-21 10:15:12 -04:00
|
|
|
|
2017-01-24 14:24:59 -05:00
|
|
|
desc 'Delete all merged branches'
|
2017-07-26 08:33:09 -04:00
|
|
|
delete ':id/repository/merged_branches' do
|
2019-12-03 13:06:49 -05:00
|
|
|
::Branches::DeleteMergedService.new(user_project, current_user).async_execute
|
2016-09-21 10:15:12 -04:00
|
|
|
|
2017-02-22 12:37:13 -05:00
|
|
|
accepted!
|
2016-09-21 10:15:12 -04:00
|
|
|
end
|
2014-03-31 09:31:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|