Class: RepoFactFinder

Inherits:
Object
  • Object
show all
Defined in:
lib/ghdeploy/repo_fact_finder.rb

Constant Summary collapse

URL_PATTERN =
%r{^((http[s]?|ftp):/)?/?([^:/\s]+)((/\w+)*/)([\w\-\.]+[^#?\s]+)(.*)?(#[\w\-]+)?}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(remote_name) ⇒ RepoFactFinder

Returns a new instance of RepoFactFinder.



6
7
8
# File 'lib/ghdeploy/repo_fact_finder.rb', line 6

def initialize(remote_name)
  @remote_name = remote_name
end

Instance Attribute Details

#remote_nameObject (readonly)

Returns the value of attribute remote_name.



10
11
12
# File 'lib/ghdeploy/repo_fact_finder.rb', line 10

def remote_name
  @remote_name
end

Instance Method Details

#api_endpointObject



12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/ghdeploy/repo_fact_finder.rb', line 12

def api_endpoint
  return @api_endpoint if @api_endpoint
  url = remote.url
  if url.start_with?('http')
    matches = URL_PATTERN.match(url)
    @api_endpoint = "#{matches[2]}://api.#{matches[3]}"
  else
    host = url.split('@').last.split(':').first
    @api_endpoint = "https://api.#{host}"
  end
  @api_endpoint = fix_enterprise_api_endpoint(@api_endpoint)
end

#repoObject



25
26
27
28
29
# File 'lib/ghdeploy/repo_fact_finder.rb', line 25

def repo
  return @repo if @repo
  repo = url.split(%r{/|:}).last(2).join('/')
  @repo = repo.gsub(/(\.git)$/, '')
end

#tokenObject



31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/ghdeploy/repo_fact_finder.rb', line 31

def token
  github_host = api_endpoint.gsub(%r{^http[s]?://}, '')
  if github_host == 'api.github.com'
    ENV.fetch('GHDEPLOY_TOKEN')
  else
    key = github_host
          .gsub(/[\.\-]/, '_')
          .upcase
          .gsub(%r{/API/V3/}, '')
    ENV.fetch("GHDEPLOY_#{key}_TOKEN")
  end
end