2014-03-31 09:31:53 -04:00
|
|
|
require 'mime/types'
|
|
|
|
|
|
|
|
module API
|
|
|
|
# Projects API
|
|
|
|
class Branches < Grape::API
|
|
|
|
before { authenticate! }
|
|
|
|
before { authorize! :download_code, user_project }
|
|
|
|
|
2016-09-13 14:28:29 -04:00
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
|
|
|
end
|
2014-03-31 09:31:53 -04:00
|
|
|
resource :projects do
|
2016-09-13 14:28:29 -04:00
|
|
|
desc 'Get a project repository branches' do
|
|
|
|
success Entities::RepoBranch
|
|
|
|
end
|
2014-03-31 09:31:53 -04:00
|
|
|
get ":id/repository/branches" do
|
2014-12-30 07:36:13 -05:00
|
|
|
branches = user_project.repository.branches.sort_by(&:name)
|
2016-07-19 07:30:23 -04:00
|
|
|
|
|
|
|
present branches, with: Entities::RepoBranch, project: user_project
|
2014-03-31 09:31:53 -04:00
|
|
|
end
|
|
|
|
|
2016-09-13 14:28:29 -04:00
|
|
|
desc 'Get a single branch' do
|
|
|
|
success Entities::RepoBranch
|
|
|
|
end
|
|
|
|
params do
|
2016-12-06 15:56:59 -05:00
|
|
|
requires :branch, type: String, desc: 'The name of the branch'
|
2016-09-13 14:28:29 -04:00
|
|
|
end
|
2016-12-06 15:56:59 -05:00
|
|
|
get ':id/repository/branches/:branch', requirements: { branch: /.+/ } do
|
2016-09-13 14:28:29 -04:00
|
|
|
branch = user_project.repository.find_branch(params[:branch])
|
|
|
|
not_found!("Branch") unless branch
|
2016-07-19 07:30:23 -04:00
|
|
|
|
2016-09-13 14:28:29 -04:00
|
|
|
present branch, with: Entities::RepoBranch, project: user_project
|
2014-03-31 09:31:53 -04:00
|
|
|
end
|
|
|
|
|
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
|
|
|
|
success Entities::RepoBranch
|
|
|
|
end
|
|
|
|
params do
|
2016-12-06 15:56:59 -05:00
|
|
|
requires :branch, type: String, desc: 'The name of the branch'
|
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
|
2016-12-06 15:56:59 -05:00
|
|
|
put ':id/repository/branches/:branch/protect', requirements: { branch: /.+/ } do
|
2014-03-31 09:31:53 -04:00
|
|
|
authorize_admin_project
|
|
|
|
|
2016-09-13 14:28:29 -04:00
|
|
|
branch = user_project.repository.find_branch(params[:branch])
|
|
|
|
not_found!('Branch') unless branch
|
|
|
|
|
|
|
|
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
|
|
|
|
ProtectedBranches::ApiUpdateService.new(*service_args).execute(protected_branch)
|
|
|
|
else
|
|
|
|
ProtectedBranches::ApiCreateService.new(*service_args).execute
|
|
|
|
end
|
2016-07-12 10:31:55 -04:00
|
|
|
|
2016-09-06 01:05:34 -04:00
|
|
|
if protected_branch.valid?
|
2016-09-13 14:28:29 -04:00
|
|
|
present branch, with: Entities::RepoBranch, 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
|
|
|
|
|
2016-09-13 14:28:29 -04:00
|
|
|
desc 'Unprotect a single branch' do
|
|
|
|
success Entities::RepoBranch
|
|
|
|
end
|
|
|
|
params do
|
2016-12-06 15:56:59 -05:00
|
|
|
requires :branch, type: String, desc: 'The name of the branch'
|
2016-09-13 14:28:29 -04:00
|
|
|
end
|
2016-12-06 15:56:59 -05:00
|
|
|
put ':id/repository/branches/:branch/unprotect', requirements: { branch: /.+/ } do
|
2014-03-31 09:31:53 -04:00
|
|
|
authorize_admin_project
|
|
|
|
|
2016-09-13 14:28:29 -04:00
|
|
|
branch = user_project.repository.find_branch(params[:branch])
|
|
|
|
not_found!("Branch") unless branch
|
|
|
|
protected_branch = user_project.protected_branches.find_by(name: branch.name)
|
2014-03-31 09:31:53 -04:00
|
|
|
protected_branch.destroy if protected_branch
|
|
|
|
|
2016-09-13 14:28:29 -04:00
|
|
|
present branch, with: Entities::RepoBranch, project: user_project
|
2014-03-31 09:31:53 -04:00
|
|
|
end
|
2014-04-01 03:39:53 -04:00
|
|
|
|
2016-09-13 14:28:29 -04:00
|
|
|
desc 'Create branch' do
|
|
|
|
success Entities::RepoBranch
|
|
|
|
end
|
|
|
|
params do
|
|
|
|
requires :branch_name, type: String, desc: 'The name of the branch'
|
|
|
|
requires :ref, type: String, desc: 'Create branch from commit sha or existing branch'
|
|
|
|
end
|
2014-04-01 03:39:53 -04:00
|
|
|
post ":id/repository/branches" do
|
|
|
|
authorize_push_project
|
2014-09-21 04:29:52 -04:00
|
|
|
result = CreateBranchService.new(user_project, current_user).
|
2016-09-06 01:05:34 -04:00
|
|
|
execute(params[:branch_name], 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],
|
2016-07-19 07:30:23 -04:00
|
|
|
with: Entities::RepoBranch,
|
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
|
2016-12-06 15:56:59 -05:00
|
|
|
requires :branch, type: String, desc: 'The name of the branch'
|
2016-09-13 14:28:29 -04:00
|
|
|
end
|
2016-12-06 15:56:59 -05:00
|
|
|
delete ":id/repository/branches/:branch", requirements: { branch: /.+/ } do
|
2014-05-22 07:39:50 -04:00
|
|
|
authorize_push_project
|
2016-09-13 14:28:29 -04:00
|
|
|
|
2014-09-21 04:29:52 -04:00
|
|
|
result = DeleteBranchService.new(user_project, current_user).
|
2016-09-06 01:05:34 -04:00
|
|
|
execute(params[:branch])
|
2014-05-23 08:34:02 -04:00
|
|
|
|
2014-09-21 04:29:52 -04:00
|
|
|
if result[:status] == :success
|
2014-10-30 11:28:59 -04:00
|
|
|
{
|
|
|
|
branch_name: params[:branch]
|
|
|
|
}
|
2014-05-23 08:34:02 -04:00
|
|
|
else
|
2014-07-27 10:40:00 -04:00
|
|
|
render_api_error!(result[:message], result[:return_code])
|
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
|
|
|
|
|
|
|
# Delete all merged branches
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# Example Request:
|
|
|
|
# DELETE /projects/:id/repository/branches/delete_merged
|
|
|
|
delete ":id/repository/merged_branches" do
|
|
|
|
DeleteMergedBranchesService.new(user_project, current_user).async_execute
|
|
|
|
|
|
|
|
status(200)
|
|
|
|
end
|
2014-03-31 09:31:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|