Class: Dependabot::GoModules::FileParser

Inherits:
FileParsers::Base
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dependabot/go_modules/file_parser.rb

Instance Method Summary collapse

Constructor Details

#initialize(dependency_files:, source: nil, repo_contents_path: nil, credentials: [], reject_external_code: false, options: {}) ⇒ FileParser

Returns a new instance of FileParser.

Raises:

  • (ArgumentError)


36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/dependabot/go_modules/file_parser.rb', line 36

def initialize(
  dependency_files:,
  source: nil,
  repo_contents_path: nil,
  credentials: [],
  reject_external_code: false,
  options: {}
)
  super

  raise ArgumentError, "repo_contents_path is required" if repo_contents_path.nil?

  set_go_environment_variables
end

Instance Method Details

#ecosystemObject



67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/dependabot/go_modules/file_parser.rb', line 67

def ecosystem
  @ecosystem ||= T.let(
    begin
      Ecosystem.new(
        name: ECOSYSTEM,
        package_manager: package_manager,
        language: language
      )
    end,
    T.nilable(Dependabot::Ecosystem)
  )
end

#parseObject



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/dependabot/go_modules/file_parser.rb', line 52

def parse
  dependency_set = Dependabot::FileParsers::Base::DependencySet.new

  required_packages.each do |hsh|
    unless skip_dependency?(hsh) # rubocop:disable Style/Next

      dep = dependency_from_details(hsh)
      dependency_set << dep
    end
  end

  dependency_set.dependencies
end

#run_in_parsed_context(command) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'lib/dependabot/go_modules/file_parser.rb', line 82

def run_in_parsed_context(command)
  SharedHelpers.in_a_temporary_repo_directory(T.must(source&.directory), repo_contents_path) do |path|
    # Create a fake empty module for local modules that are not inside the repository.
    # This allows us to run go commands that require all modules to be present.
    local_replacements.each do |_, stub_path|
      FileUtils.mkdir_p(stub_path)
      FileUtils.touch(File.join(stub_path, "go.mod"))
    end

    File.write("go.mod", go_mod_content)

    stdout, stderr, status = Open3.capture3(command)
    handle_parser_error(path, stderr) unless status.success?

    stdout
  end
end