Class: Dependabot::Source
- Inherits:
-
Object
- Object
- Dependabot::Source
- 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
- GITHUB_ENTERPRISE_SOURCE =
%r{ (?<protocol>(http://|https://|git://|ssh://))* (?<username>[^@]+@)* (?<host>[^/]+) [/:] (?<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
- CODECOMMIT_SOURCE =
%r{ (?<protocol>(http://|https://|git://|ssh://)) git[-] (?<provider>codecommit) (?:.*) (?:\.com/v1/repos/) (?<repo>([^/]*)) (?:/)?(?<directory>[^?]*)? [?]? (?<ref>.*)? }x.freeze
- SOURCE_REGEX =
/ (?:#{GITHUB_SOURCE})| (?:#{GITLAB_SOURCE})| (?:#{BITBUCKET_SOURCE})| (?:#{AZURE_SOURCE})| (?:#{CODECOMMIT_SOURCE}) /x.freeze
- IGNORED_PROVIDER_HOSTS =
%w(gitbox.apache.org svn.apache.org fuchsia.googlesource.com).freeze
Instance Attribute Summary collapse
-
#api_endpoint ⇒ Object
Returns the value of attribute api_endpoint.
-
#branch ⇒ Object
Returns the value of attribute branch.
-
#commit ⇒ Object
Returns the value of attribute commit.
-
#directory ⇒ Object
Returns the value of attribute directory.
-
#hostname ⇒ Object
Returns the value of attribute hostname.
-
#provider ⇒ Object
Returns the value of attribute provider.
-
#repo ⇒ Object
Returns the value of attribute repo.
Class Method Summary collapse
- .from_url(url_string) ⇒ Object
- .github_enterprise?(base_url) ⇒ Boolean
- .github_enterprise_from_url(url_string) ⇒ Object
Instance Method Summary collapse
-
#initialize(provider:, repo:, directory: nil, branch: nil, commit: nil, hostname: nil, api_endpoint: nil) ⇒ Source
constructor
A new instance of Source.
- #organization ⇒ Object
- #project ⇒ Object
- #unscoped_repo ⇒ Object
- #url ⇒ Object
- #url_with_directory ⇒ Object
Constructor Details
#initialize(provider:, repo:, directory: nil, branch: nil, commit: nil, hostname: nil, api_endpoint: nil) ⇒ Source
Returns a new instance of Source.
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/dependabot/source.rb', line 109 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_endpoint ⇒ Object
Returns the value of attribute api_endpoint.
63 64 65 |
# File 'lib/dependabot/source.rb', line 63 def api_endpoint @api_endpoint end |
#branch ⇒ Object
Returns the value of attribute branch.
63 64 65 |
# File 'lib/dependabot/source.rb', line 63 def branch @branch end |
#commit ⇒ Object
Returns the value of attribute commit.
63 64 65 |
# File 'lib/dependabot/source.rb', line 63 def commit @commit end |
#directory ⇒ Object
Returns the value of attribute directory.
63 64 65 |
# File 'lib/dependabot/source.rb', line 63 def directory @directory end |
#hostname ⇒ Object
Returns the value of attribute hostname.
63 64 65 |
# File 'lib/dependabot/source.rb', line 63 def hostname @hostname end |
#provider ⇒ Object
Returns the value of attribute provider.
63 64 65 |
# File 'lib/dependabot/source.rb', line 63 def provider @provider end |
#repo ⇒ Object
Returns the value of attribute repo.
63 64 65 |
# File 'lib/dependabot/source.rb', line 63 def repo @repo end |
Class Method Details
.from_url(url_string) ⇒ Object
66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/dependabot/source.rb', line 66 def self.from_url(url_string) return github_enterprise_from_url(url_string) 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 |
.github_enterprise?(base_url) ⇒ Boolean
98 99 100 101 102 103 104 105 106 107 |
# File 'lib/dependabot/source.rb', line 98 def self.github_enterprise?(base_url) resp = Excon.get(File.join(base_url, "status")) resp.status == 200 && # Alternatively: resp.headers["Server"] == "GitHub.com", but this # currently doesn't work with development environments resp.headers["X-GitHub-Request-Id"] && !resp.headers["X-GitHub-Request-Id"].empty? rescue StandardError false end |
.github_enterprise_from_url(url_string) ⇒ Object
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/dependabot/source.rb', line 79 def self.github_enterprise_from_url(url_string) captures = url_string&.match(GITHUB_ENTERPRISE_SOURCE)&.named_captures return unless captures return if IGNORED_PROVIDER_HOSTS.include?(captures.fetch("host")) base_url = "https://#{captures.fetch('host')}" return unless github_enterprise?(base_url) new( provider: "github", repo: captures.fetch("repo"), directory: captures.fetch("directory"), branch: captures.fetch("branch"), hostname: captures.fetch("host"), api_endpoint: File.join(base_url, "api", "v3") ) end |
Instance Method Details
#organization ⇒ Object
151 152 153 |
# File 'lib/dependabot/source.rb', line 151 def organization repo.split("/").first end |
#project ⇒ Object
155 156 157 158 159 160 161 162 |
# File 'lib/dependabot/source.rb', line 155 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_repo ⇒ Object
164 165 166 |
# File 'lib/dependabot/source.rb', line 164 def unscoped_repo repo.split("/").last end |
#url ⇒ Object
127 128 129 |
# File 'lib/dependabot/source.rb', line 127 def url "https://" + hostname + "/" + repo end |
#url_with_directory ⇒ Object
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/dependabot/source.rb', line 131 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 |