Class: Dependabot::Source

Inherits:
Object
  • Object
show all
Defined in:
lib/dependabot/source.rb

Constant Summary collapse

GITHUB_SOURCE =
%r{
  (?<provider>github)
  (?:\.com)[/:]
  (?<repo>[\w.-]+/(?:(?!\.git|\.\s)[\w.-])+)
  (?:(?:/tree|/blob)/(?<branch>[^/]+)/(?<directory>.*)[\#|/])?
}x.freeze
GITLAB_SOURCE =
%r{
  (?<provider>gitlab)
  (?:\.com)[/:]
  (?<repo>[\w.-]+/(?:(?!\.git|\.\s)[\w.-])+)
  (?:(?:/tree|/blob)/(?<branch>[^/]+)/(?<directory>.*)[\#|/])?
}x.freeze
BITBUCKET_SOURCE =
%r{
  (?<provider>bitbucket)
  (?:\.org)[/:]
  (?<repo>[\w.-]+/(?:(?!\.git|\.\s)[\w.-])+)
  (?:(?:/src)/(?<branch>[^/]+)/(?<directory>.*)[\#|/])?
}x.freeze
AZURE_SOURCE =
%r{
  (?<provider>azure)
  (?:\.com)[/:]
  (?<repo>[\w.-]+/([\w.-]+/)?(?:_git/)(?:(?!\.git|\.\s)[\w.-])+)
}x.freeze
SOURCE_REGEX =
/
  (?:#{GITHUB_SOURCE})|
  (?:#{GITLAB_SOURCE})|
  (?:#{BITBUCKET_SOURCE})|
  (?:#{AZURE_SOURCE})
/x.freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(provider:, repo:, directory: nil, branch: nil, commit: nil, hostname: nil, api_endpoint: nil) ⇒ Source

Returns a new instance of Source.


55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/dependabot/source.rb', line 55

def initialize(provider:, repo:, directory: nil, branch: nil, commit: nil,
               hostname: nil, api_endpoint: nil)
  if (hostname.nil? ^ api_endpoint.nil?) && (provider != "codecommit")
    msg = "Both hostname and api_endpoint must be specified if either "\
          "are. Alternatively, both may be left blank to use the "\
          "provider's defaults."
    raise msg
  end

  @provider = provider
  @repo = repo
  @directory = directory
  @branch = branch
  @commit = commit
  @hostname = hostname || default_hostname(provider)
  @api_endpoint = api_endpoint || default_api_endpoint(provider)
end

Instance Attribute Details

#api_endpointObject

Returns the value of attribute api_endpoint


39
40
41
# File 'lib/dependabot/source.rb', line 39

def api_endpoint
  @api_endpoint
end

#branchObject

Returns the value of attribute branch


39
40
41
# File 'lib/dependabot/source.rb', line 39

def branch
  @branch
end

#commitObject

Returns the value of attribute commit


39
40
41
# File 'lib/dependabot/source.rb', line 39

def commit
  @commit
end

#directoryObject

Returns the value of attribute directory


39
40
41
# File 'lib/dependabot/source.rb', line 39

def directory
  @directory
end

#hostnameObject

Returns the value of attribute hostname


39
40
41
# File 'lib/dependabot/source.rb', line 39

def hostname
  @hostname
end

#providerObject

Returns the value of attribute provider


39
40
41
# File 'lib/dependabot/source.rb', line 39

def provider
  @provider
end

#repoObject

Returns the value of attribute repo


39
40
41
# File 'lib/dependabot/source.rb', line 39

def repo
  @repo
end

Class Method Details

.from_url(url_string) ⇒ Object


42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/dependabot/source.rb', line 42

def self.from_url(url_string)
  return unless url_string&.match?(SOURCE_REGEX)

  captures = url_string.match(SOURCE_REGEX).named_captures

  new(
    provider: captures.fetch("provider"),
    repo: captures.fetch("repo"),
    directory: captures.fetch("directory"),
    branch: captures.fetch("branch")
  )
end

Instance Method Details

#organizationObject

rubocop:enable Metrics/CyclomaticComplexity


99
100
101
# File 'lib/dependabot/source.rb', line 99

def organization
  repo.split("/").first
end

#projectObject


103
104
105
106
107
108
109
110
# File 'lib/dependabot/source.rb', line 103

def project
  raise "Project is an Azure DevOps concept only" unless provider == "azure"

  parts = repo.split("/_git/")
  return parts.first.split("/").last if parts.first.split("/").count == 2

  parts.last
end

#unscoped_repoObject


112
113
114
# File 'lib/dependabot/source.rb', line 112

def unscoped_repo
  repo.split("/").last
end

#urlObject


73
74
75
# File 'lib/dependabot/source.rb', line 73

def url
  "https://" + hostname + "/" + repo
end

#url_with_directoryObject

rubocop:disable Metrics/CyclomaticComplexity


78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/dependabot/source.rb', line 78

def url_with_directory
  return url if [nil, ".", "/"].include?(directory)

  case provider
  when "github", "gitlab"
    path = Pathname.new(File.join("tree/#{branch || 'HEAD'}", directory)).
           cleanpath.to_path
    url + "/" + path
  when "bitbucket"
    path = Pathname.new(File.join("src/#{branch || 'default'}", directory)).
           cleanpath.to_path
    url + "/" + path
  when "azure"
    url + "?path=#{directory}"
  when "codecommit"
    raise "The codecommit provider does not utilize URLs"
  else raise "Unexpected repo provider '#{provider}'"
  end
end