Skip to content

Commit

Permalink
fixed all the lint issues
Browse files Browse the repository at this point in the history
  • Loading branch information
honeyankit committed May 20, 2024
1 parent d2c89e4 commit 2b15ba0
Showing 1 changed file with 45 additions and 34 deletions.
79 changes: 45 additions & 34 deletions cargo/lib/dependabot/cargo/update_checker/latest_version_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,56 +109,67 @@ def available_versions
def crates_listing
return @crates_listing unless @crates_listing.nil?

info = dependency.requirements.filter_map { |r| r[:source] }.first
index = (info && info[:index]) || CRATES_IO_API
info = fetch_dependency_info
index = fetch_index(info)

# Default request headers
hdrs = { "User-Agent" => "Dependabot (dependabot.com)" }
hdrs = default_headers
hdrs.merge!(auth_headers(info)) if index != CRATES_IO_API

if index != CRATES_IO_API
# Add authentication headers if credentials are present for this registry
registry_creds = credentials.find do |cred|
cred["type"] == "cargo_registry" && cred["registry"] == info[:name]
end
url = metadata_fetch_url(dependency, index)

unless registry_creds.nil?
# If there is a credential, but no actual token at this point, it means that dependabot-cli
# stripped the token from our credentials. In this case, the dependabot proxy will reintroduce
# the correct token, so we just use 'placeholder_token' as the token value.
token = registry_creds["token"] || "placeholder_token"
# B4PR
puts "Calling #{url} to fetch metadata for #{dependency.name} from #{index}"

hdrs["Authorization"] = token
end
end
response = fetch_response(url, hdrs)
return {} if response.status == 404

url = metadata_fetch_url(dependency, index)
@crates_listing = parse_response(response, index)

# B4PR
puts "Calling #{url} to fetch metadata for #{dependency.name} from #{index}"
puts "Fetched metadata for #{dependency.name} from #{index} successfully"
puts response.body

@crates_listing
end

def fetch_dependency_info
dependency.requirements.filter_map { |r| r[:source] }.first
end

def fetch_index(info)
(info && info[:index]) || CRATES_IO_API
end

response = Excon.get(
def default_headers
{ "User-Agent" => "Dependabot (dependabot.com)" }
end

def auth_headers(info)
registry_creds = credentials.find do |cred|
cred["type"] == "cargo_registry" && cred["registry"] == info[:name]
end

return {} if registry_creds.nil?

token = registry_creds["token"] || "placeholder_token"
{ "Authorization" => token }
end

def fetch_response(url, headers)
Excon.get(
url,
idempotent: true,
**SharedHelpers.excon_defaults(headers: hdrs)
**SharedHelpers.excon_defaults(headers: headers)
)
end

if response.status == 404
# Return {} if the URL is invalid and response is 404
return {}
end

def parse_response(response, index)
if index.start_with?("sparse+")
parsed_response = response.body.lines.map { |line| JSON.parse(line) }
@crates_listing = { "versions" => parsed_response }
{ "versions" => parsed_response }
else
@crates_listing = JSON.parse(response.body)
JSON.parse(response.body)
end

# B4PR
puts "Fetched metadata for #{dependency.name} from #{index} successfully"
puts response.body

@crates_listing
end

def metadata_fetch_url(dependency, index)
Expand Down

0 comments on commit 2b15ba0

Please sign in to comment.