lib/danger/request_sources/github/github.rb in danger-5.2.2 vs lib/danger/request_sources/github/github.rb in danger-5.3.0
- old
+ new
@@ -243,22 +243,14 @@
diff_lines = self.pr_diff.lines
pr_comments = client.pull_request_comments(ci_source.repo_slug, ci_source.pull_request_id)
danger_comments = pr_comments.select { |comment| Comment.from_github(comment).generated_by_danger?(danger_id) }
non_danger_comments = pr_comments - danger_comments
- warnings = submit_inline_comments_for_kind!(
- "warning", warnings, diff_lines, danger_comments, previous_violations["warning"], danger_id: danger_id
- )
- errors = submit_inline_comments_for_kind!(
- "no_entry_sign", errors, diff_lines, danger_comments, previous_violations["error"], danger_id: danger_id
- )
- messages = submit_inline_comments_for_kind!(
- "book", messages, diff_lines, danger_comments, previous_violations["message"], danger_id: danger_id
- )
- markdowns = submit_inline_comments_for_kind!(
- nil, markdowns, diff_lines, danger_comments, [], danger_id: danger_id
- )
+ warnings = submit_inline_comments_for_kind!(:warning, warnings, diff_lines, danger_comments, previous_violations["warning"], danger_id: danger_id)
+ errors = submit_inline_comments_for_kind!(:error, errors, diff_lines, danger_comments, previous_violations["error"], danger_id: danger_id)
+ messages = submit_inline_comments_for_kind!(:message, messages, diff_lines, danger_comments, previous_violations["message"], danger_id: danger_id)
+ markdowns = submit_inline_comments_for_kind!(:markdown, markdowns, diff_lines, danger_comments, [], danger_id: danger_id)
# submit removes from the array all comments that are still in force
# so we strike out all remaining ones
danger_comments.each do |comment|
violation = violations_from_table(comment["body"]).first
@@ -291,22 +283,23 @@
blob_regexp = %r{blob/[0-9a-z]+/}
m1.file == m2.file && m1.line == m2.line &&
m1.message.sub(blob_regexp, "") == m2.message.sub(blob_regexp, "")
end
- def submit_inline_comments_for_kind!(emoji, messages, diff_lines, danger_comments, previous_violations, danger_id: "danger")
+ def submit_inline_comments_for_kind!(kind, messages, diff_lines, danger_comments, previous_violations, danger_id: "danger")
head_ref = pr_json["head"]["sha"]
previous_violations ||= []
- is_markdown_content = emoji.nil?
+ is_markdown_content = kind == :markdown
+ emoji = { warning: "warning", error: "no_entry_sign", message: "book" }[kind]
messages.reject do |m|
next false unless m.file && m.line
- position = find_position_in_diff diff_lines, m
+ position = find_position_in_diff diff_lines, m, kind
# Keep the change if it's line is not in the diff and not in dismiss mode
- next self.dismiss_out_of_range_messages if position.nil?
+ next dismiss_out_of_range_messages_for(kind) if position.nil?
# Once we know we're gonna submit it, we format it
if is_markdown_content
body = generate_inline_markdown_body(m, danger_id: danger_id, template: "github")
else
@@ -347,11 +340,11 @@
# Remove this element from the array
next true
end
end
- def find_position_in_diff(diff_lines, message)
+ def find_position_in_diff(diff_lines, message, kind)
range_header_regexp = /@@ -([0-9]+),([0-9]+) \+(?<start>[0-9]+)(,(?<end>[0-9]+))? @@.*/
file_header_regexp = %r{^diff --git a/.*}
pattern = "+++ b/" + message.file + "\n"
file_start = diff_lines.index(pattern)
@@ -370,11 +363,11 @@
# file_line is set once we find the hunk the line is in
# we need to count how many lines in new file we have
# so we do it one by one ignoring the deleted lines
if !file_line.nil? && !line.start_with?("-")
if file_line == message.line
- file_line = nil if dismiss_out_of_range_messages && !line.start_with?("+")
+ file_line = nil if dismiss_out_of_range_messages_for(kind) && !line.start_with?("+")
break
end
file_line += 1
end
@@ -428,9 +421,19 @@
def organisation
matched = self.issue_json["repository_url"].match(%r{repos\/(.*)\/})
return matched[1] if matched && matched[1]
rescue
nil
+ end
+
+ def dismiss_out_of_range_messages_for(kind)
+ if self.dismiss_out_of_range_messages.kind_of?(Hash) && self.dismiss_out_of_range_messages[kind]
+ self.dismiss_out_of_range_messages[kind]
+ elsif self.dismiss_out_of_range_messages == true
+ self.dismiss_out_of_range_messages
+ else
+ false
+ end
end
# @return [String] A URL to the specific file, ready to be downloaded
def file_url(organisation: nil, repository: nil, branch: nil, path: nil)
organisation ||= self.organisation