summaryrefslogtreecommitdiff
path: root/lib/gitlab/gitlab_import/importer.rb
blob: 3e9087a556cbbdfe942020774cc9ddf04f04f3d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
44
45
46
47
48
module Gitlab
  module GitlabImport
    class Importer
      attr_reader :project, :client

      def initialize(project)
        @project = project
        @client = Client.new(project.creator.gitlab_access_token)
      end

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

        #Issues && Comments
        issues = client.issues(project_identifier)
        
        issues.each do |issue|
          body = "*Created by: #{issue["author"]["name"]}*\n\n#{issue["description"]}"
          
          
          comments = client.issue_comments(project_identifier, issue["id"])
          if comments.any?
            body += "\n\n\n**Imported comments:**\n"
          end
          comments.each do |comment|
            body += "\n\n*By #{comment["author"]["name"]} on #{comment["created_at"]}*\n\n#{comment["body"]}"
          end

          project.issues.create!(
            description: body, 
            title: issue["title"],
            state: issue["state"],
            author_id: gl_user_id(project, issue["author"]["id"])
          )
        end
        
        true
      end

      private

      def gl_user_id(project, gitlab_id)
        user = User.joins(:identities).find_by("identities.extern_uid = ?", gitlab_id.to_s)
        (user && user.id) || project.creator_id
      end
    end
  end
end