Welcome to mirror list, hosted at ThFree Co, Russian Federation.

repository_importer.rb « importer « github_import « gitlab « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: aba4729e9c8ebdeda31672eb2ac521e408f5672d (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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# frozen_string_literal: true

module Gitlab
  module GithubImport
    module Importer
      class RepositoryImporter
        include Gitlab::Utils::StrongMemoize

        attr_reader :project, :client, :wiki_formatter

        def initialize(project, client)
          @project = project
          @client = client
          @wiki_formatter = ::Gitlab::LegacyGithubImport::WikiFormatter.new(project)
        end

        # Returns true if we should import the wiki for the project.
        # rubocop: disable CodeReuse/ActiveRecord
        def import_wiki?
          client_repository&.has_wiki &&
            !project.wiki_repository_exists? &&
            Gitlab::GitalyClient::RemoteService.exists?(wiki_url)
        end
        # rubocop: enable CodeReuse/ActiveRecord

        # Imports the repository data.
        #
        # This method will return true if the data was imported successfully or
        # the repository had already been imported before.
        def execute
          imported =
            # It's possible a repository has already been imported when running
            # this code, e.g. because we had to retry this job after
            # `import_wiki?` raised a rate limit error. In this case we'll skip
            # re-importing the main repository.
            if project.empty_repo?
              import_repository
            else
              true
            end

          update_clone_time if imported

          imported = import_wiki_repository if import_wiki? && imported

          imported
        end

        def import_repository
          project.ensure_repository

          refmap = Gitlab::GithubImport.refmap
          project.repository.fetch_as_mirror(project.import_url, refmap: refmap, forced: true)

          project.change_head(default_branch) if default_branch

          # The initial fetch can bring in lots of loose refs and objects.
          # Running a `git gc` will make importing pull requests faster.
          Repositories::HousekeepingService.new(project, :gc).execute

          true
        end

        def import_wiki_repository
          project.wiki.repository.import_repository(wiki_formatter.import_url)

          true
        rescue ::Gitlab::Git::CommandError => e
          if e.message !~ /repository not exported/
            project.create_wiki

            raise e
          else
            true
          end
        end

        def wiki_url
          wiki_formatter.import_url
        end

        def update_clone_time
          project.touch(:last_repository_updated_at) # rubocop: disable Rails/SkipsModelValidations
        end

        private

        def default_branch
          client_repository&.default_branch
        end

        def client_repository
          strong_memoize(:client_repository) do
            client.repository(project.import_source)
          end
        end
      end
    end
  end
end