BigW Consortium Gitlab

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

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

      def initialize(project)
        @project = project
10 11 12
        import_data = project.import_data.try(:data)
        github_session = import_data["github_session"] if import_data
        @client = Client.new(github_session["github_access_token"])
13
        @formatter = Gitlab::ImportFormatter.new
Valery Sizov committed
14 15 16
      end

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

      private

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

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

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

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

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

48
          if !pull_request.cross_project? && pull_request.valid?
49
            merge_request = MergeRequest.create!(pull_request.attributes)
50 51
            import_comments(pull_request.number, merge_request)
            import_comments_on_diff(pull_request.number, merge_request)
52
          end
53
        end
54 55

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

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

65 66 67
      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)
68
      end
Valery Sizov committed
69

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

      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
83 84

        true
85
      rescue Gitlab::Shell::Error => e
86 87 88 89 90
        # 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
91
        else
92
          true
93
        end
94
      end
Valery Sizov committed
95 96 97
    end
  end
end