Class: Danger::PR

Inherits:
Runner
  • Object
show all
Defined in:
lib/danger/commands/pr.rb

Instance Attribute Summary

Attributes inherited from Runner

#cork

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(argv) ⇒ PR

Returns a new instance of PR.



20
21
22
23
24
25
26
27
28
29
30
# File 'lib/danger/commands/pr.rb', line 20

def initialize(argv)
  @pr_url = argv.shift_argument
  @clear_http_cache = argv.flag?("clear-http-cache", false)
  dangerfile = argv.option("dangerfile", "Dangerfile")

  super

  @dangerfile_path = dangerfile if File.exist?(dangerfile)

  setup_pry if should_pry?(argv)
end

Class Method Details

.optionsObject



12
13
14
15
16
17
18
# File 'lib/danger/commands/pr.rb', line 12

def self.options
  [
    ["--clear-http-cache", "Clear the local http cache before running Danger locally."],
    ["--pry", "Drop into a Pry shell after evaluating the Dangerfile."],
    ["--dangerfile=<path/to/dangerfile>", "The location of your Dangerfile"]
  ]
end

Instance Method Details

#runObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/danger/commands/pr.rb', line 60

def run
  ENV["DANGER_USE_LOCAL_GIT"] = "YES"
  ENV["LOCAL_GIT_PR_URL"] = @pr_url if @pr_url

  # setup caching for Github calls to hitting the API rate limit too quickly
  cache_file = File.join(ENV["DANGER_TMPDIR"] || Dir.tmpdir, "danger_local_cache")
  cache = HTTPCache.new(cache_file, clear_cache: @clear_http_cache)
  Octokit.middleware = Faraday::RackBuilder.new do |builder|
    builder.use Faraday::HttpCache, store: cache, serializer: Marshal, shared_cache: false
    builder.use Octokit::Response::RaiseError
    builder.adapter Faraday.default_adapter
  end

  env = EnvironmentManager.new(ENV, cork)
  dm = Dangerfile.new(env, cork)
  dm.init_plugins

  source = dm.env.ci_source
  if source.nil? or source.repo_slug.empty?
    cork.puts "danger pr failed because it only works with GitHub projects at the moment. Sorry.".red
    exit 0
  end

  gh = dm.env.request_source

  cork.puts "Running your Dangerfile against this PR - https://#{gh.host}/#{source.repo_slug}/pull/#{source.pull_request_id}"

  if verbose != true
    cork.puts "Turning on --verbose"
    dm.verbose = true
  end

  cork.puts

  # We can use tokenless here, as it's running on someone's computer
  # and is IP locked, as opposed to on the CI.
  gh.support_tokenless_auth = true

  begin
    gh.fetch_details
  rescue Octokit::NotFound
    cork.puts "Local repository was not found on GitHub. If you're trying to test a private repository please provide a valid API token through " + "DANGER_GITHUB_API_TOKEN".yellow + " environment variable."
    return
  end

  dm.env.request_source = gh

  begin
    dm.env.fill_environment_vars
    dm.env.ensure_danger_branches_are_setup
    dm.env.scm.diff_for_folder(".", from: Danger::EnvironmentManager.danger_base_branch, to: Danger::EnvironmentManager.danger_head_branch)

    dm.parse(Pathname.new(@dangerfile_path))
    dm.print_results
  ensure
    dm.env.clean_up
  end
end

#setup_pryObject



36
37
38
39
40
41
42
43
# File 'lib/danger/commands/pr.rb', line 36

def setup_pry
  File.delete "_Dangerfile.tmp" if File.exist? "_Dangerfile.tmp"
  FileUtils.cp @dangerfile_path, "_Dangerfile.tmp"
  File.open("_Dangerfile.tmp", "a") do |f|
    f.write("binding.pry; File.delete(\"_Dangerfile.tmp\")")
  end
  @dangerfile_path = "_Dangerfile.tmp"
end

#should_pry?(argv) ⇒ Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/danger/commands/pr.rb', line 32

def should_pry?(argv)
  argv.flag?("pry", false) && !@dangerfile_path.empty? && validate_pry_available
end

#validate!Object



53
54
55
56
57
58
# File 'lib/danger/commands/pr.rb', line 53

def validate!
  super
  unless @dangerfile_path
    help! "Could not find a Dangerfile."
  end
end

#validate_pry_availableObject



45
46
47
48
49
50
51
# File 'lib/danger/commands/pr.rb', line 45

def validate_pry_available
  require "pry"
rescue LoadError
  cork.warn "Pry was not found, and is required for 'danger pr --pry'."
  cork.print_warnings
  abort
end