summaryrefslogtreecommitdiff
path: root/lib/gitlab/github_import/comment_formatter.rb
blob: 8911b81ec9a67f6853d0be42861868a039876796 (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
module Gitlab
  module GithubImport
    class CommentFormatter < BaseFormatter
      attr_writer :author_id

      def attributes
        {
          project: project,
          note: note,
          commit_id: raw_data.commit_id,
          line_code: line_code,
          author_id: author_id,
          type: type,
          created_at: raw_data.created_at,
          updated_at: raw_data.updated_at
        }
      end

      private

      def author
        @author ||= UserFormatter.new(client, raw_data.user)
      end

      def author_id
        author.gitlab_id || project.creator_id
      end

      def body
        raw_data.body || ""
      end

      def line_code
        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)
        Gitlab::Git.diff_line_code(file_path, line.new_pos, line.old_pos)
      end

      def on_diff?
        diff_hunk.present?
      end

      def diff_hunk
        raw_data.diff_hunk
      end

      def file_path
        raw_data.path
      end

      def note
        if author.gitlab_id
          body
        else
          formatter.author_line(author.login) + body
        end
      end

      def type
        'LegacyDiffNote' if on_diff?
      end
    end
  end
end