lib/danger/commands/local.rb in danger-0.9.1 vs lib/danger/commands/local.rb in danger-0.10.0

- old
+ new

@@ -1,19 +1,26 @@ +require "danger/commands/local_helpers/http_cache" +require "faraday/http_cache" +require "octokit" +require "tmpdir" + module Danger class Local < Runner self.summary = "Run the Dangerfile locally." self.command = "local" def initialize(argv) @dangerfile_path = "Dangerfile" if File.exist? "Dangerfile" @pr_num = argv.option("use-merged-pr") + @clear_http_cache = argv.flag?("clear-http-cache", false) super end 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."] + ["--use-merged-pr=[#id]", "The ID of an already merged PR inside your history to use as a reference for the local run."], + ["--clear-http-cache", "Clear the local http cache before running Danger locally."] ].concat(super) end def validate! super @@ -24,10 +31,19 @@ def run ENV["DANGER_USE_LOCAL_GIT"] = "YES" ENV["LOCAL_GIT_PR_ID"] = @pr_num if @pr_num + # 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::Builder.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) dm = Dangerfile.new(env, cork) dm.init_plugins source = dm.env.ci_source @@ -62,13 +78,25 @@ 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)) + check_and_run_org_dangerfile(dm) + dm.print_results ensure dm.env.clean_up + end + end + + # Check to see if there's a Dangerfile in the organisation, and run it if so + def check_and_run_org_dangerfile(dm) + if dm.env.request_source.organisation && !dm.env.request_source.danger_repo? && (danger_repo = dm.env.request_source.fetch_danger_repo) + url = dm.env.request_source.file_url(repository: danger_repo.name, path: "Dangerfile") + path = dm.plugin.download(url) + dm.parse(Pathname.new(path)) end end end end