Sha256: 863cdd05509b5bcdf57af843aa85d951dc4162673491adc97ad324e07592bf71

Contents?: true

Size: 1.89 KB

Versions: 11

Compression:

Stored size: 1.89 KB

Contents

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

require "git"
require "uri"

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 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 initialize(env = {})
      github_host = env["DANGER_GITHUB_HOST"] || "github.com"

      # get the remote URL
      remote = run_git("remote show origin -n").lines.grep(/Fetch URL/)[0].split(": ", 2)[1]
      if remote
        remote_url_matches = remote.match(%r{#{Regexp.escape github_host}(:|/)(?<repo_slug>.+/.+?)(?:\.git)?$})
        if !remote_url_matches.nil? and remote_url_matches["repo_slug"]
          self.repo_slug = remote_url_matches["repo_slug"]
        else
          puts "Danger local requires a repository hosted on GitHub.com or GitHub Enterprise."
        end
      end

      specific_pr = env["LOCAL_GIT_PR_ID"]
      pr_ref = specific_pr ? "##{specific_pr}" : ""
      pr_command = "log --merges --oneline"

      # get the most recent PR merge
      pr_merge = run_git(pr_command.strip).lines.grep(Regexp.new("Merge pull request " + pr_ref))[0]

      if pr_merge.to_s.empty?
        if specific_pr
          raise "Could not find the pull request (#{specific_pr}) inside the git history for this repo."
        else
          raise "No recent pull requests found for this repo, danger requires at least one PR for the local mode."
        end
      end

      self.pull_request_id = pr_merge.match("#([0-9]+)")[1]
      sha = pr_merge.split(" ")[0]
      parents = run_git("rev-list --parents -n 1 #{sha}").strip.split(" ")
      self.base_commit = parents[0]
      self.head_commit = parents[1]
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
danger-3.0.3 lib/danger/ci_source/local_git_repo.rb
danger-3.0.2 lib/danger/ci_source/local_git_repo.rb
danger-3.0.1 lib/danger/ci_source/local_git_repo.rb
danger-3.0.0 lib/danger/ci_source/local_git_repo.rb
danger-2.1.6 lib/danger/ci_source/local_git_repo.rb
danger-2.1.5 lib/danger/ci_source/local_git_repo.rb
danger-2.1.4 lib/danger/ci_source/local_git_repo.rb
danger-2.1.3 lib/danger/ci_source/local_git_repo.rb
danger-2.1.2 lib/danger/ci_source/local_git_repo.rb
danger-2.1.1 lib/danger/ci_source/local_git_repo.rb
danger-2.1.0 lib/danger/ci_source/local_git_repo.rb