Class: Dependabot::Helm::LatestVersionResolver

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Defined in:
lib/dependabot/helm/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:) ⇒ LatestVersionResolver

Returns a new instance of LatestVersionResolver.



23
24
25
26
27
# File 'lib/dependabot/helm/update_checker/latest_version_resolver.rb', line 23

def initialize(dependency:, credentials:, cooldown_options:)
  @dependency = dependency
  @credentials = credentials
  @cooldown_options = cooldown_options
end

Instance Attribute Details

#credentialsObject (readonly)

Returns the value of attribute credentials.



188
189
190
# File 'lib/dependabot/helm/update_checker/latest_version_resolver.rb', line 188

def credentials
  @credentials
end

#dependencyObject (readonly)

Returns the value of attribute dependency.



30
31
32
# File 'lib/dependabot/helm/update_checker/latest_version_resolver.rb', line 30

def dependency
  @dependency
end

Instance Method Details

#check_if_version_in_cooldown_period?(release_date) ⇒ Boolean

Returns:

  • (Boolean)


132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/dependabot/helm/update_checker/latest_version_resolver.rb', line 132

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?

  # 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 < cooldown.default_days * DAY_IN_SECONDS
end

#cooldown_enabled?Boolean

Returns:

  • (Boolean)


183
184
185
# File 'lib/dependabot/helm/update_checker/latest_version_resolver.rb', line 183

def cooldown_enabled?
  true
end

#fetch_tag_and_release_date_helm_chart(versions, repo_name, chart_name) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/dependabot/helm/update_checker/latest_version_resolver.rb', line 58

def fetch_tag_and_release_date_helm_chart(versions, repo_name, chart_name)
  Dependabot.logger.info("Filtering versions in cooldown period from chart: #{repo_name}")
  # Using index URL to fetch tags in cooldown period"
  tags = select_tags_which_in_cooldown_from_chart_index("", T.must(chart_name))
  # If no tags in result then check from github api.
  tags = select_tags_which_in_cooldown_from_chart(T.must(repo_name)) if tags.nil? || tags.empty?

  return versions if tags.nil? || tags.empty?

  versions.reject! do |release|
    tags.any?(release["version"])
  end
  versions
rescue StandardError => e
  Dependabot.logger.error("Error fetch_tag_and_release_date_helm_chart(versions): #{e.message}")
  versions
end

#fetch_tag_and_release_date_helm_chart_index(index_url, versions, chart_name) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/dependabot/helm/update_checker/latest_version_resolver.rb', line 94

def fetch_tag_and_release_date_helm_chart_index(index_url, versions, chart_name)
  Dependabot.logger.info("Filtering versions in cooldown period from chart: #{index_url}")
  return versions if select_tags_which_in_cooldown_from_chart_index(index_url, chart_name).nil?

  select_tags_which_in_cooldown_from_chart_index(index_url, chart_name)&.each do |tag_name|
    # Iterate through versions and filter out those matching the tag_name
    versions.reject! do |version|
      version == tag_name
    end
  end
  Dependabot.logger.info(
    "Allowed version tags after filtering versions in cooldown:
        #{versions.map(&:to_s).join(', ')}"
  )
  versions
rescue StandardError => e
  Dependabot.logger.error("Error fetch_tag_and_release_date_helm_chart_index : #{e.message}")
  versions
end

#filter_versions_in_cooldown_period_using_oci(tags, tags_with_release_date) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dependabot/helm/update_checker/latest_version_resolver.rb', line 37

def filter_versions_in_cooldown_period_using_oci(tags, tags_with_release_date)
  select_tags_which_in_cooldown_using_oci(tags_with_release_date)&.each do |tag_name|
    # Iterate through versions and filter out those matching the tag_name
    tags.reject! do |version|
      version == tag_name
    end
  end
  tags
rescue StandardError => e
  Dependabot.logger.error("Error filter_versions_in_cooldown_period_for_oci:: #{e.message}")
  tags
end

#package_details_fetcherObject



172
173
174
175
176
177
178
179
180
# File 'lib/dependabot/helm/update_checker/latest_version_resolver.rb', line 172

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

#release_date_to_seconds(release_date) ⇒ Object



147
148
149
150
151
152
# File 'lib/dependabot/helm/update_checker/latest_version_resolver.rb', line 147

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_tags_which_in_cooldown_from_chart(repo_name) ⇒ Object



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/dependabot/helm/update_checker/latest_version_resolver.rb', line 77

def select_tags_which_in_cooldown_from_chart(repo_name)
  version_tags_in_cooldown_from_chart = T.let([], T::Array[String])

  begin
    package_details_fetcher.fetch_tag_and_release_date_from_chart(repo_name).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_from_chart << git_tag_with_detail.tag
      end
    end
    version_tags_in_cooldown_from_chart
  rescue StandardError => e
    Dependabot.logger.error("Error checking if version is in cooldown: #{e.message}")
    version_tags_in_cooldown_from_chart
  end
end

#select_tags_which_in_cooldown_from_chart_index(index_url, chart_name) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# File 'lib/dependabot/helm/update_checker/latest_version_resolver.rb', line 115

def select_tags_which_in_cooldown_from_chart_index(index_url, chart_name)
  fetch_tag_and_release_date_helm_chart_index = T.let([], T::Array[String])

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

#select_tags_which_in_cooldown_using_oci(tags_with_release_date) ⇒ Object



155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# File 'lib/dependabot/helm/update_checker/latest_version_resolver.rb', line 155

def select_tags_which_in_cooldown_using_oci(tags_with_release_date)
  fetch_tag_and_release_date_helm_using_oci = T.let([], T::Array[String])

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