Sha256: 890a4eb6a4cafc163c025e3e68f555be4dc4372f8c2bc05837998e1cd66002e2

Contents?: true

Size: 1.48 KB

Versions: 4

Compression:

Stored size: 1.48 KB

Contents

# For more info see: https://github.com/schacon/ruby-git

require "git"
require "uri"
require "danger/ci_source/support/remote_finder"
require "danger/ci_source/support/merged_pull_request_finder"
require "danger/request_sources/github"

module Danger
  # ignore
  class LocalGitRepo < CI
    attr_accessor :base_commit, :head_commit

    def self.validates_as_ci?(env)
      env.key? "DANGER_USE_LOCAL_GIT"
    end

    def self.validates_as_pr?(_env)
      false
    end

    def git
      @git ||= GitRepo.new
    end

    def run_git(command)
      git.exec command
    end

    def supported_request_sources
      @supported_request_sources ||= [Danger::RequestSources::GitHub]
    end

    def print_repo_slug_warning
      puts "Danger local requires a repository hosted on GitHub.com or GitHub Enterprise.".freeze
    end

    def parents(sha)
      @parents ||= run_git("rev-list --parents -n 1 #{sha}").strip.split(" ".freeze)
    end

    def initialize(env = {})
      repo_slug = RemoteFinder.new(
        env["DANGER_GITHUB_HOST"] || "github.com".freeze,
        run_git("remote show origin -n".freeze)
      ).call

      pull_request_id, sha = MergedPullRequestFinder.new(
        env.fetch("LOCAL_GIT_PR_ID") { "" },
        run_git("log --oneline -1000000".freeze)
      ).call

      self.repo_slug = repo_slug ? repo_slug : print_repo_slug_warning
      self.pull_request_id = pull_request_id
      self.base_commit = parents(sha)[0]
      self.head_commit = parents(sha)[1]
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
danger-3.5.0 lib/danger/ci_source/local_git_repo.rb
danger-3.4.2 lib/danger/ci_source/local_git_repo.rb
danger-3.4.1 lib/danger/ci_source/local_git_repo.rb
danger-3.4.0 lib/danger/ci_source/local_git_repo.rb