module Danger::Helpers::CommentsParsingHelper

Constants

GITHUB_OLD_REGEX
NEW_REGEX

Public Instance Methods

parse_comment(comment) click to toggle source
# 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) click to toggle source

@!group Extension points Produces a message-like from a row in a comment table

@param [String] row

The content of the row in the table

@return [Violation or Markdown] the extracted message

# 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) click to toggle source

@endgroup

# 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) click to toggle source
# 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) click to toggle source
# 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

Private Instance Methods

danger_table?(table) click to toggle source
# File lib/danger/helpers/comments_parsing_helper.rb, line 60
def danger_table?(table)
  # The old GitHub specific method relied on
  # the width of a `th` element to find the table
  # title and determine if it was a danger table.
  # The new method uses a more robust data-danger-table
  # tag instead.
  match = GITHUB_OLD_REGEX.match(table)
  return match if match

  return NEW_REGEX.match(table)
end