danger 3.3.2 → 3.4.0

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
  SHA1:
3
- metadata.gz: 423ee19a94a521cfab7f662fd5897bc0130ad848
4
- data.tar.gz: dff351383ef77ef6ed555687d51584a8b733e037
3
+ metadata.gz: 331ec016762064e8a92668e5b9b56521e5873f50
4
+ data.tar.gz: 69fb9998dbe05c5465301a4a0396a28ce4ebf1ca
5
5
  SHA512:
6
- metadata.gz: 5b44215d3633c38174e8cacc523169c76c23999f19e6b134f50be7214cc5a763d39e514e92e4118f9c332130911c8b2123f4b67cf5680ee2eee2689710a5980d
7
- data.tar.gz: fa3be34a121bbe67101086fb3ecd6d2d0598c13bc048a28bbdf14cd4da4228168c9dcb002243877df714358e27e21660a3bead61c8831e72892e89b3cc41b7fa
6
+ metadata.gz: 0abf5fed8d027d3368573054d82b87c4010bf806ef83d0d41f8b964d438e44fd35dd99f822de9ebc581c3d3d59ea6810145235a08b7ed7a20682f4ae35e80019
7
+ data.tar.gz: 2189b8616f4a18ee79d12863c5ce0db5dc803996635c5e14b6f56f66050b97207cb76198fc36cadad80399e4b3ed6c0901814f653972236d57ab4a5f37015583
@@ -46,7 +46,7 @@ module Danger
46
46
  ).call
47
47
 
48
48
  pull_request_id, sha = MergedPullRequestFinder.new(
49
- env["LOCAL_GIT_PR_ID"] || "",
49
+ env.fetch("LOCAL_GIT_PR_ID") { "" },
50
50
  run_git("log --oneline -1000000".freeze)
51
51
  ).call
52
52
 
@@ -17,20 +17,20 @@ module Danger
17
17
 
18
18
  # @return [String] "#42"
19
19
  def pull_request_ref
20
- specific_pull_request_id ? "##{specific_pull_request_id}" : "".freeze
20
+ !specific_pull_request_id.empty? ? "##{specific_pull_request_id}" : "#\\d+".freeze
21
21
  end
22
22
 
23
23
  # @return [String] Log line of format: "Merge pull request #42"
24
24
  def most_recent_merged_pull_request
25
25
  @most_recent_merged_pull_request ||= begin
26
- git_logs.lines.grep(/Merge pull request #{pull_request_ref}/)[0]
26
+ git_logs.lines.grep(/Merge pull request #{pull_request_ref} from/)[0]
27
27
  end
28
28
  end
29
29
 
30
30
  # @return [String] Log line of format: "description (#42)"
31
31
  def most_recent_squash_and_merged_pull_request
32
32
  @most_recent_squash_and_merged_pull_request ||= begin
33
- git_logs.lines.grep(/#{pull_request_ref}/)[0]
33
+ git_logs.lines.grep(/\(#{pull_request_ref}\)/)[0]
34
34
  end
35
35
  end
36
36
 
@@ -38,16 +38,45 @@ module Danger
38
38
  def merged_pull_request
39
39
  return if pull_request_ref.empty?
40
40
 
41
- if most_recent_merged_pull_request
41
+ if both_present?
42
+ pick_the_most_recent_one_from_two_matches
43
+ elsif only_merged_pull_request_present?
42
44
  most_recent_merged_pull_request
43
- elsif most_recent_squash_and_merged_pull_request
45
+ elsif only_squash_and_merged_pull_request_present?
44
46
  most_recent_squash_and_merged_pull_request
45
47
  end
46
48
  end
47
49
 
50
+ def both_present?
51
+ most_recent_merged_pull_request && most_recent_squash_and_merged_pull_request
52
+ end
53
+
54
+ def only_merged_pull_request_present?
55
+ return false if most_recent_squash_and_merged_pull_request
56
+
57
+ !most_recent_merged_pull_request.nil? && !most_recent_merged_pull_request.empty?
58
+ end
59
+
60
+ def only_squash_and_merged_pull_request_present?
61
+ return false if most_recent_merged_pull_request
62
+
63
+ !most_recent_squash_and_merged_pull_request.nil? && !most_recent_squash_and_merged_pull_request.empty?
64
+ end
65
+
66
+ def pick_the_most_recent_one_from_two_matches
67
+ merged_index = git_logs.lines.index(most_recent_merged_pull_request)
68
+ squash_and_merged_index = git_logs.lines.index(most_recent_squash_and_merged_pull_request)
69
+
70
+ if merged_index > squash_and_merged_index # smaller index is more recent
71
+ most_recent_squash_and_merged_pull_request
72
+ else
73
+ most_recent_merged_pull_request
74
+ end
75
+ end
76
+
48
77
  def check_if_any_merged_pull_request!
49
78
  if merged_pull_request.to_s.empty?
50
- if specific_pull_request_id
79
+ if !specific_pull_request_id.empty?
51
80
  raise "Could not find the Pull Request (#{specific_pull_request_id}) inside the git history for this repo."
52
81
  else
53
82
  raise "No recent Pull Requests found for this repo, danger requires at least one Pull Request for the local mode.".freeze
@@ -32,7 +32,7 @@ module Danger
32
32
  class TeamCity < CI
33
33
  class << self
34
34
  def validates_as_github_pr?(env)
35
- ["GITHUB_PULL_REQUEST_ID", "GITHUB_REPO_URL", "GITHUB_REPO_URL"].all? { |x| env[x] && !env[x].empty? }
35
+ ["GITHUB_PULL_REQUEST_ID", "GITHUB_REPO_URL"].all? { |x| env[x] && !env[x].empty? }
36
36
  end
37
37
 
38
38
  def validates_as_gitlab_pr?(env)
@@ -93,7 +93,7 @@ module Danger
93
93
  ui.pause 1
94
94
 
95
95
  if considered_an_oss_repo?
96
- ui.say "#{@bot_name} does not need privilidged access to your repo or org. This is because Danger will only"
96
+ ui.say "#{@bot_name} does not need privileged access to your repo or org. This is because Danger will only"
97
97
  ui.say "be writing comments, and you do not need special access for that."
98
98
  else
99
99
  ui.say "#{@bot_name} will need access to your repo. Simply because the code is not available for the public"
@@ -9,6 +9,14 @@ module Danger
9
9
  self.summary = "Run the Dangerfile locally."
10
10
  self.command = "local"
11
11
 
12
+ def self.options
13
+ [
14
+ ["--use-merged-pr=[#id]", "The ID of an already merged PR inside your history to use as a reference for the local run."],
15
+ ["--clear-http-cache", "Clear the local http cache before running Danger locally."],
16
+ ["--pry", "Drop into a Pry shell after evaluating the Dangerfile."]
17
+ ]
18
+ end
19
+
12
20
  def initialize(argv)
13
21
  @pr_num = argv.option("use-merged-pr")
14
22
  @clear_http_cache = argv.flag?("clear-http-cache", false)
@@ -39,14 +47,6 @@ module Danger
39
47
  abort
40
48
  end
41
49
 
42
- def self.options
43
- [
44
- ["--use-merged-pr=[#id]", "The ID of an already merged PR inside your history to use as a reference for the local run."],
45
- ["--clear-http-cache", "Clear the local http cache before running Danger locally."],
46
- ["--pry", "Drop into a Pry shell after evaluating the Dangerfile."]
47
- ].concat(super)
48
- end
49
-
50
50
  def validate!
51
51
  super
52
52
  unless @dangerfile_path
@@ -67,7 +67,7 @@ module Danger
67
67
  builder.adapter Faraday.default_adapter
68
68
  end
69
69
 
70
- env = EnvironmentManager.new(ENV)
70
+ env = EnvironmentManager.new(ENV, cork)
71
71
  dm = Dangerfile.new(env, cork)
72
72
  dm.init_plugins
73
73
 
@@ -8,10 +8,12 @@ module Danger
8
8
  require "claide_plugin"
9
9
  @subcommands << CLAide::Command::Plugins
10
10
  CLAide::Plugins.config =
11
- CLAide::Plugins::Configuration.new("Danger",
12
- "danger",
13
- "https://raw.githubusercontent.com/danger/danger.systems/master/plugins-search-generated.json",
14
- "https://github.com/danger/danger-plugin-template")
11
+ CLAide::Plugins::Configuration.new(
12
+ "Danger",
13
+ "danger",
14
+ "https://raw.githubusercontent.com/danger/danger.systems/master/plugins-search-generated.json",
15
+ "https://github.com/danger/danger-plugin-template"
16
+ )
15
17
 
16
18
  require "danger/commands/plugins/plugin_lint"
17
19
  require "danger/commands/plugins/plugin_json"
@@ -27,9 +29,10 @@ module Danger
27
29
 
28
30
  def initialize(argv)
29
31
  dangerfile = argv.option("dangerfile", "Dangerfile")
30
- @dangerfile_path = dangerfile if File.exist? dangerfile
32
+ @dangerfile_path = dangerfile if File.exist?(dangerfile)
31
33
  @base = argv.option("base")
32
34
  @head = argv.option("head")
35
+ @fail_on_errors = argv.option("fail-on-errors", false)
33
36
  @danger_id = argv.option("danger_id", "danger")
34
37
  @cork = Cork::Board.new(silent: argv.option("silent", false),
35
38
  verbose: argv.option("verbose", false))
@@ -39,7 +42,7 @@ module Danger
39
42
  def validate!
40
43
  super
41
44
  if self.class == Runner && !@dangerfile_path
42
- help! "Could not find a Dangerfile."
45
+ help!("Could not find a Dangerfile.")
43
46
  end
44
47
  end
45
48
 
@@ -47,16 +50,20 @@ module Danger
47
50
  [
48
51
  ["--base=[master|dev|stable]", "A branch/tag/commit to use as the base of the diff"],
49
52
  ["--head=[master|dev|stable]", "A branch/tag/commit to use as the head"],
53
+ ["--fail-on-errors=<true|false>", "Should always fail the build process, defaults to false"],
50
54
  ["--dangerfile=<path/to/dangerfile>", "The location of your Dangerfile"],
51
55
  ["--danger_id=<id>", "The identifier of this Danger instance"]
52
56
  ].concat(super)
53
57
  end
54
58
 
55
59
  def run
56
- Executor.new(ENV).run(base: @base,
57
- head: @head,
58
- dangerfile_path: @dangerfile_path,
59
- danger_id: @danger_id)
60
+ Executor.new(ENV).run(
61
+ base: @base,
62
+ head: @head,
63
+ dangerfile_path: @dangerfile_path,
64
+ danger_id: @danger_id,
65
+ fail_on_errors: @fail_on_errors
66
+ )
60
67
  end
61
68
  end
62
69
  end
@@ -233,6 +233,54 @@ module Danger
233
233
  end
234
234
  end
235
235
 
236
+ def failed?
237
+ violation_report[:errors].count > 0
238
+ end
239
+
240
+ def post_results(danger_id)
241
+ violations = violation_report
242
+
243
+ env.request_source.update_pull_request!(
244
+ warnings: violations[:warnings],
245
+ errors: violations[:errors],
246
+ messages: violations[:messages],
247
+ markdowns: status_report[:markdowns],
248
+ danger_id: danger_id
249
+ )
250
+ end
251
+
252
+ def setup_for_running(base_branch, head_branch)
253
+ env.ensure_danger_branches_are_setup
254
+ env.scm.diff_for_folder(".".freeze, from: base_branch, to: head_branch)
255
+ end
256
+
257
+ def run(base_branch, head_branch, dangerfile_path, danger_id)
258
+ # Setup internal state
259
+ init_plugins
260
+ env.fill_environment_vars
261
+
262
+ begin
263
+ # Sets up the git environment
264
+ setup_for_running(base_branch, head_branch)
265
+
266
+ # Parse the local Dangerfile
267
+ parse(Pathname.new(dangerfile_path))
268
+
269
+ # Push results to the API
270
+ # Pass along the details of the run to the request source
271
+ # to send back to the code review site.
272
+ post_results(danger_id)
273
+
274
+ # Print results in the terminal
275
+ print_results
276
+ ensure
277
+ # Makes sure that Danger specific git branches are cleaned
278
+ env.clean_up
279
+ end
280
+
281
+ failed?
282
+ end
283
+
236
284
  private
237
285
 
238
286
  def print_list(title, rows)
@@ -3,7 +3,7 @@ require "danger/request_sources/request_source"
3
3
 
4
4
  module Danger
5
5
  class EnvironmentManager
6
- attr_accessor :ci_source, :request_source, :scm
6
+ attr_accessor :ci_source, :request_source, :scm, :ui
7
7
 
8
8
  # Finds a Danger::CI class based on the ENV
9
9
  def self.local_ci_source(env)
@@ -15,9 +15,10 @@ module Danger
15
15
  local_ci_source(env).validates_as_pr?(env)
16
16
  end
17
17
 
18
- def initialize(env)
18
+ def initialize(env, ui)
19
19
  ci_klass = self.class.local_ci_source(env)
20
20
  self.ci_source = ci_klass.new(env)
21
+ self.ui = ui
21
22
 
22
23
  RequestSources::RequestSource.available_request_sources.each do |klass|
23
24
  next unless self.ci_source.supports?(klass)
@@ -28,7 +29,7 @@ module Danger
28
29
  self.request_source = request_source
29
30
  end
30
31
 
31
- raise "Could not find a Request Source for #{ci_klass}\nCI: #{ci_source.inspect}".red unless self.request_source
32
+ raise_error_for_no_request_source(env, ui) unless self.request_source
32
33
  self.scm = self.request_source.scm
33
34
  end
34
35
 
@@ -67,5 +68,53 @@ module Danger
67
68
  def self.danger_base_branch
68
69
  "danger_base"
69
70
  end
71
+
72
+ def raise_error_for_no_request_source(env, ui)
73
+ title, subtitle = extract_title_and_subtitle_from_source(ci_source.repo_url)
74
+ subtitle += travis_note if env["TRAVIS_SECURE_ENV_VARS"] == "true"
75
+
76
+ ui_display_no_request_source_error_message(ui, env, title, subtitle)
77
+
78
+ exit(1)
79
+ end
80
+
81
+ private
82
+
83
+ def get_repo_source(repo_url)
84
+ if repo_url =~ /github/i
85
+ RequestSources::GitHub
86
+ elsif repo_url =~ /gitlab/i
87
+ RequestSources::GitLab
88
+ elsif repo_url =~ /bitbucket\.(org|com)/i
89
+ RequestSources::BitbucketCloud
90
+ end
91
+ end
92
+
93
+ def extract_title_and_subtitle_from_source(repo_url)
94
+ source = get_repo_source(repo_url)
95
+
96
+ if source
97
+ title = "For your #{source.source_name} repo, you need to expose: " + source.env_vars.join(", ").yellow
98
+ subtitle = "You may also need: #{source.optional_env_vars.join(', ')}" if source.optional_env_vars.any?
99
+ else
100
+ title = "For Danger to run on this project, you need to expose a set of following the ENV vars:\n#{RequestSources::RequestSource.available_source_names_and_envs.join("\n")}"
101
+ end
102
+
103
+ [title, (subtitle || "")]
104
+ end
105
+
106
+ def ui_display_no_request_source_error_message(ui, env, title, subtitle)
107
+ ui.title "Could not set up API to Code Review site for Danger\n".freeze
108
+ ui.puts title
109
+ ui.puts subtitle
110
+ ui.puts "\nFound these keys in your ENV: #{env.keys.join(', '.freeze)}."
111
+ ui.puts "\nFailing the build, Danger cannot run without API access.".freeze
112
+ ui.puts "You can see more information at http://danger.systems/guides/getting_started.html".freeze
113
+ end
114
+
115
+ def travis_note
116
+ "\nTravis note: If you have an open source project, you should ensure 'Display value in build log' enabled for these flags, so that PRs from forks work." \
117
+ "\nThis also means that people can see this token, so this account should have no write access to repos."
118
+ end
70
119
  end
71
120
  end
@@ -10,61 +10,63 @@ module Danger
10
10
  base: nil,
11
11
  head: nil,
12
12
  dangerfile_path: nil,
13
- danger_id: nil)
13
+ danger_id: nil,
14
+ fail_on_errors: nil)
15
+ # Create a silent Cork instance if cork is nil, as it's likely a test
16
+ cork ||= Cork::Board.new(silent: false, verbose: false)
14
17
 
15
- cork ||= Cork::Board.new(silent: false,
16
- verbose: false)
18
+ # Run some validations
19
+ validate!(cork)
17
20
 
18
- # Could we find a CI source at all?
19
- unless EnvironmentManager.local_ci_source(@system_env)
20
- abort("Could not find the type of CI for Danger to run on.".red)
21
- end
22
-
23
- # Could we determine that the CI source is inside a PR?
24
- unless EnvironmentManager.pr?(@system_env)
25
- cork.puts "Not a Pull Request - skipping `danger` run".yellow
26
- return
27
- end
28
-
29
- # OK, then we can have some
30
- env ||= EnvironmentManager.new(@system_env)
21
+ # OK, we now know that Danger can run in this enviroment
22
+ env ||= EnvironmentManager.new(system_env, cork)
31
23
  dm ||= Dangerfile.new(env, cork)
32
24
 
33
- dm.init_plugins
25
+ ran_status = begin
26
+ dm.run(
27
+ base_branch(base),
28
+ head_branch(head),
29
+ dangerfile_path,
30
+ danger_id
31
+ )
32
+ end
34
33
 
35
- dm.env.fill_environment_vars
34
+ # By default Danger will use the status API to fail a build,
35
+ # allowing execution to continue, this behavior isn't always
36
+ # optimal for everyone.
37
+ exit(1) if fail_on_errors && ran_status
38
+ end
36
39
 
37
- begin
38
- dm.env.ensure_danger_branches_are_setup
40
+ def validate!(cork)
41
+ validate_ci!
42
+ validate_pr!(cork)
43
+ end
39
44
 
40
- # Offer the chance for a user to specify a branch through the command line
41
- ci_base = base || EnvironmentManager.danger_base_branch
42
- ci_head = head || EnvironmentManager.danger_head_branch
45
+ private
43
46
 
44
- dm.env.scm.diff_for_folder(".", from: ci_base, to: ci_head)
47
+ attr_reader :system_env
45
48
 
46
- # Parse the local Dangerfile
47
- dm.parse(Pathname.new(dangerfile_path))
49
+ # Could we find a CI source at all?
50
+ def validate_ci!
51
+ unless EnvironmentManager.local_ci_source(system_env)
52
+ abort("Could not find the type of CI for Danger to run on.".red)
53
+ end
54
+ end
48
55
 
49
- post_results(dm, danger_id)
50
- dm.print_results
51
- ensure
52
- dm.env.clean_up
56
+ # Could we determine that the CI source is inside a PR?
57
+ def validate_pr!(cork)
58
+ unless EnvironmentManager.pr?(system_env)
59
+ cork.puts "Not a Pull Request - skipping `danger` run".yellow
60
+ exit(0)
53
61
  end
54
62
  end
55
63
 
56
- def post_results(danger_file, danger_id)
57
- request_source = danger_file.env.request_source
58
- violations = danger_file.violation_report
59
- status = danger_file.status_report
64
+ def base_branch(user_specified_base_branch)
65
+ user_specified_base_branch || EnvironmentManager.danger_base_branch
66
+ end
60
67
 
61
- request_source.update_pull_request!(
62
- warnings: violations[:warnings],
63
- errors: violations[:errors],
64
- messages: violations[:messages],
65
- markdowns: status[:markdowns],
66
- danger_id: danger_id
67
- )
68
+ def head_branch(user_specified_head_branch)
69
+ user_specified_head_branch || EnvironmentManager.danger_head_branch
68
70
  end
69
71
  end
70
72
  end
@@ -197,10 +197,20 @@ module Danger
197
197
  # @return [void]
198
198
  def import_local(path)
199
199
  Dir[path].each do |file|
200
+ validate_file_contains_plugin!(file)
201
+
200
202
  # Without the expand_path it would fail if the path doesn't start with ./
201
203
  require File.expand_path(file)
202
204
  refresh_plugins
203
205
  end
204
206
  end
207
+
208
+ def validate_file_contains_plugin!(file)
209
+ content = IO.read(file)
210
+
211
+ if content.scan(/class\s+(?<plugin_class>[\w]+)\s+<\s+Plugin/i).empty?
212
+ raise("#{file} doesn't contain any valid danger plugin.")
213
+ end
214
+ end
205
215
  end
206
216
  end
@@ -28,7 +28,7 @@ module Danger
28
28
  #
29
29
  # @example Warn when somebody tries to add nokogiri to the project
30
30
  #
31
- # diff = git.diff_for_file["Gemfile.lock"]
31
+ # diff = git.diff_for_file("Gemfile.lock")
32
32
  # if diff && diff.patch =~ "nokogiri"
33
33
  # warn 'Please do not add nokogiri to the project. Thank you.'
34
34
  # end
@@ -8,6 +8,13 @@ module Danger
8
8
  include Danger::Helpers::CommentsHelper
9
9
  attr_accessor :pr_json
10
10
 
11
+ def self.env_vars
12
+ [
13
+ "DANGER_BITBUCKETCLOUD_USERNAME",
14
+ "DANGER_BITBUCKETCLOUD_PASSWORD"
15
+ ]
16
+ end
17
+
11
18
  def initialize(ci_source, environment)
12
19
  self.ci_source = ci_source
13
20
  self.environment = environment
@@ -42,10 +49,12 @@ module Danger
42
49
  head_commit = self.pr_json[:source][:commit][:hash]
43
50
 
44
51
  # Next, we want to ensure that we have a version of the current branch at a known location
52
+ scm.ensure_commitish_exists! base_commit
45
53
  self.scm.exec "branch #{EnvironmentManager.danger_base_branch} #{base_commit}"
46
54
 
47
55
  # OK, so we want to ensure that we have a known head branch, this will always represent
48
56
  # the head of the PR ( e.g. the most recent commit that will be merged. )
57
+ scm.ensure_commitish_exists! head_commit
49
58
  self.scm.exec "branch #{EnvironmentManager.danger_head_branch} #{head_commit}"
50
59
  end
51
60
 
@@ -8,6 +8,14 @@ module Danger
8
8
  include Danger::Helpers::CommentsHelper
9
9
  attr_accessor :pr_json
10
10
 
11
+ def self.env_vars
12
+ [
13
+ "DANGER_BITBUCKETSERVER_USERNAME",
14
+ "DANGER_BITBUCKETSERVER_PASSWORD",
15
+ "DANGER_BITBUCKETSERVER_HOST"
16
+ ]
17
+ end
18
+
11
19
  def initialize(ci_source, environment)
12
20
  self.ci_source = ci_source
13
21
  self.environment = environment
@@ -42,10 +50,12 @@ module Danger
42
50
  head_commit = self.pr_json[:fromRef][:latestCommit]
43
51
 
44
52
  # Next, we want to ensure that we have a version of the current branch at a known location
53
+ scm.ensure_commitish_exists! base_commit
45
54
  self.scm.exec "branch #{EnvironmentManager.danger_base_branch} #{base_commit}"
46
55
 
47
56
  # OK, so we want to ensure that we have a known head branch, this will always represent
48
57
  # the head of the PR ( e.g. the most recent commit that will be merged. )
58
+ scm.ensure_commitish_exists! head_commit
49
59
  self.scm.exec "branch #{EnvironmentManager.danger_head_branch} #{head_commit}"
50
60
  end
51
61
 
@@ -10,16 +10,20 @@ module Danger
10
10
 
11
11
  attr_accessor :pr_json, :issue_json, :support_tokenless_auth
12
12
 
13
+ def self.env_vars
14
+ ["DANGER_GITHUB_API_TOKEN"]
15
+ end
16
+
17
+ def self.optional_env_vars
18
+ ["DANGER_GITHUB_HOST", "DANGER_GITHUB_API_BASE_URL"]
19
+ end
20
+
13
21
  def initialize(ci_source, environment)
14
22
  self.ci_source = ci_source
15
23
  self.environment = environment
16
24
  self.support_tokenless_auth = false
17
25
 
18
- Octokit.auto_paginate = true
19
26
  @token = @environment["DANGER_GITHUB_API_TOKEN"]
20
- if api_url
21
- Octokit.api_endpoint = api_url
22
- end
23
27
  end
24
28
 
25
29
  def validates_as_api_source?
@@ -34,16 +38,23 @@ module Danger
34
38
  @host = @environment["DANGER_GITHUB_HOST"] || "github.com"
35
39
  end
36
40
 
41
+ # `DANGER_GITHUB_API_HOST` is the old name kept for legacy reasons and
42
+ # backwards compatibility. `DANGER_GITHUB_API_BASE_URL` is the new
43
+ # correctly named variable.
37
44
  def api_url
38
- # `DANGER_GITHUB_API_HOST` is the old name kept for legacy reasons and
39
- # backwards compatibility. `DANGER_GITHUB_API_BASE_URL` is the new
40
- # correctly named variable.
41
- @environment["DANGER_GITHUB_API_HOST"] || @environment["DANGER_GITHUB_API_BASE_URL"]
45
+ @environment.fetch("DANGER_GITHUB_API_HOST") do
46
+ @environment.fetch("DANGER_GITHUB_API_BASE_URL") do
47
+ "https://api.github.com/".freeze
48
+ end
49
+ end
42
50
  end
43
51
 
44
52
  def client
45
53
  raise "No API token given, please provide one using `DANGER_GITHUB_API_TOKEN`" if !@token && !support_tokenless_auth
46
- @client ||= Octokit::Client.new(access_token: @token)
54
+
55
+ @client ||= begin
56
+ Octokit::Client.new(access_token: @token, auto_paginate: true, api_endpoint: api_url)
57
+ end
47
58
  end
48
59
 
49
60
  def pr_diff
@@ -56,10 +67,12 @@ module Danger
56
67
  head_commit = self.pr_json["head"]["sha"]
57
68
 
58
69
  # Next, we want to ensure that we have a version of the current branch at a known location
70
+ scm.ensure_commitish_exists! base_commit
59
71
  self.scm.exec "branch #{EnvironmentManager.danger_base_branch} #{base_commit}"
60
72
 
61
73
  # OK, so we want to ensure that we have a known head branch, this will always represent
62
74
  # the head of the PR ( e.g. the most recent commit that will be merged. )
75
+ scm.ensure_commitish_exists! head_commit
63
76
  self.scm.exec "branch #{EnvironmentManager.danger_head_branch} #{head_commit}"
64
77
  end
65
78
 
@@ -9,6 +9,14 @@ module Danger
9
9
  include Danger::Helpers::CommentsHelper
10
10
  attr_accessor :mr_json, :commits_json
11
11
 
12
+ def self.env_vars
13
+ ["DANGER_GITLAB_API_TOKEN"]
14
+ end
15
+
16
+ def self.optional_env_vars
17
+ ["DANGER_GITLAB_HOST", "DANGER_GITLAB_API_BASE_URL"]
18
+ end
19
+
12
20
  def initialize(ci_source, environment)
13
21
  self.ci_source = ci_source
14
22
  self.environment = environment
@@ -43,7 +51,7 @@ module Danger
43
51
 
44
52
  def base_commit
45
53
  first_commit_in_branch = self.commits_json.last.id
46
- @base_comit ||= self.scm.exec "rev-parse #{first_commit_in_branch}^1"
54
+ @base_commit ||= self.scm.exec "rev-parse #{first_commit_in_branch}^1"
47
55
  end
48
56
 
49
57
  def mr_comments
@@ -64,10 +72,12 @@ module Danger
64
72
  head_commit = self.scm.head_commit
65
73
 
66
74
  # Next, we want to ensure that we have a version of the current branch at a known location
75
+ scm.ensure_commitish_exists! base_commit
67
76
  self.scm.exec "branch #{EnvironmentManager.danger_base_branch} #{base_commit}"
68
77
 
69
78
  # OK, so we want to ensure that we have a known head branch, this will always represent
70
79
  # the head of the PR ( e.g. the most recent commit that will be merged. )
80
+ scm.ensure_commitish_exists! head_commit
71
81
  self.scm.exec "branch #{EnvironmentManager.danger_head_branch} #{head_commit}"
72
82
  end
73
83
 
@@ -5,6 +5,14 @@ module Danger
5
5
 
6
6
  attr_accessor :ci_source, :environment, :scm, :host, :ignored_violations
7
7
 
8
+ def self.env_vars
9
+ raise "Subclass and overwrite self.env_vars"
10
+ end
11
+
12
+ def self.optional_env_vars
13
+ []
14
+ end
15
+
8
16
  def self.inherited(child_class)
9
17
  available_request_sources.add child_class
10
18
  super
@@ -14,6 +22,16 @@ module Danger
14
22
  @available_request_sources ||= Set.new
15
23
  end
16
24
 
25
+ def self.source_name
26
+ to_s.sub("Danger::RequestSources::".freeze, "".freeze)
27
+ end
28
+
29
+ def self.available_source_names_and_envs
30
+ available_request_sources.map do |klass|
31
+ " - #{klass.source_name}: #{klass.env_vars.join(', '.freeze).yellow}"
32
+ end
33
+ end
34
+
17
35
  def initialize(_ci_source, _environment)
18
36
  raise "Subclass and overwrite initialize"
19
37
  end
@@ -36,15 +36,15 @@ module Danger
36
36
  exec("remote show origin -n").lines.grep(/Fetch URL/)[0].split(": ", 2)[1].chomp
37
37
  end
38
38
 
39
+ def ensure_commitish_exists!(commitish)
40
+ exec("fetch") if exec("rev-parse --quiet --verify #{commitish}").empty?
41
+ end
42
+
39
43
  private
40
44
 
41
45
  def default_env
42
46
  { "LANG" => "en_US.UTF-8" }
43
47
  end
44
-
45
- def ensure_commitish_exists!(commitish)
46
- exec "fetch" if exec("--no-pager show #{commitish}").empty?
47
- end
48
48
  end
49
49
  end
50
50
 
@@ -1,4 +1,4 @@
1
1
  module Danger
2
- VERSION = "3.3.2".freeze
2
+ VERSION = "3.4.0".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: 3.3.2
4
+ version: 3.4.0
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: 2016-09-19 00:00:00.000000000 Z
12
+ date: 2016-09-29 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: claide