importer.rb 3.36 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
James Lopez's avatar
James Lopez committed
10
        if import_data_credentials
James Lopez's avatar
James Lopez committed
11
          @client = Client.new(import_data_credentials[:user])
James Lopez's avatar
James Lopez committed
12 13 14 15
          @formatter = Gitlab::ImportFormatter.new
        else
          raise Projects::ImportService::Error, "Unable to find project import data credentials for project ID: #{@project.id}"
        end
Valery Sizov's avatar
Valery Sizov committed
16 17 18
      end

      def execute
19
        import_issues && import_pull_requests && import_wiki
20 21 22 23
      end

      private

James Lopez's avatar
James Lopez committed
24 25 26 27
      def import_data_credentials
        @import_data_credentials ||= project.import_data.credentials if project.import_data
      end

28
      def import_issues
29 30
        client.list_issues(project.import_source, state: :all,
                                                  sort: :created,
31 32
                                                  direction: :asc).each do |raw_data|
          gh_issue = IssueFormatter.new(project, raw_data)
33

34 35
          if gh_issue.valid?
            issue = Issue.create!(gh_issue.attributes)
36

37 38
            if gh_issue.has_comments?
              import_comments(gh_issue.number, issue)
Valery Sizov's avatar
Valery Sizov committed
39 40 41
            end
          end
        end
42 43

        true
44 45
      rescue ActiveRecord::RecordInvalid => e
        raise Projects::ImportService::Error, e.message
Valery Sizov's avatar
Valery Sizov committed
46 47
      end

48 49 50
      def import_pull_requests
        client.pull_requests(project.import_source, state: :all,
                                                    sort: :created,
51
                                                    direction: :asc).each do |raw_data|
52
          pull_request = PullRequestFormatter.new(project, raw_data)
53

54 55 56 57 58 59 60
          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
61
          end
62
        end
63 64

        true
65 66
      rescue ActiveRecord::RecordInvalid => e
        raise Projects::ImportService::Error, e.message
67 68
      end

69 70 71
      def import_comments(issue_number, noteable)
        comments = client.issue_comments(project.import_source, issue_number)
        create_comments(comments, noteable)
72 73
      end

74 75 76
      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)
77
      end
Valery Sizov's avatar
Valery Sizov committed
78

79 80 81 82
      def create_comments(comments, noteable)
        comments.each do |raw_data|
          comment = CommentFormatter.new(project, raw_data)
          noteable.notes.create!(comment.attributes)
83
        end
Valery Sizov's avatar
Valery Sizov committed
84
      end
85 86 87 88 89 90 91

      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
92 93

        true
94
      rescue Gitlab::Shell::Error => e
95 96 97 98 99
        # 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
100
        else
101
          true
102
        end
103
      end
Valery Sizov's avatar
Valery Sizov committed
104 105 106
    end
  end
end