summaryrefslogtreecommitdiff
path: root/lib/gitlab/github_import/importer/protected_branch_importer.rb
blob: 16215fdce8e2516a0801af2a4b6a440c850d39c9 (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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# frozen_string_literal: true

module Gitlab
  module GithubImport
    module Importer
      class ProtectedBranchImporter
        attr_reader :protected_branch, :project, :client

        # protected_branch - An instance of
        #   `Gitlab::GithubImport::Representation::ProtectedBranch`.
        # project - An instance of `Project`
        # client - An instance of `Gitlab::GithubImport::Client`
        def initialize(protected_branch, project, client)
          @protected_branch = protected_branch
          @project = project
          @client = client
        end

        def execute
          # The creator of the project is always allowed to create protected
          # branches, so we skip the authorization check in this service class.
          ProtectedBranches::CreateService
            .new(project, project.creator, params)
            .execute(skip_authorization: true)
        end

        private

        def params
          {
            name: protected_branch.id,
            push_access_levels_attributes: [{ access_level: Gitlab::Access::MAINTAINER }],
            merge_access_levels_attributes: [{ access_level: Gitlab::Access::MAINTAINER }],
            allow_force_push: allow_force_push?
          }
        end

        def allow_force_push?
          if ProtectedBranch.protected?(project, protected_branch.id)
            ProtectedBranch.allow_force_push?(project, protected_branch.id) && protected_branch.allow_force_pushes
          else
            protected_branch.allow_force_pushes
          end
        end
      end
    end
  end
end