BigW Consortium Gitlab

branches.rb 5.84 KB
Newer Older
1 2 3 4
require 'mime/types'

module API
  class Branches < Grape::API
5 6
    include PaginationParams

7 8
    BRANCH_ENDPOINT_REQUIREMENTS = API::PROJECT_ENDPOINT_REQUIREMENTS.merge(branch: API::NO_SLASH_URL_PART_REGEX)

9 10
    before { authorize! :download_code, user_project }

11 12 13 14 15 16 17 18 19 20
    helpers do
      def find_branch!(branch_name)
        begin
          user_project.repository.find_branch(branch_name) || not_found!('Branch')
        rescue Gitlab::Git::CommandError
          render_api_error!('The branch refname is invalid', 400)
        end
      end
    end

21 22 23
    params do
      requires :id, type: String, desc: 'The ID of a project'
    end
24
    resource :projects, requirements: API::PROJECT_ENDPOINT_REQUIREMENTS do
25
      desc 'Get a project repository branches' do
26
        success Entities::Branch
27
      end
28 29 30
      params do
        use :pagination
      end
31
      get ':id/repository/branches' do
32 33 34
        repository = user_project.repository
        branches = ::Kaminari.paginate_array(repository.branches.sort_by(&:name))
        merged_branch_names = repository.merged_branch_names(branches.map(&:name))
35

36
        present paginate(branches), with: Entities::Branch, project: user_project, merged_branch_names: merged_branch_names
37 38
      end

39 40
      resource ':id/repository/branches/:branch', requirements: BRANCH_ENDPOINT_REQUIREMENTS do
        desc 'Get a single branch' do
41
          success Entities::Branch
42 43 44 45 46 47 48 49
        end
        params do
          requires :branch, type: String, desc: 'The name of the branch'
        end
        head do
          user_project.repository.branch_exists?(params[:branch]) ? status(204) : status(404)
        end
        get do
50
          branch = find_branch!(params[:branch])
51

52
          present branch, with: Entities::Branch, project: user_project
53
        end
54 55
      end

56
      # Note: This API will be deprecated in favor of the protected branches API.
57 58 59
      # 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`.
60
      desc 'Protect a single branch' do
61
        success Entities::Branch
62 63
      end
      params do
64
        requires :branch, type: String, desc: 'The name of the branch'
65 66 67
        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
68
      put ':id/repository/branches/:branch/protect', requirements: BRANCH_ENDPOINT_REQUIREMENTS do
69 70
        authorize_admin_project

71
        branch = find_branch!(params[:branch])
72 73

        protected_branch = user_project.protected_branches.find_by(name: branch.name)
74

75
        protected_branch_params = {
76 77 78
          name: branch.name,
          developers_can_push: params[:developers_can_push],
          developers_can_merge: params[:developers_can_merge]
79 80
        }

81
        service_args = [user_project, current_user, protected_branch_params]
82

83
        protected_branch = if protected_branch
84
                             ::ProtectedBranches::LegacyApiUpdateService.new(*service_args).execute(protected_branch)
85
                           else
86
                             ::ProtectedBranches::LegacyApiCreateService.new(*service_args).execute
87
                           end
88

89
        if protected_branch.valid?
90
          present branch, with: Entities::Branch, project: user_project
91
        else
92
          render_api_error!(protected_branch.errors.full_messages, 422)
93
        end
94 95
      end

96
      # Note: This API will be deprecated in favor of the protected branches API.
97
      desc 'Unprotect a single branch' do
98
        success Entities::Branch
99 100
      end
      params do
101
        requires :branch, type: String, desc: 'The name of the branch'
102
      end
103
      put ':id/repository/branches/:branch/unprotect', requirements: BRANCH_ENDPOINT_REQUIREMENTS do
104 105
        authorize_admin_project

106
        branch = find_branch!(params[:branch])
107
        protected_branch = user_project.protected_branches.find_by(name: branch.name)
108
        protected_branch&.destroy
109

110
        present branch, with: Entities::Branch, project: user_project
111
      end
112

113
      desc 'Create branch' do
114
        success Entities::Branch
115 116
      end
      params do
117
        requires :branch, type: String, desc: 'The name of the branch'
118 119
        requires :ref, type: String, desc: 'Create branch from commit sha or existing branch'
      end
120
      post ':id/repository/branches' do
121
        authorize_push_project
122

123 124
        result = CreateBranchService.new(user_project, current_user)
                 .execute(params[:branch], params[:ref])
125

126 127
        if result[:status] == :success
          present result[:branch],
128
                  with: Entities::Branch,
129 130 131 132
                  project: user_project
        else
          render_api_error!(result[:message], 400)
        end
133
      end
134

135 136
      desc 'Delete a branch'
      params do
137
        requires :branch, type: String, desc: 'The name of the branch'
138
      end
139
      delete ':id/repository/branches/:branch', requirements: BRANCH_ENDPOINT_REQUIREMENTS do
140
        authorize_push_project
141

142
        branch = find_branch!(params[:branch])
143 144 145

        commit = user_project.repository.commit(branch.dereferenced_target)

146
        destroy_conditionally!(commit, last_updated: commit.authored_date) do
147 148
          result = DeleteBranchService.new(user_project, current_user)
                    .execute(params[:branch])
149

150 151 152
          if result[:status] != :success
            render_api_error!(result[:message], result[:return_code])
          end
153
        end
154
      end
155

156
      desc 'Delete all merged branches'
157
      delete ':id/repository/merged_branches' do
158 159
        DeleteMergedBranchesService.new(user_project, current_user).async_execute

160
        accepted!
161
      end
162 163 164
    end
  end
end