Class: Danger::Local

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

Instance Attribute Summary

Attributes inherited from Runner

#cork

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Runner

#post_results, report_error

Constructor Details

#initialize(argv) ⇒ Local

Returns a new instance of Local.



6
7
8
9
10
# File 'lib/danger/commands/local.rb', line 6

def initialize(argv)
  @dangerfile_path = "Dangerfile" if File.exist? "Dangerfile"
  @pr_num = argv.option("use-merged-pr")
  super
end

Class Method Details

.optionsObject



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

def self.options
  [
    ["--use-merged-pr=[#id]", "The ID of an already merged PR inside your history to use as a reference for the local run."]
  ].concat(super)
end

Instance Method Details

#runObject



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/danger/commands/local.rb', line 25

def run
  ENV["DANGER_USE_LOCAL_GIT"] = "YES"
  ENV["LOCAL_GIT_PR_ID"] = @pr_num if @pr_num

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

  source = dm.env.ci_source
  if source.nil? or source.repo_slug.empty?
    cork.puts "danger local 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

#validate!Object



18
19
20
21
22
23
# File 'lib/danger/commands/local.rb', line 18

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