2017-02-06 13:38:17 -05:00
|
|
|
require 'mime/types'
|
|
|
|
|
|
|
|
module API
|
|
|
|
module V3
|
|
|
|
class Branches < Grape::API
|
|
|
|
before { authenticate! }
|
|
|
|
before { authorize! :download_code, user_project }
|
|
|
|
|
|
|
|
params do
|
|
|
|
requires :id, type: String, desc: 'The ID of a project'
|
|
|
|
end
|
2017-03-15 14:09:24 -04:00
|
|
|
resource :projects, requirements: { id: %r{[^/]+} } do
|
2017-02-06 13:38:17 -05:00
|
|
|
desc 'Get a project repository branches' do
|
2017-10-05 04:48:05 -04:00
|
|
|
success ::API::Entities::Branch
|
2017-02-06 13:38:17 -05:00
|
|
|
end
|
|
|
|
get ":id/repository/branches" do
|
|
|
|
branches = user_project.repository.branches.sort_by(&:name)
|
|
|
|
|
2017-10-05 04:48:05 -04:00
|
|
|
present branches, with: ::API::Entities::Branch, project: user_project
|
2017-02-06 13:38:17 -05:00
|
|
|
end
|
2017-02-22 12:37:13 -05:00
|
|
|
|
2017-02-20 14:32:44 -05:00
|
|
|
desc 'Delete a branch'
|
|
|
|
params do
|
|
|
|
requires :branch, type: String, desc: 'The name of the branch'
|
|
|
|
end
|
|
|
|
delete ":id/repository/branches/:branch", requirements: { branch: /.+/ } do
|
|
|
|
authorize_push_project
|
|
|
|
|
2017-06-21 09:48:12 -04:00
|
|
|
result = DeleteBranchService.new(user_project, current_user)
|
|
|
|
.execute(params[:branch])
|
2017-02-20 14:32:44 -05:00
|
|
|
|
|
|
|
if result[:status] == :success
|
|
|
|
status(200)
|
|
|
|
{
|
|
|
|
branch_name: params[:branch]
|
|
|
|
}
|
|
|
|
else
|
|
|
|
render_api_error!(result[:message], result[:return_code])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-22 12:37:13 -05:00
|
|
|
desc 'Delete all merged branches'
|
|
|
|
delete ":id/repository/merged_branches" do
|
|
|
|
DeleteMergedBranchesService.new(user_project, current_user).async_execute
|
|
|
|
|
|
|
|
status(200)
|
|
|
|
end
|
2017-03-16 20:15:05 -04:00
|
|
|
|
|
|
|
desc 'Create branch' do
|
2017-10-05 04:48:05 -04:00
|
|
|
success ::API::Entities::Branch
|
2017-03-16 20:15:05 -04:00
|
|
|
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
|
|
|
|
post ":id/repository/branches" do
|
|
|
|
authorize_push_project
|
2017-06-21 09:48:12 -04:00
|
|
|
result = CreateBranchService.new(user_project, current_user)
|
|
|
|
.execute(params[:branch_name], params[:ref])
|
2017-03-16 20:15:05 -04:00
|
|
|
|
|
|
|
if result[:status] == :success
|
|
|
|
present result[:branch],
|
2017-10-05 04:48:05 -04:00
|
|
|
with: ::API::Entities::Branch,
|
2017-03-16 20:15:05 -04:00
|
|
|
project: user_project
|
|
|
|
else
|
|
|
|
render_api_error!(result[:message], 400)
|
|
|
|
end
|
|
|
|
end
|
2017-02-06 13:38:17 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|