Skip to content

Commit

Permalink
Strict type Dependabot::MetadataFinders::Base (dependabot#8774)
Browse files Browse the repository at this point in the history
  • Loading branch information
JamieMagee authored Jan 16, 2024
1 parent 5abeda1 commit 99b320e
Show file tree
Hide file tree
Showing 7 changed files with 107 additions and 53 deletions.
134 changes: 94 additions & 40 deletions common/lib/dependabot/metadata_finders/base.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,40 @@
# typed: true
# typed: strict
# frozen_string_literal: true

require "sorbet-runtime"
require "dependabot/source"

module Dependabot
module MetadataFinders
class Base
extend T::Sig
extend T::Helpers

require "dependabot/metadata_finders/base/changelog_finder"
require "dependabot/metadata_finders/base/release_finder"
require "dependabot/metadata_finders/base/commits_finder"

PACKAGE_MANAGERS_WITH_RELIABLE_DIRECTORIES = %w(npm_and_yarn pub).freeze
PACKAGE_MANAGERS_WITH_RELIABLE_DIRECTORIES = T.let(%w(npm_and_yarn pub).freeze, T::Array[String])

sig { returns(Dependabot::Dependency) }
attr_reader :dependency

attr_reader :dependency, :credentials
sig { returns(T::Array[T::Hash[String, String]]) }
attr_reader :credentials

sig do
params(
dependency: Dependabot::Dependency,
credentials: T::Array[T::Hash[String, String]]
)
.void
end
def initialize(dependency:, credentials:)
@dependency = dependency
@credentials = credentials
end

sig { returns(T.nilable(String)) }
def source_url
if reliable_source_directory?
source&.url_with_directory
Expand All @@ -27,106 +43,144 @@ def source_url
end
end

sig { returns(T.nilable(String)) }
def homepage_url
source_url
end

sig { returns(T.nilable(String)) }
def changelog_url
@changelog_finder ||= ChangelogFinder.new(
dependency: dependency,
source: source,
credentials: credentials,
suggested_changelog_url: suggested_changelog_url
@changelog_finder ||= T.let(
ChangelogFinder.new(
dependency: dependency,
source: source,
credentials: credentials,
suggested_changelog_url: suggested_changelog_url
),
T.nilable(ChangelogFinder)
)
@changelog_finder.changelog_url
end

sig { returns(T.nilable(String)) }
def changelog_text
@changelog_finder ||= ChangelogFinder.new(
dependency: dependency,
source: source,
credentials: credentials,
suggested_changelog_url: suggested_changelog_url
@changelog_finder ||= T.let(
ChangelogFinder.new(
dependency: dependency,
source: source,
credentials: credentials,
suggested_changelog_url: suggested_changelog_url
),
T.nilable(ChangelogFinder)
)
@changelog_finder.changelog_text
end

sig { returns(T.nilable(String)) }
def upgrade_guide_url
@changelog_finder ||= ChangelogFinder.new(
dependency: dependency,
source: source,
credentials: credentials,
suggested_changelog_url: suggested_changelog_url
@changelog_finder ||= T.let(
ChangelogFinder.new(
dependency: dependency,
source: source,
credentials: credentials,
suggested_changelog_url: suggested_changelog_url
),
T.nilable(ChangelogFinder)
)
@changelog_finder.upgrade_guide_url
end

sig { returns(T.nilable(String)) }
def upgrade_guide_text
@changelog_finder ||= ChangelogFinder.new(
dependency: dependency,
source: source,
credentials: credentials,
suggested_changelog_url: suggested_changelog_url
@changelog_finder ||= T.let(
ChangelogFinder.new(
dependency: dependency,
source: source,
credentials: credentials,
suggested_changelog_url: suggested_changelog_url
),
T.nilable(ChangelogFinder)
)
@changelog_finder.upgrade_guide_text
end

sig { returns(T.nilable(String)) }
def releases_url
@release_finder ||= ReleaseFinder.new(
dependency: dependency,
source: source,
credentials: credentials
@release_finder ||= T.let(
ReleaseFinder.new(
dependency: dependency,
source: source,
credentials: credentials
),
T.nilable(ReleaseFinder)
)
@release_finder.releases_url
end

sig { returns(T.nilable(String)) }
def releases_text
@release_finder ||= ReleaseFinder.new(
dependency: dependency,
source: source,
credentials: credentials
@release_finder ||= T.let(
ReleaseFinder.new(
dependency: dependency,
source: source,
credentials: credentials
),
T.nilable(ReleaseFinder)
)
@release_finder.releases_text
end

sig { returns(T.nilable(String)) }
def commits_url
@commits_finder ||= CommitsFinder.new(
dependency: dependency,
source: source,
credentials: credentials
@commits_finder ||= T.let(
CommitsFinder.new(
dependency: dependency,
source: source,
credentials: credentials
),
T.nilable(CommitsFinder)
)
@commits_finder.commits_url
end

sig { returns(T::Array[T::Hash[Symbol, String]]) }
def commits
@commits_finder ||= CommitsFinder.new(
dependency: dependency,
source: source,
credentials: credentials
@commits_finder ||= T.let(
CommitsFinder.new(
dependency: dependency,
source: source,
credentials: credentials
),
T.nilable(CommitsFinder)
)
@commits_finder.commits
end

sig { overridable.returns(T.nilable(String)) }
def maintainer_changes
nil
end

private

sig { overridable.returns(T.nilable(String)) }
def suggested_changelog_url
nil
end

sig { returns(T.nilable(Dependabot::Source)) }
def source
return @source if defined?(@source)

@source = look_up_source
@source = T.let(look_up_source, T.nilable(Dependabot::Source))
end

sig { overridable.returns(Dependabot::Source) }
def look_up_source
raise NotImplementedError
end

sig { returns(T::Boolean) }
def reliable_source_directory?
MetadataFinders::Base::PACKAGE_MANAGERS_WITH_RELIABLE_DIRECTORIES
.include?(dependency.package_manager)
Expand Down
2 changes: 1 addition & 1 deletion docker/lib/dependabot/docker/metadata_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class MetadataFinder < Dependabot::MetadataFinders::Base
def look_up_source
return if dependency.requirements.empty?

new_source = dependency.requirements.first[:source]
new_source = dependency.requirements.first&.fetch(:source)
return unless new_source && new_source[:registry] && new_source[:tag]

image_ref = "#{new_source[:registry]}/#{dependency.name}:#{new_source[:tag]}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class MetadataFinder < Dependabot::MetadataFinders::Base
private

def look_up_source
url = dependency.requirements.first.fetch(:source)[:url] ||
dependency.requirements.first.fetch(:source).fetch("url")
url = dependency.requirements.first&.fetch(:source)&.fetch(:url) ||
dependency.requirements.first&.fetch(:source)&.fetch("url")

Source.from_url(url)
end
Expand Down
8 changes: 4 additions & 4 deletions gradle/lib/dependabot/gradle/metadata_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def repo_has_subdir_for_dep?(tmp_source)
@repo_has_subdir_for_dep[tmp_source] =
fetcher.send(:repo_contents, raise_errors: false)
.select { |f| f.type == "dir" }
.any? { |f| artifact.end_with?(f.name) }
.any? { |f| artifact&.end_with?(f.name) }
rescue Dependabot::BranchNotFound
tmp_source.branch = nil
retry
Expand Down Expand Up @@ -101,7 +101,7 @@ def source_from_anywhere_in_pom(pom)

github_urls.find do |url|
repo = T.must(Source.from_url(url)).repo
repo.end_with?(dependency.name.split(":").last)
repo.end_with?(T.must(dependency.name.split(":").last))
end
end

Expand Down Expand Up @@ -145,7 +145,7 @@ def parent_pom_file(pom)

def maven_repo_url
source = dependency.requirements
.find { |r| r&.fetch(:source) }&.fetch(:source)
.find { |r| r.fetch(:source) }&.fetch(:source)

source&.fetch(:url, nil) ||
source&.fetch("url") ||
Expand All @@ -162,7 +162,7 @@ def maven_repo_dependency_url
dependency.name.split(":")
end

"#{maven_repo_url}/#{group_id.tr('.', '/')}/#{artifact_id}"
"#{maven_repo_url}/#{group_id&.tr('.', '/')}/#{artifact_id}"
end

def plugin?
Expand Down
8 changes: 4 additions & 4 deletions nuget/lib/dependabot/nuget/metadata_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def look_up_source
end

def src_repo_from_project
source = dependency.requirements.find { |r| r&.fetch(:source) }&.fetch(:source)
source = dependency.requirements.find { |r| r.fetch(:source) }&.fetch(:source)
return unless source

# Query the service index e.g. https://nuget.pkg.github.com/ORG/index.json
Expand Down Expand Up @@ -123,14 +123,14 @@ def dependency_nuspec_file

def dependency_nuspec_url
source = dependency.requirements
.find { |r| r&.fetch(:source) }&.fetch(:source)
.find { |r| r.fetch(:source) }&.fetch(:source)

source.fetch(:nuspec_url) if source&.key?(:nuspec_url)
end

def dependency_source_url
source = dependency.requirements
.find { |r| r&.fetch(:source) }&.fetch(:source)
.find { |r| r.fetch(:source) }&.fetch(:source)

return unless source
return source.fetch(:source_url) if source.key?(:source_url)
Expand All @@ -141,7 +141,7 @@ def dependency_source_url
# rubocop:disable Metrics/PerceivedComplexity
def auth_header
source = dependency.requirements
.find { |r| r&.fetch(:source) }&.fetch(:source)
.find { |r| r.fetch(:source) }&.fetch(:source)
url = source&.fetch(:url, nil) || source&.fetch("url")

token = credentials
Expand Down
2 changes: 1 addition & 1 deletion pub/lib/dependabot/pub/metadata_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class MetadataFinder < Dependabot::MetadataFinders::Base
private

def look_up_source
source = dependency.requirements&.first&.dig(:source)
source = dependency.requirements.first&.dig(:source)
if source&.dig("type") == "git"
result = T.must(Source.from_url(source.dig("description", "url")))
result.directory = source.dig("description", "path")
Expand Down
2 changes: 1 addition & 1 deletion swift/lib/dependabot/swift/metadata_finder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def new_source_type
def find_source_from_git_url
info = dependency.source_details

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

Expand Down

0 comments on commit 99b320e

Please sign in to comment.