Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strict type Dependabot::Hex::MetaDataFinder #9913

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions hex/lib/dependabot/hex/metadata_finder.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
# typed: true
# typed: strict
# frozen_string_literal: true

require "excon"
require "dependabot/metadata_finders"
require "dependabot/metadata_finders/base"
require "dependabot/registry_client"
require "sorbet-runtime"

module Dependabot
module Hex
class MetadataFinder < Dependabot::MetadataFinders::Base
SOURCE_KEYS = %w(
extend T::Sig

SOURCE_KEYS = T.let(%w(
GitHub Github github
GitLab Gitlab gitlab
BitBucket Bitbucket bitbucket
Source source
).freeze
).freeze, T::Array[String])

private

sig { override.returns(T.nilable(Dependabot::Source)) }
def look_up_source
case new_source_type
when "default" then find_source_from_hex_listing
Expand All @@ -26,31 +30,35 @@ def look_up_source
end
end

sig { returns(T.nilable(String)) }
def new_source_type
dependency.source_type
end

sig { returns(T.nilable(Dependabot::Source)) }
def find_source_from_hex_listing
potential_source_urls =
SOURCE_KEYS
.filter_map { |key| hex_listing.dig("meta", "links", key) }
.filter_map { |key| T.must(hex_listing).dig("meta", "links", key) }

source_url = potential_source_urls.find { |url| Source.from_url(url) }
Source.from_url(source_url)
end

sig { returns(T.nilable(Dependabot::Source)) }
def find_source_from_git_url
info = dependency.requirements.filter_map { |r| r[:source] }.first

url = info[:url] || info.fetch("url")
Source.from_url(url)
end

sig { returns(T.nilable(T::Hash[String, T.untyped])) }
def hex_listing
return @hex_listing unless @hex_listing.nil?

response = Dependabot::RegistryClient.get(url: "https://hex.pm/api/packages/#{dependency.name}")
@hex_listing = JSON.parse(response.body)
@hex_listing = T.let(JSON.parse(response.body), T.nilable(T::Hash[String, T.untyped]))
end
end
end
Expand Down
Loading