Class: ReleaseHighlight

Inherits:
Object
  • Object
show all
Defined in:
app/models/release_highlight.rb

Defined Under Namespace

Classes: QueryResult

Constant Summary collapse

CACHE_DURATION =
1.hour
FREE_PACKAGE =
'Free'
PREMIUM_PACKAGE =
'Premium'
ULTIMATE_PACKAGE =
'Ultimate'

Class Method Summary collapse

Class Method Details

.cache_key(key) ⇒ Object



70
71
72
73
# File 'app/models/release_highlight.rb', line 70

def self.cache_key(key)
  variant = Gitlab::CurrentSettings.current_application_settings.whats_new_variant
  ['release_highlight', variant, key, Gitlab.revision].join(':')
end

.current_packageObject



108
109
110
111
112
113
114
115
116
117
118
119
# File 'app/models/release_highlight.rb', line 108

def self.current_package
  return FREE_PACKAGE unless defined?(License)

  case License.current&.plan&.downcase
  when License::PREMIUM_PLAN
    PREMIUM_PACKAGE
  when License::ULTIMATE_PLAN
    ULTIMATE_PACKAGE
  else
    FREE_PACKAGE
  end
end

.file_pathsObject



60
61
62
# File 'app/models/release_highlight.rb', line 60

def self.file_paths
  @file_paths ||= self.relative_file_paths.map { |path| path.prepend(Rails.root.to_s) }
end

.include_item?(item) ⇒ Boolean

Returns:

  • (Boolean)


121
122
123
124
125
126
127
128
129
# File 'app/models/release_highlight.rb', line 121

def self.include_item?(item)
  platform = Gitlab.com? ? 'gitlab-com' : 'self-managed'

  return false unless item[platform]

  return true unless Gitlab::CurrentSettings.current_application_settings.whats_new_variant_current_tier?

  item['available_in']&.include?(current_package)
end

.load_items(page:) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'app/models/release_highlight.rb', line 28

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])

  items&.map! do |item|
    next unless include_item?(item)

    begin
      item.tap { |i| i['description'] = Banzai.render(i['description'], { project: nil }) }
    rescue StandardError => e
      Gitlab::ErrorTracking.track_exception(e, file_path: file_path)

      next
    end
  end

  items&.compact
rescue Psych::Exception => e
  Gitlab::ErrorTracking.track_exception(e, file_path: file_path)

  []
end

.most_recent_item_countObject



82
83
84
85
86
87
88
# File 'app/models/release_highlight.rb', line 82

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
  end
end

.most_recent_version_digestObject



90
91
92
93
94
95
96
97
98
99
100
# File 'app/models/release_highlight.rb', line 90

def self.most_recent_version_digest
  key = self.cache_key('most_recent_version_digest')

  Gitlab::ProcessMemoryCache.cache_backend.fetch(key, expires_in: CACHE_DURATION) do
    version = self.paginated&.items&.first&.[]('release')&.to_s

    next if version.nil?

    Digest::SHA256.hexdigest(version)
  end
end

.next_page(current_page: 1) ⇒ Object



75
76
77
78
79
80
# File 'app/models/release_highlight.rb', line 75

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

.next_page?(result) ⇒ Boolean

Returns:

  • (Boolean)


131
132
133
134
135
136
137
# File 'app/models/release_highlight.rb', line 131

def self.next_page?(result)
  return false unless result

  # if all items for the current page doesn't belong to the current tier
  # or failed to parse current YAML, loading next page
  result.items == [] && result.next_page.present?
end

.paginated(page: 1) ⇒ Object



10
11
12
13
14
# File 'app/models/release_highlight.rb', line 10

def self.paginated(page: 1)
  result = self.paginated_query(page: page)
  result = self.paginated_query(page: result.next_page) while next_page?(result)
  result
end

.paginated_query(page:) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/release_highlight.rb', line 16

def self.paginated_query(page:)
  key = self.cache_key("items:page-#{page}")

  Rails.cache.fetch(key, expires_in: CACHE_DURATION) do
    items = self.load_items(page: page)

    next if items.nil?

    QueryResult.new(items: items, next_page: next_page(current_page: page))
  end
end

.relative_file_pathsObject



64
65
66
67
68
# File 'app/models/release_highlight.rb', line 64

def self.relative_file_paths
  Rails.cache.fetch(self.cache_key('file_paths'), expires_in: CACHE_DURATION) do
    Dir.glob(whats_new_path).sort.reverse.map { |path| path.delete_prefix(Rails.root.to_s) }
  end
end

.whats_new_pathObject



56
57
58
# File 'app/models/release_highlight.rb', line 56

def self.whats_new_path
  Rails.root.join('data/whats_new/*.yml')
end