Skip to content

Commit

Permalink
Unify memoization (#7772)
Browse files Browse the repository at this point in the history
  • Loading branch information
deivid-rodriguez authored Aug 9, 2023
1 parent 6d2d4ce commit b4fc1c4
Show file tree
Hide file tree
Showing 10 changed files with 38 additions and 49 deletions.
3 changes: 1 addition & 2 deletions common/lib/dependabot/metadata_finders/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,9 +117,8 @@ def suggested_changelog_url
end

def source
return @source if @source_lookup_attempted
return @source if defined?(@source)

@source_lookup_attempted = true
@source = look_up_source
end

Expand Down
5 changes: 2 additions & 3 deletions composer/lib/dependabot/composer/file_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,9 @@ def composer_json
end

def composer_lock
return @composer_lock if @composer_lock_lookup_attempted
return @composer_lock if defined?(@composer_lock)

@composer_lock_lookup_attempted = true
@composer_lock ||= fetch_file_if_present("composer.lock")
@composer_lock = fetch_file_if_present("composer.lock")
end

# NOTE: This is fetched but currently unused
Expand Down
11 changes: 5 additions & 6 deletions elm/lib/dependabot/elm/update_checker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,17 @@ def filter_lower_versions(versions_array)
end

def all_versions
return @all_versions if @version_lookup_attempted

@version_lookup_attempted = true
@all_versions ||= fetch_all_versions
end

def fetch_all_versions
response = Dependabot::RegistryClient.get(
url: "https://package.elm-lang.org/packages/#{dependency.name}/releases.json"
)

return @all_versions = [] unless response.status == 200
return [] unless response.status == 200

@all_versions =
JSON.parse(response.body).
JSON.parse(response.body).
keys.
map { |v| version_class.new(v) }.
sort
Expand Down
9 changes: 6 additions & 3 deletions hex/lib/dependabot/hex/file_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,13 @@ def mixfile
end

def lockfile
return @lockfile if @lockfile_lookup_attempted
return @lockfile if defined?(@lockfile)

@lockfile_lookup_attempted = true
@lockfile ||= fetch_file_from_host("mix.lock")
@lockfile = fetch_lockfile
end

def fetch_lockfile
fetch_file_from_host("mix.lock")
rescue Dependabot::DependencyFileNotFound
nil
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -261,25 +261,25 @@ def version_endpoint_working?
end

def npm_details
return @npm_details if @npm_details_lookup_attempted
return @npm_details if defined?(@npm_details)

@npm_details_lookup_attempted = true
@npm_details ||=
begin
npm_response = fetch_npm_response

check_npm_response(npm_response)
JSON.parse(npm_response.body)
rescue JSON::ParserError,
Excon::Error::Timeout,
Excon::Error::Socket,
RegistryError => e
if git_dependency?
nil
else
raise_npm_details_error(e)
end
end
@npm_details = fetch_npm_details
end

def fetch_npm_details
npm_response = fetch_npm_response

check_npm_response(npm_response)
JSON.parse(npm_response.body)
rescue JSON::ParserError,
Excon::Error::Timeout,
Excon::Error::Socket,
RegistryError => e
if git_dependency?
nil
else
raise_npm_details_error(e)
end
end

def fetch_npm_response
Expand Down
11 changes: 6 additions & 5 deletions nuget/lib/dependabot/nuget/file_fetcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,11 +103,12 @@ def sln_file_names
# rubocop:enable Metrics/PerceivedComplexity

def directory_build_files
return @directory_build_files if @directory_build_files_checked
@directory_build_files ||= fetch_directory_build_files
end

@directory_build_files_checked = true
def fetch_directory_build_files
attempted_paths = []
@directory_build_files = []
directory_build_files = []

# Don't need to insert "." here, because Directory.Build.props files
# can only be used by project files (not packages.config ones)
Expand All @@ -131,11 +132,11 @@ def directory_build_files

attempted_paths << path
file = fetch_file_if_present(path)
@directory_build_files << file if file
directory_build_files << file if file
end
end

@directory_build_files
directory_build_files
end

def possible_build_file_paths(base)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,6 @@ def initialize(dependencies:, dependency_files:, credentials:)
end

def updated_dependency_files
return @updated_dependency_files if @update_already_attempted

@update_already_attempted = true
@updated_dependency_files ||= fetch_updated_dependency_files
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ def initialize(dependencies:, dependency_files:, credentials:)
end

def updated_dependency_files
return @updated_dependency_files if @update_already_attempted

@update_already_attempted = true
@updated_dependency_files ||= fetch_updated_dependency_files
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ def initialize(dependencies:, dependency_files:, credentials:)
end

def updated_dependency_files
return @updated_dependency_files if @update_already_attempted

@update_already_attempted = true
@updated_dependency_files ||= fetch_updated_dependency_files
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,6 @@ def initialize(dependencies:, dependency_files:, credentials:)
end

def updated_dependency_files
return @updated_dependency_files if @update_already_attempted

@update_already_attempted = true
@updated_dependency_files ||= fetch_updated_dependency_files
end

Expand Down

0 comments on commit b4fc1c4

Please sign in to comment.