Sha256: a7b0eeaeea8e63e681bd12ffd40027a19f5825017d80076779fcad02d3304e38

Contents?: true

Size: 1.89 KB

Versions: 46

Compression:

Stored size: 1.89 KB

Contents

module Danger
  class Executor
    def initialize(system_env)
      @system_env = system_env
    end

    def run(env: nil,
            dm: nil,
            cork: nil,
            base: nil,
            head: nil,
            dangerfile_path: nil,
            danger_id: nil,
            new_comment: nil,
            fail_on_errors: nil)
      # Create a silent Cork instance if cork is nil, as it's likely a test
      cork ||= Cork::Board.new(silent: false, verbose: false)

      # Run some validations
      validate!(cork)

      # OK, we now know that Danger can run in this enviroment
      env ||= EnvironmentManager.new(system_env, cork)
      dm ||= Dangerfile.new(env, cork)

      ran_status = begin
        dm.run(
          base_branch(base),
          head_branch(head),
          dangerfile_path,
          danger_id,
          new_comment
        )
      end

      # By default Danger will use the status API to fail a build,
      # allowing execution to continue, this behavior isn't always
      # optimal for everyone.
      exit(1) if fail_on_errors && ran_status
    end

    def validate!(cork)
      validate_ci!
      validate_pr!(cork)
    end

    private

    attr_reader :system_env

    # Could we find a CI source at all?
    def validate_ci!
      unless EnvironmentManager.local_ci_source(system_env)
        abort("Could not find the type of CI for Danger to run on.".red)
      end
    end

    # Could we determine that the CI source is inside a PR?
    def validate_pr!(cork)
      unless EnvironmentManager.pr?(system_env)
        cork.puts "Not a Pull Request - skipping `danger` run".yellow
        exit(0)
      end
    end

    def base_branch(user_specified_base_branch)
      user_specified_base_branch || EnvironmentManager.danger_base_branch
    end

    def head_branch(user_specified_head_branch)
      user_specified_head_branch || EnvironmentManager.danger_head_branch
    end
  end
end

Version data entries

46 entries across 46 versions & 1 rubygems

Version Path
danger-5.5.7 lib/danger/danger_core/executor.rb
danger-5.5.6 lib/danger/danger_core/executor.rb
danger-5.5.5 lib/danger/danger_core/executor.rb
danger-5.5.4 lib/danger/danger_core/executor.rb
danger-5.5.3 lib/danger/danger_core/executor.rb
danger-5.5.2 lib/danger/danger_core/executor.rb
danger-5.5.1 lib/danger/danger_core/executor.rb
danger-5.5.0 lib/danger/danger_core/executor.rb
danger-5.4.3 lib/danger/danger_core/executor.rb
danger-5.4.2 lib/danger/danger_core/executor.rb
danger-5.4.1 lib/danger/danger_core/executor.rb
danger-5.4.0 lib/danger/danger_core/executor.rb
danger-5.3.5 lib/danger/danger_core/executor.rb
danger-5.3.4 lib/danger/danger_core/executor.rb
danger-5.3.3 lib/danger/danger_core/executor.rb
danger-5.3.2 lib/danger/danger_core/executor.rb
danger-5.3.1 lib/danger/danger_core/executor.rb
danger-5.3.0 lib/danger/danger_core/executor.rb
danger-5.2.2 lib/danger/danger_core/executor.rb
danger-5.2.1 lib/danger/danger_core/executor.rb