Sha256: 031c1af0f0d6680e11d9459f257f53444ab2534064d76f3f0b4d184136de11ef

Contents?: true

Size: 1.83 KB

Versions: 7

Compression:

Stored size: 1.83 KB

Contents

require "danger/ci_source/ci_source"
require "danger/request_source/request_source"

module Danger
  class EnvironmentManager
    attr_accessor :ci_source, :request_source, :scm

    # Finds a Danger::CI class based on the ENV
    def self.local_ci_source(env)
      CI.available_ci_sources.find { |ci| ci.validates_as_ci? env }
    end

    # Uses the current Danger::CI subclass, and sees if it is a PR
    def self.pr?(env)
      local_ci_source(env).validates_as_pr?(env)
    end

    def initialize(env)
      ci_klass = self.class.local_ci_source(env)
      self.ci_source = ci_klass.new(env)

      RequestSources::RequestSource.available_request_sources.each do |klass|
        next unless self.ci_source.supports?(klass)

        request_source = klass.new(self.ci_source, ENV)
        next unless request_source.validates_as_ci?
        self.request_source = request_source
      end

      raise "Could not find a Request Source for #{ci_klass}".red unless self.request_source
      self.scm = self.request_source.scm
    end

    def pr?
      self.ci_source != nil
    end

    def fill_environment_vars
      request_source.fetch_details
    end

    def ensure_danger_branches_are_setup
      clean_up

      self.request_source.setup_danger_branches
    end

    def clean_up
      [EnvironmentManager.danger_base_branch, EnvironmentManager.danger_head_branch].each do |branch|
        scm.exec("branch -D #{branch}") unless scm.exec("rev-parse --quiet --verify #{branch}").empty?
      end
    end

    def meta_info_for_base
      scm.exec("--no-pager log #{EnvironmentManager.danger_base_branch} -n1")
    end

    def meta_info_for_head
      scm.exec("--no-pager log #{EnvironmentManager.danger_head_branch} -n1")
    end

    def self.danger_head_branch
      "danger_head"
    end

    def self.danger_base_branch
      "danger_base"
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
danger-2.1.6 lib/danger/danger_core/environment_manager.rb
danger-2.1.5 lib/danger/danger_core/environment_manager.rb
danger-2.1.4 lib/danger/danger_core/environment_manager.rb
danger-2.1.3 lib/danger/danger_core/environment_manager.rb
danger-2.1.2 lib/danger/danger_core/environment_manager.rb
danger-2.1.1 lib/danger/danger_core/environment_manager.rb
danger-2.1.0 lib/danger/danger_core/environment_manager.rb