BigW Consortium Gitlab

importer.rb 1.88 KB
Newer Older
1 2 3 4 5 6 7
module Gitlab
  module GitlabImport
    class Importer
      attr_reader :project, :client

      def initialize(project)
        @project = project
8 9 10
        import_data = project.import_data
        if import_data && import_data.credentials && import_data.credentials[:password]
          @client = Client.new(import_data.credentials[:password])
11 12 13 14
          @formatter = Gitlab::ImportFormatter.new
        else
          raise Projects::ImportService::Error, "Unable to find project import data credentials for project ID: #{@project.id}"
        end
15 16 17
      end

      def execute
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
        ActiveRecord::Base.no_touching do
          project_identifier = CGI.escape(project.import_source)

          # Issues && Comments
          issues = client.issues(project_identifier)

          issues.each do |issue|
            body = @formatter.author_line(issue["author"]["name"])
            body += issue["description"]

            comments = client.issue_comments(project_identifier, issue["id"])

            if comments.any?
              body += @formatter.comments_header
            end

            comments.each do |comment|
              body += @formatter.comment(comment["author"]["name"], comment["created_at"], comment["body"])
            end

            project.issues.create!(
              iid: issue["iid"],
              description: body,
              title: issue["title"],
              state: issue["state"],
              updated_at: issue["updated_at"],
44
              author_id: gitlab_user_id(project, issue["author"]["id"]),
45
              confidential: issue["confidential"]
46
            )
47 48
          end
        end
49

50 51 52 53 54
        true
      end

      private

55
      def gitlab_user_id(project, gitlab_id)
56
        user = User.joins(:identities).find_by("identities.extern_uid = ? AND identities.provider = 'gitlab'", gitlab_id.to_s)
57 58 59 60 61
        (user && user.id) || project.creator_id
      end
    end
  end
end