Module: Danger::Helpers::CommentsParsingHelper

Included in:
CommentsHelper
Defined in:
lib/danger/helpers/comments_parsing_helper.rb

Extension points collapse

Instance Method Summary collapse

Instance Method Details

#parse_comment(comment) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/danger/helpers/comments_parsing_helper.rb', line 28

def parse_comment(comment)
  tables = parse_tables_from_comment(comment)
  violations = {}
  tables.each do |table|
    match = danger_table?(table)
    next unless match

    title = match[1]
    kind = table_kind_from_title(title)
    next unless kind

    violations[kind] = violations_from_table(table)
  end

  violations.reject { |_, v| v.empty? }
end

#parse_message_from_row(row) ⇒ Violation or Markdown

Produces a message-like from a row in a comment table

Parameters:

  • row (String)

    The content of the row in the table

Returns:



11
12
13
# File 'lib/danger/helpers/comments_parsing_helper.rb', line 11

def parse_message_from_row(row)
  Violation.new(row, true)
end

#parse_tables_from_comment(comment) ⇒ Object



17
18
19
# File 'lib/danger/helpers/comments_parsing_helper.rb', line 17

def parse_tables_from_comment(comment)
  comment.split("</table>")
end

#table_kind_from_title(title) ⇒ Object



45
46
47
48
49
50
51
52
53
# File 'lib/danger/helpers/comments_parsing_helper.rb', line 45

def table_kind_from_title(title)
  if title =~ /error/i
    :error
  elsif title =~ /warning/i
    :warning
  elsif title =~ /message/i
    :message
  end
end

#violations_from_table(table) ⇒ Object



21
22
23
24
25
26
# File 'lib/danger/helpers/comments_parsing_helper.rb', line 21

def violations_from_table(table)
  row_regex = %r{<td data-sticky="true">(?:<del>)?(.*?)(?:</del>)?\s*</td>}im
  table.scan(row_regex).flatten.map do |row|
    parse_message_from_row(row.strip)
  end
end