summaryrefslogtreecommitdiff
path: root/lib/version_check.rb
blob: 2d132001f54cc499624173f0e0c0a74f782ea62c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
# frozen_string_literal: true

require "base64"

class VersionCheck
  include ReactiveCaching

  self.reactive_cache_work_type = :external_dependency
  self.reactive_cache_worker_finder = ->(_id, *args) { from_cache }

  def self.data
    { version: Gitlab::VERSION }
  end

  def self.headers
    { "REFERER": Gitlab.config.gitlab.url }
  end

  def self.url
    encoded_data = Base64.urlsafe_encode64(data.to_json)

    "#{host}/check.json?gitlab_info=#{encoded_data}"
  end

  def self.host
    'https://version.gitlab.com'
  end

  def self.from_cache(*)
    new
  end

  def id
    Gitlab::VERSION
  end

  def calculate_reactive_cache(*)
    response = Gitlab::HTTP.try_get(self.class.url, headers: self.class.headers)

    case response&.code
    when 200
      response.body
    end
  end

  def response
    with_reactive_cache do |data|
      Gitlab::Json.parse(data) if data
    end
  end
end

VersionCheck.prepend_mod