lib/danger/request_sources/github/github.rb in danger-4.2.2 vs lib/danger/request_sources/github/github.rb in danger-4.3.0
- old
+ new
@@ -10,11 +10,11 @@
module Danger
module RequestSources
class GitHub < RequestSource
include Danger::Helpers::CommentsHelper
- attr_accessor :pr_json, :issue_json, :support_tokenless_auth
+ attr_accessor :pr_json, :issue_json, :support_tokenless_auth, :dismiss_out_of_range_messages
def self.env_vars
["DANGER_GITHUB_API_TOKEN"]
end
@@ -24,10 +24,11 @@
def initialize(ci_source, environment)
self.ci_source = ci_source
self.environment = environment
self.support_tokenless_auth = false
+ self.dismiss_out_of_range_messages = false
@token = @environment["DANGER_GITHUB_API_TOKEN"]
end
def validates_as_api_source?
@@ -132,16 +133,10 @@
previous_violations = {}
else
previous_violations = parse_comment(last_comment.body)
end
- main_violations = (warnings + errors + messages + markdowns).reject(&:inline?)
- if previous_violations.empty? && main_violations.empty?
- # Just remove the comment, if there's nothing to say.
- delete_old_comments!(danger_id: danger_id)
- end
-
cmp = proc do |a, b|
next -1 unless a.file
next 1 unless b.file
next a.line <=> b.line if a.file == b.file
@@ -160,10 +155,16 @@
messages: comment_messages,
markdowns: comment_markdowns,
previous_violations: previous_violations,
danger_id: danger_id)
+ main_violations = comment_warnings + comment_errors + comment_messages + comment_markdowns
+ if previous_violations.empty? && main_violations.empty?
+ # Just remove the comment, if there's nothing to say.
+ delete_old_comments!(danger_id: danger_id)
+ end
+
# If there are still violations to show
unless main_violations.empty?
body = generate_comment(warnings: comment_warnings,
errors: comment_errors,
messages: comment_messages,
@@ -230,11 +231,11 @@
end
end
def submit_inline_comments!(warnings: [], errors: [], messages: [], markdowns: [], previous_violations: [], danger_id: "danger")
# Avoid doing any fetchs if there's no inline comments
- return if (warnings + errors + messages).select(&:inline?).empty?
+ return if (warnings + errors + messages + markdowns).select(&:inline?).empty?
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
@@ -280,12 +281,12 @@
submit_inline = proc do |m|
next false unless m.file && m.line
position = find_position_in_diff diff_lines, m
- # Keep the change if it's line is not in the diff
- next false if position.nil?
+ # 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?
# 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
@@ -330,21 +331,24 @@
messages.reject!(&submit_inline)
end
def find_position_in_diff(diff_lines, message)
range_header_regexp = /@@ -([0-9]+),([0-9]+) \+(?<start>[0-9]+)(,(?<end>[0-9]+))? @@.*/
- file_header_regexp = %r{ a/.*}
+ file_header_regexp = %r{^diff --git a/.*}
pattern = "+++ b/" + message.file + "\n"
file_start = diff_lines.index(pattern)
return nil if file_start.nil?
position = -1
file_line = nil
diff_lines.drop(file_start).each do |line|
+ # If we found the start of another file diff, we went too far
+ break if line.match file_header_regexp
+
match = line.match range_header_regexp
# 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
@@ -357,13 +361,10 @@
# the line we're looking for
position += 1
next unless match
- # If we found the start of another file diff, we went too far
- break if line.match file_header_regexp
-
range_start = match[:start].to_i
if match[:end]
range_end = match[:end].to_i + range_start
else
range_end = range_start
@@ -379,11 +380,11 @@
position unless file_line.nil?
end
# See the tests for examples of data coming in looks like
def parse_message_from_row(row)
- message_regexp = %r{(<(a |span data-)href="https://github.com/#{ci_source.repo_slug}/blob/[0-9a-z]+/(?<file>[^#]+)#L(?<line>[0-9]+)"(>[^<]*</a> - |/>))?(?<message>.*?)}im
+ message_regexp = %r{(<(a |span data-)href="https://#{host}/#{ci_source.repo_slug}/blob/[0-9a-z]+/(?<file>[^#]+)#L(?<line>[0-9]+)"(>[^<]*</a> - |/>))?(?<message>.*?)}im
match = message_regexp.match(row)
if match[:line]
line = match[:line].to_i
else
@@ -391,10 +392,10 @@
end
Violation.new(row, true, match[:file], line)
end
def markdown_link_to_message(message, hide_link)
- url = "https://github.com/#{ci_source.repo_slug}/blob/#{pr_json['head']['sha']}/#{message.file}#L#{message.line}"
+ url = "https://#{host}/#{ci_source.repo_slug}/blob/#{pr_json['head']['sha']}/#{message.file}#L#{message.line}"
if hide_link
"<span data-href=\"#{url}\"/>"
else
"[#{message.file}#L#{message.line}](#{url}) - "