summaryrefslogtreecommitdiff
path: root/lib/gitlab/legacy_github_import/project_creator.rb
blob: ca1a1b8e9bd468ff266c5515ddf67efb9fd519bf (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
49
50
51
52
53
54
55
56
# frozen_string_literal: true

module Gitlab
  module LegacyGithubImport
    class ProjectCreator
      attr_reader :repo, :name, :namespace, :current_user, :session_data, :type

      def initialize(repo, name, namespace, current_user, session_data, type: 'github')
        @repo = repo
        @name = name
        @namespace = namespace
        @current_user = current_user
        @session_data = session_data
        @type = type
      end

      def execute(extra_attrs = {})
        attrs = {
          name: name,
          path: name,
          description: repo.description,
          namespace_id: namespace.id,
          visibility_level: visibility_level,
          import_type: type,
          import_source: repo.full_name,
          import_url: import_url,
          skip_wiki: skip_wiki
        }.merge!(extra_attrs)

        ::Projects::CreateService.new(current_user, attrs).execute
      end

      private

      def import_url
        repo.clone_url.sub('://', "://#{session_data[:github_access_token]}@")
      end

      def visibility_level
        visibility_level = repo.private ? Gitlab::VisibilityLevel::PRIVATE : Gitlab::VisibilityLevel::PUBLIC
        visibility_level = Gitlab::CurrentSettings.default_project_visibility if Gitlab::CurrentSettings.restricted_visibility_levels.include?(visibility_level)

        visibility_level
      end

      #
      # If the GitHub project repository has wiki, we should not create the
      # default wiki. Otherwise the GitHub importer will fail because the wiki
      # repository already exist.
      #
      def skip_wiki
        repo.has_wiki?
      end
    end
  end
end