Module: Dependabot::GoModules::PathConverter

Defined in:
lib/dependabot/go_modules/path_converter.rb

Class Method Summary collapse

Class Method Details

.git_url_for_path(path) ⇒ Object


13
14
15
16
17
18
19
20
21
22
# File 'lib/dependabot/go_modules/path_converter.rb', line 13

def self.git_url_for_path(path)
  # Save a query by manually converting golang.org/x names
  import_path = path.gsub(%r{^golang\.org/x}, "github.com/golang")

  SharedHelpers.run_helper_subprocess(
    command: NativeHelpers.helper_path,
    function: "getVcsRemoteForImport",
    args: { import: import_path }
  )
end

.git_url_for_path_without_go_helper(path) ⇒ Object

Used in dependabot-backend, which doesn't have access to any Go helpers. TODO: remove the need for this.


27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/dependabot/go_modules/path_converter.rb', line 27

def self.git_url_for_path_without_go_helper(path)
  # Save a query by manually converting golang.org/x names
  tmp_path = path.gsub(%r{^golang\.org/x}, "github.com/golang")

  # Currently, Dependabot::Source.new will return `nil` if it can't
  # find a git SCH associated with a path. If it is ever extended to
  # handle non-git sources we'll need to add an additional check here.
  return Source.from_url(tmp_path).url if Source.from_url(tmp_path)
  return "https://#{tmp_path}" if tmp_path.end_with?(".git")
  return unless ( = (path))

  # Look for a GitHub, Bitbucket or GitLab URL in the response
  .scan(Dependabot::Source::SOURCE_REGEX) do
    source_url = Regexp.last_match.to_s
    return Source.from_url(source_url).url
  end

  # If none are found, parse the response and return the go-import path
  doc = Nokogiri::XML()
  doc.remove_namespaces!
  import_details =
    doc.xpath("//meta").
    find { |n| n.attributes["name"]&.value == "go-import" }&.
    attributes&.fetch("content")&.value&.split(/\s+/)
  return unless import_details && import_details[1] == "git"

  import_details[2]
end