module Danger::Helpers::CommentsHelper

Public Instance Methods

apply_template(tables: [], markdowns: [], danger_id: "danger", template: "github", request_source: template) click to toggle source
# File lib/danger/helpers/comments_helper.rb, line 83
def apply_template(tables: [], markdowns: [], danger_id: "danger", template: "github", request_source: template)
  require "erb"

  md_template = File.join(Danger.gem_path, "lib/danger/comment_generators/#{template}.md.erb")

  # erb: http://www.rrn.dk/rubys-erb-templating-system
  # for the extra args: http://stackoverflow.com/questions/4632879/erb-template-removing-the-trailing-line
  @tables = tables
  @markdowns = markdowns.map(&:message)
  @danger_id = danger_id
  @emoji_mapper = EmojiMapper.new(request_source.sub("_inline", ""))

  return ERB.new(File.read(md_template), trim_mode: "-").result(binding)
end
generate_comment(warnings: [], errors: [], messages: [], markdowns: [], previous_violations: {}, danger_id: "danger", template: "github") click to toggle source
# File lib/danger/helpers/comments_helper.rb, line 98
def generate_comment(warnings: [], errors: [], messages: [], markdowns: [], previous_violations: {}, danger_id: "danger", template: "github")
  apply_template(
    tables: [
      table("Error", "no_entry_sign", errors, previous_violations, template: template),
      table("Warning", "warning", warnings, previous_violations, template: template),
      table("Message", "book", messages, previous_violations, template: template)
    ],
    markdowns: markdowns,
    danger_id: danger_id,
    template: template
  )
end
generate_description(warnings: nil, errors: nil, template: "github") click to toggle source
# File lib/danger/helpers/comments_helper.rb, line 151
def generate_description(warnings: nil, errors: nil, template: "github")
  emoji_mapper = EmojiMapper.new(template)
  if (errors.nil? || errors.empty?) && (warnings.nil? || warnings.empty?)
    return ENV["DANGER_SUCCESS_MESSAGE"] || "All green. #{random_compliment}"
  else
    message = "#{emoji_mapper.map('warning')} "
    message += "#{'Error'.danger_pluralize(errors.count)}. " unless errors.empty?
    message += "#{'Warning'.danger_pluralize(warnings.count)}. " unless warnings.empty?
    message += "Don't worry, everything is fixable."
    return message
  end
end
generate_inline_comment_body(emoji, message, danger_id: "danger", resolved: false, template: "github") click to toggle source
# File lib/danger/helpers/comments_helper.rb, line 131
def generate_inline_comment_body(emoji,
                                 message,
                                 danger_id: "danger",
                                 resolved: false,
                                 template: "github")
  apply_template(
    tables: [{ content: [message], resolved: resolved, emoji: emoji }],
    danger_id: danger_id,
    template: "#{template}_inline"
  )
end
generate_inline_markdown_body(markdown, danger_id: "danger", template: "github") click to toggle source
# File lib/danger/helpers/comments_helper.rb, line 143
def generate_inline_markdown_body(markdown, danger_id: "danger", template: "github")
  apply_template(
    markdowns: [markdown],
    danger_id: danger_id,
    template: "#{template}_inline"
  )
end
generate_message_group_comment(message_group:, danger_id: "danger", resolved: [], template: "github") click to toggle source

resolved is essentially reserved for future use - eventually we might have some nice generic resolved-thing going :)

# File lib/danger/helpers/comments_helper.rb, line 113
def generate_message_group_comment(message_group:,
                                   danger_id: "danger",
                                   resolved: [],
                                   template: "github")
  # cheating a bit - I don't want to alter the apply_template API
  # so just sneak around behind its back setting some instance variables
  # to get them to show up in the template
  @message_group = message_group
  @resolved = resolved
  request_source_name = template.sub("_message_group", "")

  apply_template(danger_id: danger_id,
                 markdowns: message_group.markdowns,
                 template: template,
                 request_source: request_source_name)
    .sub(/\A\n*/, "")
end
markdown_parser(text) click to toggle source
# File lib/danger/helpers/comments_helper.rb, line 13
def markdown_parser(text)
  Kramdown::Document.new(text, input: "GFM", smart_quotes: %w(apos apos quot quot))
end
messages_are_equivalent(m1, m2) click to toggle source

@!group Extension points Determine whether two messages are equivalent

request_source implementations are invited to override this method. This is mostly here to enable sources to detect when inlines change only in their commit hash and not in content per-se. since the link is implementation dependent so should be the comparison.

@param [Violation or Markdown] m1 @param [Violation or Markdown] m2

@return [Boolean] whether they represent the same message

# File lib/danger/helpers/comments_helper.rb, line 45
def messages_are_equivalent(m1, m2)
  m1 == m2
end
process_markdown(violation, hide_link = false) click to toggle source

@endgroup

# File lib/danger/helpers/comments_helper.rb, line 51
def process_markdown(violation, hide_link = false)
  message = violation.message
  message = "#{markdown_link_to_message(violation, hide_link)}#{message}" if violation.file && violation.line

  html = markdown_parser(message).to_html
  # Remove the outer `<p>` and `</p>`.
  html = html.strip.sub(%r{\A<p>(.*)</p>\z}m, '\1')
  Violation.new(html, violation.sticky, violation.file, violation.line)
end
random_compliment() click to toggle source
# File lib/danger/helpers/comments_helper.rb, line 164
def random_compliment
  ["Well done.", "Congrats.", "Woo!",
   "Yay.", "Jolly good show.", "Good on 'ya.", "Nice work."].sample
end
table(name, emoji, violations, all_previous_violations, template: "github") click to toggle source
# File lib/danger/helpers/comments_helper.rb, line 61
def table(name, emoji, violations, all_previous_violations, template: "github")
  content = violations
  content = content.map { |v| process_markdown(v) } unless ["bitbucket_server", "vsts"].include?(template)

  kind = table_kind_from_title(name)
  previous_violations = all_previous_violations[kind] || []
  resolved_violations = previous_violations.reject do |pv|
    content.count { |v| messages_are_equivalent(v, pv) } > 0
  end

  resolved_messages = resolved_violations.map(&:message).uniq
  count = content.count

  {
    name: name,
    emoji: emoji,
    content: content,
    resolved: resolved_messages,
    count: count
  }
end

Private Instance Methods

pluralize(string, count) click to toggle source
# File lib/danger/helpers/comments_helper.rb, line 171
def pluralize(string, count)
  string.danger_pluralize(count)
end
truncate(string) click to toggle source
# File lib/danger/helpers/comments_helper.rb, line 175
def truncate(string)
  max_message_length = 30
  string.danger_truncate(max_message_length)
end