2020-11-24 13:09:14 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ReleaseHighlight
|
|
|
|
CACHE_DURATION = 1.hour
|
|
|
|
FILES_PATH = Rails.root.join('data', 'whats_new', '*.yml')
|
2020-12-10 10:10:12 -05:00
|
|
|
RELEASE_VERSIONS_IN_A_YEAR = 12
|
|
|
|
|
|
|
|
def self.for_version(version:)
|
|
|
|
index = self.versions.index(version)
|
|
|
|
|
|
|
|
return if index.nil?
|
|
|
|
|
|
|
|
page = index + 1
|
|
|
|
|
|
|
|
self.paginated(page: page)
|
|
|
|
end
|
2020-11-24 13:09:14 -05:00
|
|
|
|
|
|
|
def self.paginated(page: 1)
|
2020-12-10 10:10:12 -05:00
|
|
|
key = self.cache_key("items:page-#{page}")
|
|
|
|
|
|
|
|
Rails.cache.fetch(key, expires_in: CACHE_DURATION) do
|
2020-11-24 13:09:14 -05:00
|
|
|
items = self.load_items(page: page)
|
|
|
|
|
|
|
|
next if items.nil?
|
|
|
|
|
2020-12-10 10:10:12 -05:00
|
|
|
QueryResult.new(items: items, next_page: next_page(current_page: page))
|
2020-11-24 13:09:14 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.load_items(page:)
|
|
|
|
index = page - 1
|
|
|
|
file_path = file_paths[index]
|
|
|
|
|
|
|
|
return if file_path.nil?
|
|
|
|
|
|
|
|
file = File.read(file_path)
|
|
|
|
items = YAML.safe_load(file, permitted_classes: [Date])
|
|
|
|
|
|
|
|
platform = Gitlab.com? ? 'gitlab-com' : 'self-managed'
|
2020-12-15 16:09:53 -05:00
|
|
|
|
|
|
|
items&.map! do |item|
|
|
|
|
next unless item[platform]
|
|
|
|
|
|
|
|
begin
|
|
|
|
item.tap {|i| i['body'] = Kramdown::Document.new(i['body']).to_html }
|
|
|
|
rescue => e
|
|
|
|
Gitlab::ErrorTracking.track_exception(e, file_path: file_path)
|
|
|
|
|
|
|
|
next
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
items&.compact
|
2020-11-24 13:09:14 -05:00
|
|
|
rescue Psych::Exception => e
|
|
|
|
Gitlab::ErrorTracking.track_exception(e, file_path: file_path)
|
|
|
|
|
|
|
|
nil
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.file_paths
|
2020-12-10 10:10:12 -05:00
|
|
|
@file_paths ||= Rails.cache.fetch(self.cache_key('file_paths'), expires_in: CACHE_DURATION) do
|
2020-11-24 13:09:14 -05:00
|
|
|
Dir.glob(FILES_PATH).sort.reverse
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-10 10:10:12 -05:00
|
|
|
def self.cache_key(key)
|
|
|
|
['release_highlight', key, Gitlab.revision].join(':')
|
2020-11-24 13:09:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.next_page(current_page: 1)
|
|
|
|
next_page = current_page + 1
|
|
|
|
next_index = next_page - 1
|
|
|
|
|
|
|
|
next_page if self.file_paths[next_index]
|
|
|
|
end
|
|
|
|
|
2020-12-10 10:10:12 -05:00
|
|
|
def self.most_recent_item_count
|
|
|
|
key = self.cache_key('recent_item_count')
|
|
|
|
|
|
|
|
Gitlab::ProcessMemoryCache.cache_backend.fetch(key, expires_in: CACHE_DURATION) do
|
|
|
|
self.paginated&.items&.count
|
2020-11-24 13:09:14 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-12-10 10:10:12 -05:00
|
|
|
def self.versions
|
|
|
|
key = self.cache_key('versions')
|
|
|
|
|
|
|
|
Gitlab::ProcessMemoryCache.cache_backend.fetch(key, expires_in: CACHE_DURATION) do
|
|
|
|
versions = self.file_paths.first(RELEASE_VERSIONS_IN_A_YEAR).map do |path|
|
|
|
|
/\d*\_(\d*\_\d*)\.yml$/.match(path).captures[0].gsub(/0(?=\d)/, "").tr("_", ".")
|
|
|
|
end
|
|
|
|
|
|
|
|
versions.uniq
|
2020-11-24 13:09:14 -05:00
|
|
|
end
|
|
|
|
end
|
2020-12-10 10:10:12 -05:00
|
|
|
|
|
|
|
QueryResult = Struct.new(:items, :next_page, keyword_init: true) do
|
|
|
|
include Enumerable
|
|
|
|
|
|
|
|
delegate :each, to: :items
|
|
|
|
end
|
2020-11-24 13:09:14 -05:00
|
|
|
end
|