Module: Danger::Helpers::CommentsHelper

Included in:
RequestSources::GitHub, RequestSources::GitLab
Defined in:
lib/danger/helpers/comments_helper.rb

Defined Under Namespace

Classes: Comment

Instance Method Summary collapse

Instance Method Details

#generate_comment(warnings: [], errors: [], messages: [], markdowns: [], previous_violations: {}, danger_id: "danger", template: "github") ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/danger/helpers/comments_helper.rb', line 70

def generate_comment(warnings: [], errors: [], messages: [], markdowns: [], previous_violations: {}, danger_id: "danger", template: "github")
  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 = [
    table("Error", "no_entry_sign", errors, previous_violations),
    table("Warning", "warning", warnings, previous_violations),
    table("Message", "book", messages, previous_violations)
  ]
  @markdowns = markdowns
  @danger_id = danger_id

  return ERB.new(File.read(md_template), 0, "-").result(binding)
end

#generate_description(warnings: nil, errors: nil) ⇒ Object



88
89
90
91
92
93
94
95
96
97
98
# File 'lib/danger/helpers/comments_helper.rb', line 88

def generate_description(warnings: nil, errors: nil)
  if errors.empty? && warnings.empty?
    return "All green. #{random_compliment}"
  else
    message = ""
    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

#markdown_parser(text) ⇒ Object



6
7
8
# File 'lib/danger/helpers/comments_helper.rb', line 6

def markdown_parser(text)
  Kramdown::Document.new(text, input: "GFM")
end

#parse_comment(comment) ⇒ Object



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

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_tables_from_comment(comment) ⇒ Object



10
11
12
# File 'lib/danger/helpers/comments_helper.rb', line 10

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

#process_markdown(violation) ⇒ Object



19
20
21
22
23
24
25
# File 'lib/danger/helpers/comments_helper.rb', line 19

def process_markdown(violation)
  html = markdown_parser(violation.message).to_html
  # Remove the outer `<p>`, the -5 represents a newline + `</p>`
  html = html[3...-5] if html.start_with? "<p>"

  Violation.new(html, violation.sticky)
end

#random_complimentObject



100
101
102
103
104
# File 'lib/danger/helpers/comments_helper.rb', line 100

def random_compliment
  compliment = ["Well done.", "Congrats.", "Woo!",
                "Yay.", "Jolly good show.", "Good on 'ya.", "Nice work."]
  compliment.sample
end

#table(name, emoji, violations, all_previous_violations) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/danger/helpers/comments_helper.rb', line 43

def table(name, emoji, violations, all_previous_violations)
  content = violations.map { |v| process_markdown(v) }.uniq
  kind = table_kind_from_title(name)
  previous_violations = all_previous_violations[kind] || []
  messages = content.map(&:message)
  resolved_violations = previous_violations.uniq - messages
  count = content.count

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

#table_kind_from_title(title) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/danger/helpers/comments_helper.rb', line 60

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



14
15
16
17
# File 'lib/danger/helpers/comments_helper.rb', line 14

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