importer.rb 3.09 KB
Newer Older
Valery Sizov's avatar
Valery Sizov committed
1
module Gitlab
2
  module GithubImport
Valery Sizov's avatar
Valery Sizov committed
3
    class Importer
4
5
      include Gitlab::ShellAdapter

Valery Sizov's avatar
Valery Sizov committed
6
      attr_reader :project, :client
Valery Sizov's avatar
Valery Sizov committed
7
8
9

      def initialize(project)
        @project = project
10
        credentials = project.import_data.credentials if import_data
11
        @client = Client.new(credentials[:user])
12
        @formatter = Gitlab::ImportFormatter.new
Valery Sizov's avatar
Valery Sizov committed
13
14
15
      end

      def execute
16
        import_issues && import_pull_requests && import_wiki
17
18
19
20
21
      end

      private

      def import_issues
22
23
        client.list_issues(project.import_source, state: :all,
                                                  sort: :created,
24
25
                                                  direction: :asc).each do |raw_data|
          gh_issue = IssueFormatter.new(project, raw_data)
26

27
28
          if gh_issue.valid?
            issue = Issue.create!(gh_issue.attributes)
29

30
31
            if gh_issue.has_comments?
              import_comments(gh_issue.number, issue)
Valery Sizov's avatar
Valery Sizov committed
32
33
34
            end
          end
        end
35
36

        true
37
38
      rescue ActiveRecord::RecordInvalid => e
        raise Projects::ImportService::Error, e.message
Valery Sizov's avatar
Valery Sizov committed
39
40
      end

41
42
43
      def import_pull_requests
        client.pull_requests(project.import_source, state: :all,
                                                    sort: :created,
44
                                                    direction: :asc).each do |raw_data|
45
          pull_request = PullRequestFormatter.new(project, raw_data)
46

47
48
49
50
51
52
53
          if pull_request.valid?
            merge_request = MergeRequest.new(pull_request.attributes)

            if merge_request.save
              import_comments(pull_request.number, merge_request)
              import_comments_on_diff(pull_request.number, merge_request)
            end
54
          end
55
        end
56
57

        true
58
59
      rescue ActiveRecord::RecordInvalid => e
        raise Projects::ImportService::Error, e.message
60
61
      end

62
63
64
      def import_comments(issue_number, noteable)
        comments = client.issue_comments(project.import_source, issue_number)
        create_comments(comments, noteable)
65
66
      end

67
68
69
      def import_comments_on_diff(pull_request_number, merge_request)
        comments = client.pull_request_comments(project.import_source, pull_request_number)
        create_comments(comments, merge_request)
70
      end
Valery Sizov's avatar
Valery Sizov committed
71

72
73
74
75
      def create_comments(comments, noteable)
        comments.each do |raw_data|
          comment = CommentFormatter.new(project, raw_data)
          noteable.notes.create!(comment.attributes)
76
        end
Valery Sizov's avatar
Valery Sizov committed
77
      end
78
79
80
81
82
83
84

      def import_wiki
        unless project.wiki_enabled?
          wiki = WikiFormatter.new(project)
          gitlab_shell.import_repository(wiki.path_with_namespace, wiki.import_url)
          project.update_attribute(:wiki_enabled, true)
        end
85
86

        true
87
      rescue Gitlab::Shell::Error => e
88
89
90
91
92
        # GitHub error message when the wiki repo has not been created,
        # this means that repo has wiki enabled, but have no pages. So,
        # we can skip the import.
        if e.message !~ /repository not exported/
          raise Projects::ImportService::Error, e.message
93
        else
94
          true
95
        end
96
      end
Valery Sizov's avatar
Valery Sizov committed
97
98
99
    end
  end
end