summaryrefslogtreecommitdiff
path: root/app/services/protected_branches/create_service.rb
blob: 6150a2a83c93fa0d8aef395c7ec062bc4f3934b7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
module ProtectedBranches
  class CreateService < BaseService
    attr_reader :protected_branch

    def execute
      raise Gitlab::Access::AccessDeniedError unless can?(current_user, :admin_project, project)

      protected_branch = project.protected_branches.new(params)

      ProtectedBranch.transaction do
        protected_branch.save!

        if protected_branch.push_access_level.blank?
          protected_branch.create_push_access_level!(access_level: Gitlab::Access::MASTER)
        end

        if protected_branch.merge_access_level.blank?
          protected_branch.create_merge_access_level!(access_level: Gitlab::Access::MASTER)
        end
      end

      protected_branch
    rescue ActiveRecord::RecordInvalid
      protected_branch
    end
  end
end