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 }
|
|
|
|
|
|
|
|
resource :projects do
|
|
|
|
# Get a project repository branches
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# Example Request:
|
|
|
|
# GET /projects/:id/repository/branches
|
|
|
|
get ":id/repository/branches" do
|
2014-12-30 07:36:13 -05:00
|
|
|
branches = user_project.repository.branches.sort_by(&:name)
|
|
|
|
present branches, with: Entities::RepoObject, project: user_project
|
2014-03-31 09:31:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Get a single branch
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# branch (required) - The name of the branch
|
|
|
|
# Example Request:
|
|
|
|
# GET /projects/:id/repository/branches/:branch
|
2016-06-30 07:46:35 -04:00
|
|
|
get ':id/repository/branches/:branch', requirements: { branch: /.+/ } do
|
2014-09-25 09:22:08 -04:00
|
|
|
@branch = user_project.repository.branches.find { |item| item.name == params[:branch] }
|
2014-12-30 07:36:13 -05:00
|
|
|
not_found!("Branch") unless @branch
|
2014-03-31 09:31:53 -04:00
|
|
|
present @branch, with: Entities::RepoObject, project: user_project
|
|
|
|
end
|
|
|
|
|
|
|
|
# Protect a single branch
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# branch (required) - The name of the branch
|
|
|
|
# Example Request:
|
|
|
|
# PUT /projects/:id/repository/branches/:branch/protect
|
2014-05-06 17:28:21 -04:00
|
|
|
put ':id/repository/branches/:branch/protect',
|
2016-06-30 07:46:35 -04:00
|
|
|
requirements: { branch: /.+/ } do
|
2014-03-31 09:31:53 -04:00
|
|
|
authorize_admin_project
|
|
|
|
|
|
|
|
@branch = user_project.repository.find_branch(params[:branch])
|
2014-12-30 07:36:13 -05:00
|
|
|
not_found!("Branch") unless @branch
|
2014-03-31 09:31:53 -04:00
|
|
|
protected_branch = user_project.protected_branches.find_by(name: @branch.name)
|
|
|
|
user_project.protected_branches.create(name: @branch.name) unless protected_branch
|
|
|
|
|
|
|
|
present @branch, with: Entities::RepoObject, project: user_project
|
|
|
|
end
|
|
|
|
|
|
|
|
# Unprotect a single branch
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# branch (required) - The name of the branch
|
|
|
|
# Example Request:
|
|
|
|
# PUT /projects/:id/repository/branches/:branch/unprotect
|
2014-05-06 17:28:21 -04:00
|
|
|
put ':id/repository/branches/:branch/unprotect',
|
2016-06-30 07:46:35 -04:00
|
|
|
requirements: { branch: /.+/ } do
|
2014-03-31 09:31:53 -04:00
|
|
|
authorize_admin_project
|
|
|
|
|
|
|
|
@branch = user_project.repository.find_branch(params[:branch])
|
2016-04-06 05:37:31 -04:00
|
|
|
not_found!("Branch") unless @branch
|
2014-03-31 09:31:53 -04:00
|
|
|
protected_branch = user_project.protected_branches.find_by(name: @branch.name)
|
|
|
|
protected_branch.destroy if protected_branch
|
|
|
|
|
|
|
|
present @branch, with: Entities::RepoObject, project: user_project
|
|
|
|
end
|
2014-04-01 03:39:53 -04:00
|
|
|
|
|
|
|
# Create branch
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# branch_name (required) - The name of the branch
|
|
|
|
# ref (required) - Create branch from commit sha or existing branch
|
|
|
|
# Example Request:
|
|
|
|
# POST /projects/:id/repository/branches
|
|
|
|
post ":id/repository/branches" do
|
|
|
|
authorize_push_project
|
2014-09-21 04:29:52 -04:00
|
|
|
result = CreateBranchService.new(user_project, current_user).
|
|
|
|
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],
|
|
|
|
with: Entities::RepoObject,
|
|
|
|
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
|
|
|
|
|
|
|
# Delete branch
|
|
|
|
#
|
|
|
|
# Parameters:
|
|
|
|
# id (required) - The ID of a project
|
|
|
|
# branch (required) - The name of the branch
|
|
|
|
# Example Request:
|
|
|
|
# DELETE /projects/:id/repository/branches/:branch
|
2015-03-31 12:08:33 -04:00
|
|
|
delete ":id/repository/branches/:branch",
|
2016-06-30 07:46:35 -04:00
|
|
|
requirements: { branch: /.+/ } do
|
2014-05-22 07:39:50 -04:00
|
|
|
authorize_push_project
|
2014-09-21 04:29:52 -04:00
|
|
|
result = DeleteBranchService.new(user_project, current_user).
|
2015-03-25 11:15:26 -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
|
2014-03-31 09:31:53 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|