branches.rb 2.44 KB
Newer Older
Robert Schilling's avatar
Robert Schilling committed
1 2 3 4 5 6 7 8 9 10 11
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
12
      resource :projects, requirements: { id: %r{[^/]+} } do
Robert Schilling's avatar
Robert Schilling committed
13
        desc 'Get a project repository branches' do
14
          success ::API::Entities::Branch
Robert Schilling's avatar
Robert Schilling committed
15 16
        end
        get ":id/repository/branches" do
17 18
          Gitlab::QueryLimiting.whitelist('https://gitlab.com/gitlab-org/gitlab-ce/issues/42276')

19 20 21
          repository = user_project.repository
          branches = repository.branches.sort_by(&:name)
          merged_branch_names = repository.merged_branch_names(branches.map(&:name))
Robert Schilling's avatar
Robert Schilling committed
22

23
          present branches, with: ::API::Entities::Branch, project: user_project, merged_branch_names: merged_branch_names
Robert Schilling's avatar
Robert Schilling committed
24
        end
25

Robert Schilling's avatar
Robert Schilling committed
26 27 28 29 30 31 32
        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

33 34
          result = DeleteBranchService.new(user_project, current_user)
                   .execute(params[:branch])
Robert Schilling's avatar
Robert Schilling committed
35 36 37 38 39 40 41 42 43 44 45

          if result[:status] == :success
            status(200)
            {
              branch_name: params[:branch]
            }
          else
            render_api_error!(result[:message], result[:return_code])
          end
        end

46 47 48 49 50 51
        desc 'Delete all merged branches'
        delete ":id/repository/merged_branches" do
          DeleteMergedBranchesService.new(user_project, current_user).async_execute

          status(200)
        end
52 53

        desc 'Create branch' do
54
          success ::API::Entities::Branch
55 56 57 58 59 60 61
        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
62 63
          result = CreateBranchService.new(user_project, current_user)
            .execute(params[:branch_name], params[:ref])
64 65 66

          if result[:status] == :success
            present result[:branch],
67
              with: ::API::Entities::Branch,
68 69 70 71 72
              project: user_project
          else
            render_api_error!(result[:message], 400)
          end
        end
Robert Schilling's avatar
Robert Schilling committed
73 74 75 76
      end
    end
  end
end