class Dependabot::Source

Constants

AZURE_SOURCE
BITBUCKET_SOURCE
GITHUB_SOURCE
GITLAB_SOURCE
SOURCE_REGEX

Attributes

api_endpoint[RW]
branch[RW]
commit[RW]
directory[RW]
hostname[RW]
provider[RW]
repo[RW]

Public Class Methods

from_url(url_string) click to toggle source
# 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
new(provider:, repo:, directory: nil, branch: nil, commit: nil, hostname: nil, api_endpoint: nil) click to toggle source
# 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

Public Instance Methods

organization() click to toggle source
# File lib/dependabot/source.rb, line 97
def organization
  repo.split("/").first
end
project() click to toggle source
# File lib/dependabot/source.rb, line 101
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() click to toggle source
# File lib/dependabot/source.rb, line 110
def unscoped_repo
  repo.split("/").last
end
url() click to toggle source
# File lib/dependabot/source.rb, line 73
def url
  "https://" + hostname + "/" + repo
end
url_with_directory() click to toggle source
# File lib/dependabot/source.rb, line 77
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

Private Instance Methods

default_api_endpoint(provider) click to toggle source
# File lib/dependabot/source.rb, line 127
def default_api_endpoint(provider)
  case provider
  when "github" then "https://api.github.com/"
  when "bitbucket" then "https://api.bitbucket.org/2.0/"
  when "gitlab" then "https://gitlab.com/api/v4"
  when "azure" then "https://dev.azure.com/"
  when "codecommit" then nil
  else raise "Unexpected provider '#{provider}'"
  end
end
default_hostname(provider) click to toggle source
# File lib/dependabot/source.rb, line 116
def default_hostname(provider)
  case provider
  when "github" then "github.com"
  when "bitbucket" then "bitbucket.org"
  when "gitlab" then "gitlab.com"
  when "azure" then "dev.azure.com"
  when "codecommit" then "us-east-1"
  else raise "Unexpected provider '#{provider}'"
  end
end