Class: Gitlab::GithubImport::Importer::RepositoryImporter

Inherits:
Object
  • Object
show all
Includes:
Utils::StrongMemoize
Defined in:
lib/gitlab/github_import/importer/repository_importer.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Constructor Details

#initialize(project, client) ⇒ RepositoryImporter

Returns a new instance of RepositoryImporter.


11
12
13
14
15
# File 'lib/gitlab/github_import/importer/repository_importer.rb', line 11

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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client


9
10
11
# File 'lib/gitlab/github_import/importer/repository_importer.rb', line 9

def client
  @client
end

#projectObject (readonly)

Returns the value of attribute project


9
10
11
# File 'lib/gitlab/github_import/importer/repository_importer.rb', line 9

def project
  @project
end

#wiki_formatterObject (readonly)

Returns the value of attribute wiki_formatter


9
10
11
# File 'lib/gitlab/github_import/importer/repository_importer.rb', line 9

def wiki_formatter
  @wiki_formatter
end

Instance Method Details

#executeObject

Imports the repository data.

This method will return true if the data was imported successfully or the repository had already been imported before.


30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/gitlab/github_import/importer/repository_importer.rb', line 30

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

#fail_import(message) ⇒ Object


87
88
89
90
# File 'lib/gitlab/github_import/importer/repository_importer.rb', line 87

def fail_import(message)
  project.import_state.mark_as_failed(message)
  false
end

#import_repositoryObject


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/gitlab/github_import/importer/repository_importer.rb', line 49

def import_repository
  project.ensure_repository

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

  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.
  Projects::HousekeepingService.new(project, :gc).execute

  true
rescue Gitlab::Git::Repository::NoRepository, Gitlab::Shell::Error => e
  fail_import("Failed to import the repository: #{e.message}")
end

#import_wiki?Boolean

Returns true if we should import the wiki for the project. rubocop: disable CodeReuse/ActiveRecord

Returns:

  • (Boolean)

19
20
21
22
23
# File 'lib/gitlab/github_import/importer/repository_importer.rb', line 19

def import_wiki?
  client_repository&.has_wiki &&
    !project.wiki_repository_exists? &&
    Gitlab::GitalyClient::RemoteService.exists?(wiki_url)
end

#import_wiki_repositoryObject


66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/gitlab/github_import/importer/repository_importer.rb', line 66

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
    fail_import("Failed to import the wiki: #{e.message}")
  else
    true
  end
end

#update_clone_timeObject


83
84
85
# File 'lib/gitlab/github_import/importer/repository_importer.rb', line 83

def update_clone_time
  project.update_column(:last_repository_updated_at, Time.zone.now)
end

#wiki_urlObject


79
80
81
# File 'lib/gitlab/github_import/importer/repository_importer.rb', line 79

def wiki_url
  wiki_formatter.import_url
end