Class: Saddler::Reporter::Support::Git::Repository

Inherits:
Object
  • Object
show all
Defined in:
lib/saddler/reporter/support/git/repository.rb

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Repository

Returns a new instance of Repository.



6
7
8
# File 'lib/saddler/reporter/support/git/repository.rb', line 6

def initialize(path)
  @git = ::Git.open(path)
end

Instance Method Details

#current_branchObject



28
29
30
# File 'lib/saddler/reporter/support/git/repository.rb', line 28

def current_branch
  env_current_branch || @git.current_branch
end

#env_current_branchObject



53
54
55
56
57
58
59
# File 'lib/saddler/reporter/support/git/repository.rb', line 53

def env_current_branch
  if ENV['CURRENT_BRANCH']
    ENV['CURRENT_BRANCH']
  elsif ENV['TRAVIS_BRANCH']
    ENV['TRAVIS_BRANCH']
  end
end

#github_api_endpointObject



49
50
51
# File 'lib/saddler/reporter/support/git/repository.rb', line 49

def github_api_endpoint
  ENV['GITHUB_API_ENDPOINT'] || 'api.github.com'
end

#github_domainObject

FIXME: if endpoint set, this return wrong result



42
43
44
45
46
47
# File 'lib/saddler/reporter/support/git/repository.rb', line 42

def github_domain
  github_api_endpoint
    .split('.')
    .slice(-2, 2)
    .join('.')
end

#headObject



32
33
34
# File 'lib/saddler/reporter/support/git/repository.rb', line 32

def head
  @git.object('HEAD')
end

#remote_urlsObject



21
22
23
24
25
26
# File 'lib/saddler/reporter/support/git/repository.rb', line 21

def remote_urls
  @git
    .branches
    .remote
    .map { |branch| branch.remote.url }
end

#slugObject



10
11
12
13
14
15
16
17
18
19
# File 'lib/saddler/reporter/support/git/repository.rb', line 10

def slug
  name = /[[:alnum:]_\-\.]*/
  repo = /[[:alnum:]_\-\.]*/
  regex_slug = %r{#{name}/#{repo}}
  regex = /.*?#{Regexp.quote(github_domain)}.*?(?<slug>#{regex_slug})/
  target = remote_urls.map do |url|
    match = regex.match(strip_git_extension(url))
    match[:slug] if match
  end.compact.first
end

#strip_git_extension(name) ⇒ Object



36
37
38
39
# File 'lib/saddler/reporter/support/git/repository.rb', line 36

def strip_git_extension(name)
  match = /\A(?<identity>.*?)(?:\.git)?\z/.match(name)
  match[:identity] if match
end