Class: Dependabot::Swift::UpdateChecker::LatestVersionResolver

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dependabot/swift/update_checker/latest_version_resolver.rb

Constant Summary collapse

DAY_IN_SECONDS =
T.let(24 * 60 * 60, Integer)

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(dependency:, credentials:, cooldown_options:, git_commit_checker:) ⇒ LatestVersionResolver

Returns a new instance of LatestVersionResolver.



27
28
29
30
31
32
33
34
35
# File 'lib/dependabot/swift/update_checker/latest_version_resolver.rb', line 27

def initialize(dependency:, credentials:, cooldown_options:, git_commit_checker:)
  @dependency = dependency
  @credentials = credentials
  @cooldown_options = cooldown_options
  @git_commit_checker = T.let(
    git_commit_checker,
    Dependabot::GitCommitChecker
  )
end

Instance Attribute Details

#credentialsObject (readonly)

Returns the value of attribute credentials.



121
122
123
# File 'lib/dependabot/swift/update_checker/latest_version_resolver.rb', line 121

def credentials
  @credentials
end

#dependencyObject (readonly)

Returns the value of attribute dependency.



38
39
40
# File 'lib/dependabot/swift/update_checker/latest_version_resolver.rb', line 38

def dependency
  @dependency
end

#git_commit_checkerObject (readonly)

Returns the value of attribute git_commit_checker.



118
119
120
# File 'lib/dependabot/swift/update_checker/latest_version_resolver.rb', line 118

def git_commit_checker
  @git_commit_checker
end

Instance Method Details

#check_if_version_in_cooldown_period?(release_date) ⇒ Boolean

Returns:

  • (Boolean)


80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/dependabot/swift/update_checker/latest_version_resolver.rb', line 80

def check_if_version_in_cooldown_period?(release_date)
  return false unless release_date.length.positive?

  cooldown = @cooldown_options
  return false unless cooldown

  return false if cooldown.nil?

  # Get maximum cooldown days based on semver parts
  days = [cooldown.default_days, cooldown.semver_major_days].max
  days = cooldown.semver_minor_days unless days > cooldown.semver_minor_days
  days = cooldown.semver_patch_days unless days > cooldown.semver_patch_days
  # Calculate the number of seconds passed since the release
  passed_seconds = Time.now.to_i - release_date_to_seconds(release_date)
  # Check if the release is within the cooldown period
  passed_seconds < days * DAY_IN_SECONDS
end

#latest_version_tagObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/dependabot/swift/update_checker/latest_version_resolver.rb', line 44

def latest_version_tag
  # step one fetch allowed version tags and
  allowed_version_tags = git_commit_checker.allowed_version_tags
  begin
    # sort the allowed version tags by name in descending order
    select_version_tags_in_cooldown_period&.each do |tag_name|
      # filter out if name is not in cooldown period
      allowed_version_tags.reject! do |gitref_filtered|
        true if gitref_filtered.name == tag_name
      end
    end
    Dependabot.logger.info("Allowed version tags after filtering versions in cooldown:
      #{allowed_version_tags.map(&:name).join(', ')}")
    git_commit_checker.max_local_tag(allowed_version_tags)
  rescue StandardError => e
    Dependabot.logger.error("Error fetching latest version tag: #{e.message}")
    git_commit_checker.local_tag_for_latest_version
  end
end

#package_details_fetcherObject



107
108
109
110
111
112
113
114
115
# File 'lib/dependabot/swift/update_checker/latest_version_resolver.rb', line 107

def package_details_fetcher
  @package_details_fetcher ||= T.let(
    Package::PackageDetailsFetcher.new(
      dependency: dependency,
      credentials: credentials,
      git_commit_checker: git_commit_checker
    ), T.nilable(Package::PackageDetailsFetcher)
  )
end

#release_date_to_seconds(release_date) ⇒ Object



99
100
101
102
103
104
# File 'lib/dependabot/swift/update_checker/latest_version_resolver.rb', line 99

def release_date_to_seconds(release_date)
  Time.parse(release_date).to_i
rescue ArgumentError => e
  Dependabot.logger.error("Invalid release date format: #{release_date} and error: #{e.message}")
  0 # Default to 360 days in seconds if parsing fails, so that it will not be in cooldown
end

#select_version_tags_in_cooldown_periodObject



65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/dependabot/swift/update_checker/latest_version_resolver.rb', line 65

def select_version_tags_in_cooldown_period
  version_tags_in_cooldown_period = T.let([], T::Array[String])

  package_details_fetcher.fetch_tag_and_release_date.each do |git_tag_with_detail|
    if check_if_version_in_cooldown_period?(T.must(git_tag_with_detail.release_date))
      version_tags_in_cooldown_period << git_tag_with_detail.tag
    end
  end
  version_tags_in_cooldown_period
rescue StandardError => e
  Dependabot.logger.error("Error checking if version is in cooldown: #{e.message}")
  version_tags_in_cooldown_period
end