danger 8.0.1 → 8.0.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5a19704ff72b8d86ac14c19bed3ccd7276c0e0c724200439b5d3a39625409c09
4
- data.tar.gz: a4b77b3e810f26f237b2057ca4214f21b6108630036aeb61228d3f2ae2e4f163
3
+ metadata.gz: 3b175fad0150fdcb243c42f37c0b9683a7dc00db8b3be4ef81d01be0103b90bb
4
+ data.tar.gz: d422bf7eadd533e5591f1ac43cde404f38d124a3c398469a1b9563c5338e8370
5
5
  SHA512:
6
- metadata.gz: c4e2eb7edc403b2ef01c2d84cf2dabacdd79426a903084e9a22d8d3506bf0519721eebfe18cff7415d83ffce992b8cb7a416661d8c2da6880dbe0f2871d19c2a
7
- data.tar.gz: 077f053cadf83b885c866329512426e0d8d7e31c6259b59c3e726532d3bddf28dca78c4aa2d760f20bed13ab7f116e4f736a276c7ab5c4793123572229fe4b0d
6
+ metadata.gz: d36a05ed7713cf0b50936c04feaa4cb49c8ba035b539ff265c9d7e231c2670efba007f5f4a2bf58936cab24c7aa0e09d5c9a2a9edff9714689fc5b03cc31d870
7
+ data.tar.gz: 611a647c06bab7c3949ded7ff12b5498b966d54f10056481ae720dfa3801f48ac27085d112f69c186feb5638b2b82b0f7583c0f020af33af8251705429fbf614
@@ -50,16 +50,29 @@ module Danger
50
50
  self.repo_url = env["GIT_REPOSITORY_URL"]
51
51
 
52
52
  matcher_url = self.repo_url
53
+ self.repo_slug = repo_slug_from(self.repo_url)
54
+ end
53
55
 
54
- #If the URL contains https:// as :// leads to inaccurate matching. So we remove it and proceed to match.
55
- if repo_url.include? "https://"
56
- matcher_url["https://"] = ''
56
+ def repo_slug_from(url)
57
+ if url =~ URI::regexp
58
+ # Try to parse the URL as a valid URI. This should cover the cases of http/https/ssh URLs.
59
+ begin
60
+ uri = URI.parse(url)
61
+ return uri.path.sub(/^(\/)/,'').sub(/(.git)$/,'')
62
+ rescue URI::InvalidURIError
63
+ # In case URL could not be parsed fallback to git URL parsing.
64
+ repo_slug_asgiturl(url)
65
+ end
66
+ else
67
+ # In case URL could not be parsed fallback to git URL parsing. git@github.com:organization/repo.git
68
+ repo_slug_asgiturl(url)
57
69
  end
70
+ end
58
71
 
72
+ def repo_slug_asgiturl(url)
73
+ matcher_url = url
59
74
  repo_matches = matcher_url.match(%r{([\/:])(([^\/]+\/)+[^\/]+?)(\.git$|$)})[2]
60
-
61
- self.repo_slug = repo_matches unless repo_matches.nil?
62
-
75
+ return repo_matches unless repo_matches.nil?
63
76
  end
64
77
  end
65
78
  end
@@ -1,5 +1,6 @@
1
1
  # http://docs.gitlab.com/ce/ci/variables/README.html
2
2
  require "uri"
3
+ require "danger/request_sources/github/github"
3
4
  require "danger/request_sources/gitlab"
4
5
 
5
6
  module Danger
@@ -15,10 +16,13 @@ module Danger
15
16
  # ```
16
17
  # ### Token Setup
17
18
  #
18
- # Add the `DANGER_GITLAB_API_TOKEN` to your pipeline env variables.
19
- class GitLabCI < CI
20
- attr_reader :project_url
19
+ # Add the `DANGER_GITLAB_API_TOKEN` to your pipeline env variables if you
20
+ # are hosting your code on GitLab. If you are using GitLab as a mirror
21
+ # for the purpose of CI/CD, while hosting your repo on GitHub, set the
22
+ # `DANGER_GITHUB_API_TOKEN` as well as the project repo URL to
23
+ # `DANGER_PROJECT_REPO_URL`.
21
24
 
25
+ class GitLabCI < CI
22
26
  def self.validates_as_ci?(env)
23
27
  env.key? "GITLAB_CI"
24
28
  end
@@ -28,11 +32,12 @@ module Danger
28
32
  "GITLAB_CI", "CI_PROJECT_PATH"
29
33
  ].all? { |x| env[x] }
30
34
 
31
- exists && determine_merge_request_id(env).to_i > 0
35
+ exists && determine_pull_or_merge_request_id(env).to_i > 0
32
36
  end
33
37
 
34
- def self.determine_merge_request_id(env)
38
+ def self.determine_pull_or_merge_request_id(env)
35
39
  return env["CI_MERGE_REQUEST_IID"] if env["CI_MERGE_REQUEST_IID"]
40
+ return env["CI_EXTERNAL_PULL_REQUEST_IID"] if env["CI_EXTERNAL_PULL_REQUEST_IID"]
36
41
  return 0 unless env["CI_COMMIT_SHA"]
37
42
 
38
43
  project_path = env["CI_MERGE_REQUEST_PROJECT_PATH"] || env["CI_PROJECT_PATH"]
@@ -54,16 +59,28 @@ module Danger
54
59
 
55
60
  def initialize(env)
56
61
  @env = env
57
- @repo_slug = env["CI_MERGE_REQUEST_PROJECT_PATH"] || env["CI_PROJECT_PATH"]
58
- @project_url = env["CI_MERGE_REQUEST_PROJECT_URL"] || env["CI_PROJECT_URL"]
62
+ @repo_slug = slug_from(env)
59
63
  end
60
64
 
61
65
  def supported_request_sources
62
- @supported_request_sources ||= [Danger::RequestSources::GitLab]
66
+ @supported_request_sources ||= [
67
+ Danger::RequestSources::GitHub,
68
+ Danger::RequestSources::GitLab
69
+ ]
63
70
  end
64
71
 
65
72
  def pull_request_id
66
- @pull_request_id ||= self.class.determine_merge_request_id(@env)
73
+ @pull_request_id ||= self.class.determine_pull_or_merge_request_id(@env)
74
+ end
75
+
76
+ private
77
+
78
+ def slug_from(env)
79
+ if env["DANGER_PROJECT_REPO_URL"]
80
+ env["DANGER_PROJECT_REPO_URL"].split('/').last(2).join('/')
81
+ else
82
+ env["CI_MERGE_REQUEST_PROJECT_PATH"] || env["CI_PROJECT_PATH"]
83
+ end
67
84
  end
68
85
  end
69
86
  end
@@ -27,6 +27,8 @@ module Danger
27
27
  # branch="%teamcity.build.branch%"
28
28
  # export GITHUB_PULL_REQUEST_ID=(${branch//\// })
29
29
  # ```
30
+ # Or if you are using the pull request feature you can set an environment parameter called `GITHUB_PULL_REQUEST_ID`
31
+ # to the value of: `%teamcity.pullRequest.number`
30
32
  #
31
33
  # #### GitLab
32
34
  #
@@ -9,7 +9,7 @@ module Danger
9
9
  def include?(pattern)
10
10
  self.each do |current|
11
11
  unless current.nil?
12
- return true if File.fnmatch(pattern, current) || pattern == current
12
+ return true if File.fnmatch(pattern, current, File::FNM_EXTGLOB) || pattern == current
13
13
  end
14
14
  end
15
15
  return false
@@ -227,6 +227,23 @@ module Danger
227
227
  paths.first(paths.count - 1).join(", ") + " & " + paths.last
228
228
  end
229
229
 
230
+ # @!group Gitlab Misc
231
+ # Use to ignore inline messages which lay outside a diff's range, thereby not posting the comment.
232
+ # You can set hash to change behavior per each kinds. (ex. `{warning: true, error: false}`)
233
+ # @param [Bool] or [Hash<Symbol, Bool>] dismiss
234
+ # Ignore out of range inline messages, defaults to `true`
235
+ #
236
+ # @return [void]
237
+ def dismiss_out_of_range_messages(dismiss = true)
238
+ if dismiss.kind_of?(Hash)
239
+ @gitlab.dismiss_out_of_range_messages = dismiss
240
+ elsif dismiss.kind_of?(TrueClass)
241
+ @gitlab.dismiss_out_of_range_messages = true
242
+ elsif dismiss.kind_of?(FalseClass)
243
+ @gitlab.dismiss_out_of_range_messages = false
244
+ end
245
+ end
246
+
230
247
  %i(title body author labels json diff).each do |suffix|
231
248
  alias_method "pr_#{suffix}".to_sym, "mr_#{suffix}".to_sym
232
249
  end
@@ -11,7 +11,7 @@ module Danger
11
11
  include Danger::Helpers::CommentsParsingHelper
12
12
 
13
13
  def markdown_parser(text)
14
- Kramdown::Document.new(text, input: "GFM")
14
+ Kramdown::Document.new(text, input: "GFM", smart_quotes: %w[apos apos quot quot])
15
15
  end
16
16
 
17
17
  # !@group Extension points
@@ -49,8 +49,8 @@ module Danger
49
49
  message = "#{markdown_link_to_message(violation, hide_link)}#{message}" if violation.file && violation.line
50
50
 
51
51
  html = markdown_parser(message).to_html
52
- # Remove the outer `<p>`, the -5 represents a newline + `</p>`
53
- html = html[3...-5] if html.start_with? "<p>"
52
+ # Remove the outer `<p>` and `</p>`.
53
+ html = html.strip.sub(%r{\A<p>(.*)</p>\z}m, '\1')
54
54
  Violation.new(html, violation.sticky, violation.file, violation.line)
55
55
  end
56
56
 
@@ -40,6 +40,10 @@ module Danger
40
40
  end
41
41
  end
42
42
 
43
+ def validates_as_ci?
44
+ true
45
+ end
46
+
43
47
  def validates_as_api_source?
44
48
  (@token && !@token.empty?) || self.environment["DANGER_USE_LOCAL_GIT"]
45
49
  end
@@ -8,7 +8,7 @@ module Danger
8
8
  module RequestSources
9
9
  class GitLab < RequestSource
10
10
  include Danger::Helpers::CommentsHelper
11
- attr_accessor :mr_json, :commits_json
11
+ attr_accessor :mr_json, :commits_json, :dismiss_out_of_range_messages
12
12
 
13
13
  FIRST_GITLAB_GEM_WITH_VERSION_CHECK = Gem::Version.new("4.6.0")
14
14
  FIRST_VERSION_WITH_INLINE_COMMENTS = Gem::Version.new("10.8.0")
@@ -24,6 +24,7 @@ module Danger
24
24
  def initialize(ci_source, environment)
25
25
  self.ci_source = ci_source
26
26
  self.environment = environment
27
+ self.dismiss_out_of_range_messages = false
27
28
 
28
29
  @token = @environment["DANGER_GITLAB_API_TOKEN"]
29
30
  end
@@ -208,7 +209,7 @@ module Danger
208
209
  rest_inline_violations = submit_inline_comments!({
209
210
  danger_id: danger_id,
210
211
  previous_violations: previous_violations
211
- }.merge(inline_violations))
212
+ }.merge(**inline_violations))
212
213
 
213
214
  main_violations = merge_violations(
214
215
  regular_violations, rest_inline_violations
@@ -227,7 +228,7 @@ module Danger
227
228
  template: "gitlab",
228
229
  danger_id: danger_id,
229
230
  previous_violations: previous_violations
230
- }.merge(main_violations))
231
+ }.merge(**main_violations))
231
232
 
232
233
  comment_result =
233
234
  if should_create_new_comment
@@ -302,6 +303,16 @@ module Danger
302
303
  nil # TODO: Implement this
303
304
  end
304
305
 
306
+ def dismiss_out_of_range_messages_for(kind)
307
+ if self.dismiss_out_of_range_messages.kind_of?(Hash) && self.dismiss_out_of_range_messages[kind]
308
+ self.dismiss_out_of_range_messages[kind]
309
+ elsif self.dismiss_out_of_range_messages == true
310
+ self.dismiss_out_of_range_messages
311
+ else
312
+ false
313
+ end
314
+ end
315
+
305
316
  # @return [String] A URL to the specific file, ready to be downloaded
306
317
  def file_url(organisation: nil, repository: nil, branch: nil, path: nil)
307
318
  branch ||= 'master'
@@ -397,10 +408,9 @@ module Danger
397
408
 
398
409
  messages.reject do |m|
399
410
  next false unless m.file && m.line
400
-
401
- # Keep the change it's in a file changed in this diff
402
- next if !mr_changed_paths.include?(m.file)
403
-
411
+ # Reject if it's out of range and in dismiss mode
412
+ next true if dismiss_out_of_range_messages_for(kind) && is_out_of_range(mr_changes.changes, m)
413
+
404
414
  # Once we know we're gonna submit it, we format it
405
415
  if is_markdown_content
406
416
  body = generate_inline_markdown_body(m, danger_id: danger_id, template: "gitlab")
@@ -473,7 +483,6 @@ module Danger
473
483
  range_header_regexp = /@@ -(?<old>[0-9]+)(,([0-9]+))? \+(?<new>[0-9]+)(,([0-9]+))? @@.*/
474
484
 
475
485
  change = changes.find { |c| c["new_path"] == message.file }
476
-
477
486
  # If there is no changes or rename only or deleted, return nil.
478
487
  return nil if change.nil? || change["diff"].empty? || change["deleted_file"]
479
488
 
@@ -520,6 +529,41 @@ module Danger
520
529
  line: current_old_line - current_new_line + message.line.to_i
521
530
  }
522
531
  end
532
+
533
+ def is_out_of_range(changes, message)
534
+ change = changes.find { |c| c["new_path"] == message.file }
535
+ # If there is no changes or rename only or deleted, return out of range.
536
+ return true if change.nil? || change["diff"].empty? || change["deleted_file"]
537
+
538
+ # If new file then return in range
539
+ return false if change["new_file"]
540
+
541
+ addition_lines = generate_addition_lines(change["diff"])
542
+ return false if addition_lines.include?(message.line.to_i)
543
+
544
+ return true
545
+ end
546
+
547
+ def generate_addition_lines(diff)
548
+ range_header_regexp = /@@ -(?<old>[0-9]+)(,([0-9]+))? \+(?<new>[0-9]+)(,([0-9]+))? @@.*/
549
+ addition_lines = []
550
+ line_number = 0
551
+ diff.each_line do |line|
552
+ if line.match range_header_regexp
553
+ line = line.split('+').last
554
+ line = line.split(' ').first
555
+ range_string = line.split(',')
556
+ line_number = range_string[0].to_i - 1
557
+ elsif line.start_with?('+')
558
+ addition_lines.push(line_number)
559
+ elsif line.start_with?('-')
560
+ line_number=line_number-1
561
+ end
562
+ line_number=line_number+1
563
+ end
564
+ addition_lines
565
+ end
566
+
523
567
  end
524
568
  end
525
569
  end
@@ -135,7 +135,7 @@ module Danger
135
135
  git_in_depth_fetch
136
136
  possible_merge_base = possible_merge_base(repo, from, to)
137
137
 
138
- raise "Cannot find a merge base between #{from} and #{to}." unless possible_merge_base
138
+ raise "Cannot find a merge base between #{from} and #{to}. If you are using shallow clone/fetch, try increasing the --depth" unless possible_merge_base
139
139
 
140
140
  possible_merge_base
141
141
  end
@@ -1,4 +1,4 @@
1
1
  module Danger
2
- VERSION = "8.0.1".freeze
2
+ VERSION = "8.0.6".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: 8.0.1
4
+ version: 8.0.6
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: 2020-06-03 00:00:00.000000000 Z
12
+ date: 2020-10-05 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: claide
@@ -107,14 +107,14 @@ dependencies:
107
107
  requirements:
108
108
  - - "~>"
109
109
  - !ruby/object:Gem::Version
110
- version: '2.0'
110
+ version: '2.3'
111
111
  type: :runtime
112
112
  prerelease: false
113
113
  version_requirements: !ruby/object:Gem::Requirement
114
114
  requirements:
115
115
  - - "~>"
116
116
  - !ruby/object:Gem::Version
117
- version: '2.0'
117
+ version: '2.3'
118
118
  - !ruby/object:Gem::Dependency
119
119
  name: kramdown-parser-gfm
120
120
  requirement: !ruby/object:Gem::Requirement