Module: Danger::Helpers::CommentsHelper
- Includes:
- CommentsParsingHelper
- Included in:
- RequestSources::BitbucketCloud, RequestSources::BitbucketServer, RequestSources::GitHub, RequestSources::GitLab
- Defined in:
- lib/danger/helpers/comments_helper.rb
Instance Method Summary collapse
- #apply_template(tables: [], markdowns: [], danger_id: "danger", template: "github") ⇒ Object
- #character_from_emoji(emoji) ⇒ Object
- #generate_comment(warnings: [], errors: [], messages: [], markdowns: [], previous_violations: {}, danger_id: "danger", template: "github") ⇒ Object
- #generate_description(warnings: nil, errors: nil) ⇒ Object
- #generate_inline_comment_body(emoji, message, danger_id: "danger", resolved: false, template: "github") ⇒ Object
- #generate_inline_markdown_body(markdown, danger_id: "danger", template: "github") ⇒ Object
-
#markdown_link_to_message(message, _) ⇒ String
!@group Extension points Produces a markdown link to the file the message points to.
- #markdown_parser(text) ⇒ Object
-
#messages_are_equivalent(m1, m2) ⇒ Boolean
!@group Extension points Determine whether two messages are equivalent.
- #parse_comment(comment) ⇒ Object
- #process_markdown(violation, hide_link = false) ⇒ Object
- #random_compliment ⇒ Object
- #table(name, emoji, violations, all_previous_violations) ⇒ Object
Methods included from CommentsParsingHelper
#parse_message_from_row, #parse_tables_from_comment, #table_kind_from_title, #violations_from_table
Instance Method Details
#apply_template(tables: [], markdowns: [], danger_id: "danger", template: "github") ⇒ Object
94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/danger/helpers/comments_helper.rb', line 94 def apply_template(tables: [], markdowns: [], 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 = tables @markdowns = markdowns.map(&:message) @danger_id = danger_id return ERB.new(File.read(md_template), 0, "-").result(binding) end |
#character_from_emoji(emoji) ⇒ Object
154 155 156 157 158 159 160 161 162 163 164 165 |
# File 'lib/danger/helpers/comments_helper.rb', line 154 def character_from_emoji(emoji) emoji.delete! ":" if emoji == "no_entry_sign" "🚫" elsif emoji == "warning" "⚠️" elsif emoji == "book" "📖" elsif emoji == "white_check_mark" "✅" end end |
#generate_comment(warnings: [], errors: [], messages: [], markdowns: [], previous_violations: {}, danger_id: "danger", template: "github") ⇒ Object
108 109 110 111 112 113 114 115 116 117 118 119 |
# File 'lib/danger/helpers/comments_helper.rb', line 108 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), table("Warning", "warning", warnings, previous_violations), table("Message", "book", , previous_violations) ], markdowns: markdowns, danger_id: danger_id, template: template ) end |
#generate_description(warnings: nil, errors: nil) ⇒ Object
137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/danger/helpers/comments_helper.rb', line 137 def generate_description(warnings: nil, errors: nil) if errors.empty? && warnings.empty? return "All green. #{random_compliment}" else = "⚠ " += "#{'Error'.danger_pluralize(errors.count)}. " unless errors.empty? += "#{'Warning'.danger_pluralize(warnings.count)}. " unless warnings.empty? += "Don't worry, everything is fixable." return end end |
#generate_inline_comment_body(emoji, message, danger_id: "danger", resolved: false, template: "github") ⇒ Object
121 122 123 124 125 126 127 |
# File 'lib/danger/helpers/comments_helper.rb', line 121 def generate_inline_comment_body(emoji, , danger_id: "danger", resolved: false, template: "github") apply_template( tables: [{ content: [], resolved: resolved, emoji: emoji }], danger_id: danger_id, template: "#{template}_inline" ) end |
#generate_inline_markdown_body(markdown, danger_id: "danger", template: "github") ⇒ Object
129 130 131 132 133 134 135 |
# File 'lib/danger/helpers/comments_helper.rb', line 129 def generate_inline_markdown_body(markdown, danger_id: "danger", template: "github") apply_template( markdowns: [markdown], danger_id: danger_id, template: "#{template}_inline" ) end |
#markdown_link_to_message(message, _) ⇒ String
!@group Extension points Produces a markdown link to the file the message points to
request_source implementations are invited to override this method with their vendor specific link.
27 28 29 |
# File 'lib/danger/helpers/comments_helper.rb', line 27 def (, _) "#{.file}#L#{.line}" end |
#markdown_parser(text) ⇒ Object
13 14 15 |
# File 'lib/danger/helpers/comments_helper.rb', line 13 def markdown_parser(text) Kramdown::Document.new(text, input: "GFM") end |
#messages_are_equivalent(m1, m2) ⇒ Boolean
!@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 dependant so should be the comparision.
43 44 45 |
# File 'lib/danger/helpers/comments_helper.rb', line 43 def (m1, m2) m1 == m2 end |
#parse_comment(comment) ⇒ Object
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 |
# File 'lib/danger/helpers/comments_helper.rb', line 57 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 |
#process_markdown(violation, hide_link = false) ⇒ Object
47 48 49 50 51 52 53 54 55 |
# File 'lib/danger/helpers/comments_helper.rb', line 47 def process_markdown(violation, hide_link = false) = violation. = "#{(violation, hide_link)}#{}" if violation.file && violation.line html = markdown_parser().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, violation.file, violation.line) end |
#random_compliment ⇒ Object
149 150 151 152 |
# File 'lib/danger/helpers/comments_helper.rb', line 149 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) ⇒ Object
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
# File 'lib/danger/helpers/comments_helper.rb', line 73 def table(name, emoji, violations, all_previous_violations) content = violations.map { |v| process_markdown(v) } kind = table_kind_from_title(name) previous_violations = all_previous_violations[kind] || [] resolved_violations = previous_violations.reject do |pv| content.count { |v| (v, pv) } > 0 end = resolved_violations.map(&:message).uniq count = content.count { name: name, emoji: emoji, content: content, resolved: , count: count } end |