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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

228 lines
8.7 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
require 'mime/types'
module API
2021-01-03 14:25:43 +05:30
class Branches < ::API::Base
2017-08-17 22:00:37 +05:30
include PaginationParams
2019-02-15 15:39:39 +05:30
BRANCH_ENDPOINT_REQUIREMENTS = API::NAMESPACE_OR_PROJECT_REQUIREMENTS.merge(branch: API::NO_SLASH_URL_PART_REGEX)
2017-09-10 17:25:29 +05:30
2020-05-24 23:13:21 +05:30
after_validation { content_type "application/json" }
2021-01-29 00:20:46 +05:30
feature_category :source_code_management
2019-09-30 21:07:59 +05:30
before do
require_repository_enabled!
2023-01-13 00:05:48 +05:30
authorize! :read_code, user_project
2019-09-30 21:07:59 +05:30
end
2014-09-02 18:07:02 +05:30
2021-09-04 01:27:46 +05:30
rescue_from Gitlab::Git::Repository::NoRepository do
not_found!
end
2018-03-17 18:26:18 +05:30
helpers do
2018-03-27 19:54:05 +05:30
params :filter_params do
optional :search, type: String, desc: 'Return list of branches matching the search criteria'
2022-04-04 11:22:00 +05:30
optional :sort, type: String, desc: 'Return list of branches sorted by the given field', values: %w[name_asc updated_asc updated_desc]
2018-03-27 19:54:05 +05:30
end
2018-03-17 18:26:18 +05:30
end
2017-08-17 22:00:37 +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'
2017-08-17 22:00:37 +05:30
end
2019-02-15 15:39:39 +05:30
resource :projects, requirements: API::NAMESPACE_OR_PROJECT_REQUIREMENTS do
2017-08-17 22:00:37 +05:30
desc 'Get a project repository branches' do
2018-03-17 18:26:18 +05:30
success Entities::Branch
2023-01-13 00:05:48 +05:30
success code: 200, model: Entities::Branch
failure [{ code: 404, message: '404 Project Not Found' }]
tags %w[branches]
is_array true
2017-08-17 22:00:37 +05:30
end
params do
use :pagination
2018-03-27 19:54:05 +05:30
use :filter_params
2020-07-28 23:09:34 +05:30
2023-01-13 00:05:48 +05:30
optional :page_token, type: String, desc: 'Name of branch to start the pagination from'
2017-08-17 22:00:37 +05:30
end
2021-12-11 22:18:48 +05:30
get ':id/repository/branches', urgency: :low do
2022-04-04 11:22:00 +05:30
cache_action([user_project, :branches, current_user, declared_params], expires_in: 30.seconds) do
2021-06-08 01:23:25 +05:30
user_project.preload_protected_branches
2018-03-27 19:54:05 +05:30
2021-06-08 01:23:25 +05:30
repository = user_project.repository
2020-07-28 23:09:34 +05:30
2021-06-08 01:23:25 +05:30
branches_finder = BranchesFinder.new(repository, declared_params(include_missing: false))
branches = Gitlab::Pagination::GitalyKeysetPager.new(self, user_project).paginate(branches_finder)
2014-09-02 18:07:02 +05:30
2021-06-08 01:23:25 +05:30
merged_branch_names = repository.merged_branch_names(branches.map(&:name))
2022-10-11 01:57:18 +05:30
present_cached(
branches,
with: Entities::Branch,
current_user: current_user,
project: user_project,
merged_branch_names: merged_branch_names,
2022-11-25 23:54:43 +05:30
expires_in: 60.minutes,
2022-10-11 01:57:18 +05:30
cache_context: -> (branch) { [current_user&.cache_key, merged_branch_names.include?(branch.name)] }
)
2021-06-08 01:23:25 +05:30
end
2017-08-17 22:00:37 +05:30
end
2016-08-24 12:49:21 +05:30
2018-03-17 18:26:18 +05:30
resource ':id/repository/branches/:branch', requirements: BRANCH_ENDPOINT_REQUIREMENTS do
params do
requires :branch, type: String, desc: 'The name of the branch'
end
2023-01-13 00:05:48 +05:30
desc 'Check if a branch exists' do
success [{ code: 204, message: 'No Content' }]
failure [{ code: 404, message: 'Not Found' }]
tags %w[branches]
end
2018-03-17 18:26:18 +05:30
head do
2020-03-13 15:44:24 +05:30
user_project.repository.branch_exists?(params[:branch]) ? no_content! : not_found!
2018-03-17 18:26:18 +05:30
end
2023-01-13 00:05:48 +05:30
desc 'Get a single repository branch' do
success Entities::Branch
success code: 200, model: Entities::Branch
failure [{ code: 404, message: 'Branch Not Found' }, { code: 404, message: 'Project Not Found' }]
tags %w[branches]
end
2021-12-11 22:18:48 +05:30
get '/', urgency: :low do
2018-03-17 18:26:18 +05:30
branch = find_branch!(params[:branch])
2018-11-08 19:23:39 +05:30
present branch, with: Entities::Branch, current_user: current_user, project: user_project
2018-03-17 18:26:18 +05:30
end
2014-09-02 18:07:02 +05:30
end
2017-09-10 17:25:29 +05:30
# Note: This API will be deprecated in favor of the protected branches API.
2016-09-13 17:45:13 +05:30
# 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`.
2017-08-17 22:00:37 +05:30
desc 'Protect a single branch' do
2018-03-17 18:26:18 +05:30
success Entities::Branch
2023-01-13 00:05:48 +05:30
success code: 200, model: Entities::Branch
failure [{ code: 404, message: '404 Branch Not Found' }]
tags %w[branches]
2017-08-17 22:00:37 +05:30
end
params do
2018-12-05 23:21:45 +05:30
requires :branch, type: String, desc: 'The name of the branch', allow_blank: false
2017-08-17 22:00:37 +05:30
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-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
put ':id/repository/branches/:branch/protect', requirements: BRANCH_ENDPOINT_REQUIREMENTS do
2014-09-02 18:07:02 +05:30
authorize_admin_project
2018-03-17 18:26:18 +05:30
branch = find_branch!(params[:branch])
2017-08-17 22:00:37 +05:30
protected_branch = user_project.protected_branches.find_by(name: branch.name)
2016-09-13 17:45:13 +05:30
protected_branch_params = {
2017-08-17 22:00:37 +05:30
name: branch.name,
developers_can_push: params[:developers_can_push],
developers_can_merge: params[:developers_can_merge]
2016-09-13 17:45:13 +05:30
}
2016-11-03 12:29:30 +05:30
service_args = [user_project, current_user, protected_branch_params]
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
protected_branch = if protected_branch
2018-03-17 18:26:18 +05:30
::ProtectedBranches::LegacyApiUpdateService.new(*service_args).execute(protected_branch)
2016-11-03 12:29:30 +05:30
else
2018-03-17 18:26:18 +05:30
::ProtectedBranches::LegacyApiCreateService.new(*service_args).execute
2016-11-03 12:29:30 +05:30
end
2016-09-13 17:45:13 +05:30
2016-11-03 12:29:30 +05:30
if protected_branch.valid?
2018-11-08 19:23:39 +05:30
present branch, with: Entities::Branch, current_user: current_user, project: user_project
2016-08-24 12:49:21 +05:30
else
2016-11-03 12:29:30 +05:30
render_api_error!(protected_branch.errors.full_messages, 422)
2016-08-24 12:49:21 +05:30
end
2014-09-02 18:07:02 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2014-09-02 18:07:02 +05:30
2017-09-10 17:25:29 +05:30
# Note: This API will be deprecated in favor of the protected branches API.
2017-08-17 22:00:37 +05:30
desc 'Unprotect a single branch' do
2018-03-17 18:26:18 +05:30
success Entities::Branch
2023-01-13 00:05:48 +05:30
success code: 200, model: Entities::Branch
failure [{ code: 404, message: '404 Project Not Found' }, { code: 404, message: '404 Branch Not Found' }]
tags %w[branches]
2017-08-17 22:00:37 +05:30
end
params do
2018-12-05 23:21:45 +05:30
requires :branch, type: String, desc: 'The name of the branch', allow_blank: false
2017-08-17 22:00:37 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: disable CodeReuse/ActiveRecord
2017-09-10 17:25:29 +05:30
put ':id/repository/branches/:branch/unprotect', requirements: BRANCH_ENDPOINT_REQUIREMENTS do
2014-09-02 18:07:02 +05:30
authorize_admin_project
2018-03-17 18:26:18 +05:30
branch = find_branch!(params[:branch])
2017-08-17 22:00:37 +05:30
protected_branch = user_project.protected_branches.find_by(name: branch.name)
2022-10-11 01:57:18 +05:30
::ProtectedBranches::DestroyService.new(user_project, current_user).execute(protected_branch) if protected_branch
2014-09-02 18:07:02 +05:30
2018-11-08 19:23:39 +05:30
present branch, with: Entities::Branch, current_user: current_user, project: user_project
2014-09-02 18:07:02 +05:30
end
2018-12-05 23:21:45 +05:30
# rubocop: enable CodeReuse/ActiveRecord
2014-09-02 18:07:02 +05:30
2017-08-17 22:00:37 +05:30
desc 'Create branch' do
2018-03-17 18:26:18 +05:30
success Entities::Branch
2023-01-13 00:05:48 +05:30
success code: 201, model: Entities::Branch
failure [{ code: 400, message: 'Failed to create branch' }, { code: 400, message: 'Branch already exists' }]
tags %w[branches]
2017-08-17 22:00:37 +05:30
end
params do
2018-12-05 23:21:45 +05:30
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
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
post ':id/repository/branches' do
2014-09-02 18:07:02 +05:30
authorize_push_project
2017-08-17 22:00:37 +05:30
2020-01-01 13:55:28 +05:30
result = ::Branches::CreateService.new(user_project, current_user)
2017-09-10 17:25:29 +05:30
.execute(params[:branch], params[:ref])
2014-09-02 18:07:02 +05:30
2015-04-26 12:48:37 +05:30
if result[:status] == :success
present result[:branch],
2018-03-17 18:26:18 +05:30
with: Entities::Branch,
2018-11-08 19:23:39 +05:30
current_user: current_user,
2015-04-26 12:48:37 +05:30
project: user_project
else
render_api_error!(result[:message], 400)
end
2014-09-02 18:07:02 +05:30
end
2023-01-13 00:05:48 +05:30
desc 'Delete a branch' do
success code: 204
failure [{ code: 404, message: 'Branch Not Found' }]
tags %w[branches]
end
2017-08-17 22:00:37 +05:30
params do
2018-12-05 23:21:45 +05:30
requires :branch, type: String, desc: 'The name of the branch', allow_blank: false
2017-08-17 22:00:37 +05:30
end
2017-09-10 17:25:29 +05:30
delete ':id/repository/branches/:branch', requirements: BRANCH_ENDPOINT_REQUIREMENTS do
2014-09-02 18:07:02 +05:30
authorize_push_project
2017-08-17 22:00:37 +05:30
2018-03-17 18:26:18 +05:30
branch = find_branch!(params[:branch])
commit = user_project.repository.commit(branch.dereferenced_target)
destroy_conditionally!(commit, last_updated: commit.authored_date) do
2020-01-01 13:55:28 +05:30
result = ::Branches::DeleteService.new(user_project, current_user)
2018-03-17 18:26:18 +05:30
.execute(params[:branch])
2014-09-02 18:07:02 +05:30
2019-07-31 22:56:46 +05:30
if result.error?
render_api_error!(result.message, result.http_status)
2018-03-17 18:26:18 +05:30
end
2014-09-02 18:07:02 +05:30
end
end
2017-08-17 22:00:37 +05:30
2023-01-13 00:05:48 +05:30
desc 'Delete all merged branches' do
success code: 202, message: '202 Accepted'
failure [{ code: 404, message: '404 Project Not Found' }]
tags %w[branches]
end
2017-09-10 17:25:29 +05:30
delete ':id/repository/merged_branches' do
2020-01-01 13:55:28 +05:30
::Branches::DeleteMergedService.new(user_project, current_user).async_execute
2017-08-17 22:00:37 +05:30
accepted!
end
2014-09-02 18:07:02 +05:30
end
end
end