BigW Consortium Gitlab

importer.rb 1.5 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.try(:data)
        gitlab_session = import_data["gitlab_session"] if import_data
        @client = Client.new(gitlab_session["gitlab_access_token"])
11
        @formatter = Gitlab::ImportFormatter.new
12 13 14 15 16 17 18
      end

      def execute
        project_identifier = URI.encode(project.import_source, '/')

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

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

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

26
          if comments.any?
27
            body += @formatter.comments_header
28
          end
29

30
          comments.each do |comment|
Douwe Maan committed
31
            body += @formatter.comment(comment["author"]["name"], comment["created_at"], comment["body"])
32 33 34
          end

          project.issues.create!(
35
            description: body,
36 37 38 39 40
            title: issue["title"],
            state: issue["state"],
            author_id: gl_user_id(project, issue["author"]["id"])
          )
        end
41

42 43 44 45 46 47
        true
      end

      private

      def gl_user_id(project, gitlab_id)
48
        user = User.joins(:identities).find_by("identities.extern_uid = ? AND identities.provider = 'gitlab'", gitlab_id.to_s)
49 50 51 52 53
        (user && user.id) || project.creator_id
      end
    end
  end
end