summaryrefslogtreecommitdiff
path: root/lib/gitlab/import_export/snippet_repo_restorer.rb
blob: b58ea14a3a8cd1c6783608f638a2d29dd0a423b6 (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 ImportExport
    class SnippetRepoRestorer < RepoRestorer
      attr_reader :snippet

      def initialize(snippet:, user:, shared:, path_to_bundle:)
        @snippet = snippet
        @user = user
        @repository = snippet.repository
        @path_to_bundle = path_to_bundle.to_s
        @shared = shared
      end

      def restore
        if File.exist?(path_to_bundle)
          create_repository_from_bundle
        else
          create_repository_from_db
        end

        true
      rescue => e
        shared.error(e)
        false
      end

      private

      def create_repository_from_bundle
        repository.create_from_bundle(path_to_bundle)
        snippet.track_snippet_repository(repository.storage)
      end

      def create_repository_from_db
        snippet.create_repository

        commit_attrs = {
          branch_name: 'master',
          message: 'Initial commit'
        }

        repository.create_file(@user, snippet.file_name, snippet.content, commit_attrs)
      end
    end
  end
end