danger 6.0.4 → 6.0.5

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0d07cd853986d22b15d4aed8699bd1877b506990221ccfd111dda79f004a4941
4
- data.tar.gz: a5eff46697b1974fe5356a5276ee2913532732c6de08ab88d98eaedbb0131681
3
+ metadata.gz: 4004dbb1205aad12f66f5278fcf5baa4a5e052c0a93c2c3aff2192294fcd5912
4
+ data.tar.gz: f9efeacd037a59d541a6e948acbd3041793387e93ecf52a7368f2a7c0080dac1
5
5
  SHA512:
6
- metadata.gz: f61272da6dbafad0f462d30567616125bdb31363ead8421b54c4fe302708848af0f90288a877a9213933b9db53c7eb8c11148da5300024e234983ae924de9d94
7
- data.tar.gz: eeb94d72d083a0819a30f5af3029c8dbae5cede42fb5b166775f3100230756952e5147f250abed70269a75aaa81b21172fb6b5c325356f810a5b3678fc24fef3
6
+ metadata.gz: 796525af00e43eb89f3226108effe5bb41560cc8afe6c9b19e4680066eaf09e70eb517e406225a6bc2a1961f3d1b2216df66afc1f1f137fe5ed5fa61c44421e4
7
+ data.tar.gz: 2dc38dcfd71cfacf44eccc0640ddb11295b53e0bb5d43aeb447ceeb884174fb069b4d218fd0f3590134d0395f73a3529f5f8e0575084375d6b32cd85287dba7a
@@ -2,9 +2,10 @@ module Danger
2
2
  class Comment
3
3
  attr_reader :id, :body
4
4
 
5
- def initialize(id, body)
5
+ def initialize(id, body, inline = nil)
6
6
  @id = id
7
7
  @body = body
8
+ @inline = inline
8
9
  end
9
10
 
10
11
  def self.from_github(comment)
@@ -13,9 +14,10 @@ module Danger
13
14
 
14
15
  def self.from_gitlab(comment)
15
16
  if comment.respond_to?(:id) && comment.respond_to?(:body)
16
- self.new(comment.id, comment.body)
17
+ type = comment.respond_to?(:type) ? comment.type : nil
18
+ self.new(comment.id, comment.body, type == "DiffNote")
17
19
  else
18
- self.new(comment["id"], comment["body"])
20
+ self.new(comment["id"], comment["body"], comment["type"] == "DiffNote")
19
21
  end
20
22
  end
21
23
 
@@ -24,7 +26,7 @@ module Danger
24
26
  end
25
27
 
26
28
  def inline?
27
- body.include?("")
29
+ @inline.nil? ? body.include?("") : @inline
28
30
  end
29
31
  end
30
32
  end
@@ -74,7 +74,7 @@ module Danger
74
74
  # @comments contains Comment objects (that have less information)
75
75
  @comments ||= begin
76
76
  if supports_inline_comments
77
- @raw_comments = client.merge_request_discussions(ci_source.repo_slug, ci_source.pull_request_id)
77
+ @raw_comments = mr_discussions
78
78
  .auto_paginate
79
79
  .flat_map { |discussion| discussion.notes.map { |note| note.merge({"discussion_id" => discussion.id}) } }
80
80
  @raw_comments
@@ -88,6 +88,10 @@ module Danger
88
88
  end
89
89
  end
90
90
 
91
+ def mr_discussions
92
+ @mr_discussions ||= client.merge_request_discussions(ci_source.repo_slug, ci_source.pull_request_id)
93
+ end
94
+
91
95
  def mr_diff
92
96
  @mr_diff ||= begin
93
97
  mr_changes
@@ -158,9 +162,11 @@ module Danger
158
162
  end
159
163
 
160
164
  def update_pull_request_with_inline_comments!(warnings: [], errors: [], messages: [], markdowns: [], danger_id: "danger", new_comment: false, remove_previous_comments: false)
161
- editable_comments = mr_comments.select { |comment| comment.generated_by_danger?(danger_id) }
165
+ editable_regular_comments = mr_comments
166
+ .select { |comment| comment.generated_by_danger?(danger_id) }
167
+ .reject(&:inline?)
162
168
 
163
- last_comment = editable_comments.last
169
+ last_comment = editable_regular_comments.last
164
170
  should_create_new_comment = new_comment || last_comment.nil? || remove_previous_comments
165
171
 
166
172
  previous_violations =
@@ -215,7 +221,6 @@ module Danger
215
221
  client.edit_merge_request_note(ci_source.repo_slug, ci_source.pull_request_id, last_comment.id, body)
216
222
  end
217
223
  end
218
-
219
224
  end
220
225
 
221
226
  def update_pull_request_without_inline_comments!(warnings: [], errors: [], messages: [], markdowns: [], danger_id: "danger", new_comment: false, remove_previous_comments: false)
@@ -323,9 +328,10 @@ module Danger
323
328
  end
324
329
 
325
330
  def submit_inline_comments!(warnings: [], errors: [], messages: [], markdowns: [], previous_violations: [], danger_id: "danger")
326
- comments = client.merge_request_discussions(ci_source.repo_slug, ci_source.pull_request_id)
331
+ comments = mr_discussions
327
332
  .auto_paginate
328
333
  .flat_map { |discussion| discussion.notes.map { |note| note.merge({"discussion_id" => discussion.id}) } }
334
+ .select { |comment| Comment.from_gitlab(comment).inline? }
329
335
 
330
336
  danger_comments = comments.select { |comment| Comment.from_gitlab(comment).generated_by_danger?(danger_id) }
331
337
  non_danger_comments = comments - danger_comments
@@ -343,7 +349,7 @@ module Danger
343
349
  violation = violations_from_table(comment["body"]).first
344
350
  if !violation.nil? && violation.sticky
345
351
  body = generate_inline_comment_body("white_check_mark", violation, danger_id: danger_id, resolved: true, template: "gitlab")
346
- client.update_merge_request_discussion_note(ci_source.repo_slug, ci_source.pull_request_id, comment["discussion_id"], comment["id"], body)
352
+ client.update_merge_request_discussion_note(ci_source.repo_slug, ci_source.pull_request_id, comment["discussion_id"], comment["id"], body: body)
347
353
  else
348
354
  # We remove non-sticky violations that have no replies
349
355
  # Since there's no direct concept of a reply in GH, we simply consider
@@ -400,12 +406,17 @@ module Danger
400
406
  end
401
407
 
402
408
  if matching_comments.empty?
409
+ old_position = find_old_position_in_diff mr_changes.changes, m
410
+ next false if old_position.nil?
411
+
403
412
  params = {
404
413
  body: body,
405
414
  position: {
406
415
  position_type: 'text',
407
416
  new_path: m.file,
408
417
  new_line: m.line,
418
+ old_path: old_position[:path],
419
+ old_line: old_position[:line],
409
420
  base_sha: self.mr_json.diff_refs.base_sha,
410
421
  start_sha: self.mr_json.diff_refs.start_sha,
411
422
  head_sha: self.mr_json.diff_refs.head_sha
@@ -426,11 +437,11 @@ module Danger
426
437
  # Update the comment to remove the strikethrough if present
427
438
  comment = matching_comments.first
428
439
  begin
429
- client.update_merge_request_discussion_note(ci_source.repo_slug, ci_source.pull_request_id, comment["discussion_id"], comment["id"], body)
440
+ client.update_merge_request_discussion_note(ci_source.repo_slug, ci_source.pull_request_id, comment["discussion_id"], comment["id"], body: body)
430
441
  rescue Gitlab::Error::Error => e
431
442
  message = [e, "body: #{body}"].join("\n")
432
443
  puts message
433
-
444
+
434
445
  next false
435
446
  end
436
447
  end
@@ -440,6 +451,58 @@ module Danger
440
451
  end
441
452
  end
442
453
 
454
+ def find_old_position_in_diff(changes, message)
455
+ range_header_regexp = /@@ -(?<old>[0-9]+)(,([0-9]+))? \+(?<new>[0-9]+)(,([0-9]+))? @@.*/
456
+
457
+ change = changes.find { |c| c["new_path"] == message.file }
458
+
459
+ # If there is no changes or rename only or deleted, return nil.
460
+ return nil if change.nil? || change["diff"].empty? || change["deleted_file"]
461
+
462
+ modified_position = {
463
+ path: change["old_path"],
464
+ line: nil
465
+ }
466
+
467
+ # If the file is new one, old line number must be nil.
468
+ return modified_position if change["new_file"]
469
+
470
+ current_old_line = 0
471
+ current_new_line = 0
472
+
473
+ change["diff"].each_line do |line|
474
+ match = line.match range_header_regexp
475
+
476
+ if match
477
+ # If the message line is at before next diffs, break from loop.
478
+ break if message.line.to_i < match[:new].to_i
479
+
480
+ # The match [:old] line does not appear yet at the header position, so reduce line number.
481
+ current_old_line = match[:old].to_i - 1
482
+ current_new_line = match[:new].to_i - 1
483
+ next
484
+ end
485
+
486
+ if line.start_with?("-")
487
+ current_old_line += 1
488
+ elsif line.start_with?("+")
489
+ current_new_line += 1
490
+ # If the message line starts with '+', old line number must be nil.
491
+ return modified_position if current_new_line == message.line.to_i
492
+ elsif !line.eql?("\\n")
493
+ current_old_line += 1
494
+ current_new_line += 1
495
+ # If the message line doesn't start with '+', old line number must be specified.
496
+ break if current_new_line == message.line.to_i
497
+ end
498
+ end
499
+
500
+ {
501
+ path: change["old_path"],
502
+ line: current_old_line - current_new_line + message.line.to_i
503
+ }
504
+ end
443
505
  end
444
506
  end
445
507
  end
508
+
@@ -1,4 +1,4 @@
1
1
  module Danger
2
- VERSION = "6.0.4".freeze
2
+ VERSION = "6.0.5".freeze
3
3
  DESCRIPTION = "Like Unit Tests, but for your Team Culture.".freeze
4
4
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: danger
3
3
  version: !ruby/object:Gem::Version
4
- version: 6.0.4
4
+ version: 6.0.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Orta Therox
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2019-04-15 00:00:00.000000000 Z
12
+ date: 2019-04-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: claide