Sha256: c3b00d38438e00cd30ff4b4f0695459b596cf748088014fb7db672d77d23d6ee

Contents?: true

Size: 1.97 KB

Versions: 5

Compression:

Stored size: 1.97 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,
            remove_previous_comments: 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,
          remove_previous_comments
        )
      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

5 entries across 5 versions & 1 rubygems

Version Path
danger-5.5.12 lib/danger/danger_core/executor.rb
danger-5.5.11 lib/danger/danger_core/executor.rb
danger-5.5.10 lib/danger/danger_core/executor.rb
danger-5.5.9 lib/danger/danger_core/executor.rb
danger-5.5.8 lib/danger/danger_core/executor.rb