Sha256: 56520a5d3ad77ff8d6fe2a611a2c08c13a32d592f78bd4e27248534d614f5291

Contents?: true

Size: 1.48 KB

Versions: 1

Compression:

Stored size: 1.48 KB

Contents

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

require 'grit'
require 'uri'

module Danger
  module CISource
    class LocalGitRepo < CI
      attr_accessor :base_commit, :head_commit

      def self.validates?(env)
        return !env["DANGER_USE_LOCAL_GIT"].nil?
      end

      def git
        @git ||= Grit::Git.new(".")
      end

      def run_git(command)
        binary = Grit::Git.git_binary
        git.sh "#{binary} #{command}"
      end

      def initialize(*)
        # get the remote URL
        remote = run_git "remote show origin -n | grep \"Fetch URL\" | cut -d ':' -f 2-"
        if remote
          remote_url_matches = remote.first.chomp.match(%r{github\.com(:|/)(?<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."
          end
        end

        # get the most recent PR merge
        logs = run_git "log --since='2 weeks ago' --merges --oneline | grep \"Merge pull request\" | head -n 1"
        pr_merge = logs[0].strip
        if pr_merge
          self.pull_request_id = pr_merge.match("#[0-9]*")[0].delete("#")
          sha = pr_merge.split(" ")[0]
          parents = run_git "rev-list --parents -n 1 #{sha}"
          self.base_commit = parents[0].strip.split(" ")[0]
          self.head_commit = parents[0].strip.split(" ")[1]
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
danger-0.6.0 lib/danger/ci_source/local_git_repo.rb