importer.rb 4.93 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

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

      def initialize(project)
        @project = project
10 11 12 13 14
        @repo = project.import_source
        @repo_url = project.import_url

        if credentials
          @client = Client.new(credentials[:user])
James Lopez's avatar
James Lopez committed
15 16 17 18
          @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
19 20 21
      end

      def execute
22 23
        import_labels && import_milestones && import_issues &&
          import_pull_requests && import_wiki
24 25 26 27
      end

      private

28 29
      def credentials
        @credentials ||= project.import_data.credentials if project.import_data
James Lopez's avatar
James Lopez committed
30 31
      end

32 33 34 35 36 37 38 39 40 41
      def import_labels
        client.labels(project.import_source).each do |raw_data|
          Label.create!(LabelFormatter.new(project, raw_data).attributes)
        end

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

42 43 44 45 46 47 48 49 50 51
      def import_milestones
        client.list_milestones(project.import_source, state: :all).each do |raw_data|
          Milestone.create!(MilestoneFormatter.new(project, raw_data).attributes)
        end

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

52
      def import_issues
53 54
        client.list_issues(project.import_source, state: :all,
                                                  sort: :created,
55 56
                                                  direction: :asc).each do |raw_data|
          gh_issue = IssueFormatter.new(project, raw_data)
57

58 59
          if gh_issue.valid?
            issue = Issue.create!(gh_issue.attributes)
60
            apply_labels(gh_issue.number, issue)
61

62 63
            if gh_issue.has_comments?
              import_comments(gh_issue.number, issue)
Valery Sizov's avatar
Valery Sizov committed
64 65 66
            end
          end
        end
67 68

        true
69 70
      rescue ActiveRecord::RecordInvalid => e
        raise Projects::ImportService::Error, e.message
Valery Sizov's avatar
Valery Sizov committed
71 72
      end

73
      def import_pull_requests
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
        pull_requests = client.pull_requests(repo, state: :all, sort: :created, direction: :asc)
                              .map { |raw| PullRequestFormatter.new(project, raw) }
                              .reject(&:cross_project?)

        source_branches_removed = pull_requests.reject(&:source_branch_exists?)
        source_branches_removed.each do |pull_request|
          client.create_ref(repo, "refs/heads/#{pull_request.source_branch}", pull_request.source_sha)
        end

        project.repository.fetch_ref(repo_url, '+refs/heads/*', 'refs/heads/*')

        pull_requests.each do |pull_request|
          merge_request = MergeRequest.new(pull_request.attributes)

          if merge_request.save
            apply_labels(pull_request.number, merge_request)
            import_comments(pull_request.number, merge_request)
            import_comments_on_diff(pull_request.number, merge_request)
92
          end
93
        end
94

95 96 97 98
        source_branches_removed.each do |pull_request|
          client.delete_ref(repo, "heads/#{pull_request.source_branch}")
        end

99
        true
100 101
      rescue ActiveRecord::RecordInvalid => e
        raise Projects::ImportService::Error, e.message
102 103
      end

104 105 106 107 108 109 110 111 112 113 114 115
      def apply_labels(number, issuable)
        issue = client.issue(project.import_source, number)

        if issue.labels.count > 0
          label_ids = issue.labels.map do |raw|
            Label.find_by(LabelFormatter.new(project, raw).attributes).try(:id)
          end

          issuable.update_attribute(:label_ids, label_ids)
        end
      end

116 117 118
      def import_comments(issue_number, noteable)
        comments = client.issue_comments(project.import_source, issue_number)
        create_comments(comments, noteable)
119 120
      end

121 122 123
      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)
124
      end
Valery Sizov's avatar
Valery Sizov committed
125

126 127 128 129
      def create_comments(comments, noteable)
        comments.each do |raw_data|
          comment = CommentFormatter.new(project, raw_data)
          noteable.notes.create!(comment.attributes)
130
        end
Valery Sizov's avatar
Valery Sizov committed
131
      end
132 133 134 135 136 137 138

      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
139 140

        true
141
      rescue Gitlab::Shell::Error => e
142 143 144 145 146
        # 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
147
        else
148
          true
149
        end
150
      end
Valery Sizov's avatar
Valery Sizov committed
151 152 153
    end
  end
end