Class: Danger::CISource::LocalGitRepo

Inherits:
CI
  • Object
show all
Defined in:
lib/danger/ci_source/local_git_repo.rb

Instance Attribute Summary collapse

Attributes inherited from CI

#pull_request_id, #repo_slug

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(env = {}) ⇒ LocalGitRepo

Returns a new instance of LocalGitRepo.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/danger/ci_source/local_git_repo.rb', line 23

def initialize(env = {})
  github_host = env["DANGER_GITHUB_HOST"] || "github.com"

  # get the remote URL
  remote = run_git "remote show origin -n | grep \"Fetch URL\" | cut -d ':' -f 2-"
  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

  # get the most recent PR merge
  pr_merge = run_git "log --since='2 weeks ago' --merges --oneline | grep \"Merge pull request\" | head -n 1".strip
  if pr_merge.to_s.empty?
    raise "No recent pull requests found for this repo, danger requires at least one PR for the local mode"
  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

Instance Attribute Details

#base_commitObject

Returns the value of attribute base_commit.



9
10
11
# File 'lib/danger/ci_source/local_git_repo.rb', line 9

def base_commit
  @base_commit
end

#head_commitObject

Returns the value of attribute head_commit.



9
10
11
# File 'lib/danger/ci_source/local_git_repo.rb', line 9

def head_commit
  @head_commit
end

Class Method Details

.validates?(env) ⇒ Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/danger/ci_source/local_git_repo.rb', line 11

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

Instance Method Details

#gitObject



15
16
17
# File 'lib/danger/ci_source/local_git_repo.rb', line 15

def git
  @git ||= GitRepo.new
end

#run_git(command) ⇒ Object



19
20
21
# File 'lib/danger/ci_source/local_git_repo.rb', line 19

def run_git(command)
  git.exec command
end