Class: Danger::CISource::LocalGitRepo

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

Overview

ignore

Instance Attribute Summary collapse

Attributes inherited from CI

#pull_request_id, #repo_slug, #repo_url

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from CI

available_ci_sources, inherited, #supports?

Constructor Details

#initialize(env = {}) ⇒ LocalGitRepo

Returns a new instance of LocalGitRepo.



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/danger/ci_source/local_git_repo.rb', line 28

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

Instance Attribute Details

#base_commitObject

Returns the value of attribute base_commit.



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

def base_commit
  @base_commit
end

#head_commitObject

Returns the value of attribute head_commit.



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

def head_commit
  @head_commit
end

Class Method Details

.validates?(env) ⇒ Boolean

Returns:

  • (Boolean)


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

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

Instance Method Details

#gitObject



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

def git
  @git ||= GitRepo.new
end

#run_git(command) ⇒ Object



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

def run_git(command)
  git.exec command
end

#supported_request_sourcesObject



24
25
26
# File 'lib/danger/ci_source/local_git_repo.rb', line 24

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