comment_formatter.rb 1.3 KB
Newer Older
1 2
module Gitlab
  module GithubImport
3
    class CommentFormatter < BaseFormatter
4 5
      attr_writer :author_id

6 7 8 9 10 11 12
      def attributes
        {
          project: project,
          note: note,
          commit_id: raw_data.commit_id,
          line_code: line_code,
          author_id: author_id,
13
          type: type,
14 15 16 17 18 19 20 21
          created_at: raw_data.created_at,
          updated_at: raw_data.updated_at
        }
      end

      private

      def author
22
        @author ||= UserFormatter.new(client, raw_data.user)
23 24 25
      end

      def author_id
26
        author.gitlab_id || project.creator_id
27 28 29 30 31 32 33
      end

      def body
        raw_data.body || ""
      end

      def line_code
34 35 36 37 38 39 40
        return unless on_diff?

        parsed_lines = Gitlab::Diff::Parser.new.parse(diff_hunk.lines)
        generate_line_code(parsed_lines.to_a.last)
      end

      def generate_line_code(line)
41
        Gitlab::Git.diff_line_code(file_path, line.new_pos, line.old_pos)
42 43 44
      end

      def on_diff?
45 46 47 48 49 50 51 52 53
        diff_hunk.present?
      end

      def diff_hunk
        raw_data.diff_hunk
      end

      def file_path
        raw_data.path
54 55 56
      end

      def note
57
        if author.gitlab_id
58 59
          body
        else
60
          formatter.author_line(author.login) + body
61
        end
62
      end
63 64 65 66

      def type
        'LegacyDiffNote' if on_diff?
      end
67 68 69
    end
  end
end